Close never called. (Open same device file from 2 Apps.)

hide_yama hide_yama at jcom.home.ne.jp
Mon Dec 19 05:34:55 PST 2005


 Hi everybody

Now, I'm writing device driver on 5.4R. But it doesn't act that I
unexpected.
So, if you know the reason and the avoidance, please tell to me.
Work is as follows.

1) Open device file "/dev/mypci" by App1.
   This App1 just calls open routine and sleep for a while in it's program.

2) Open and close the same device file by App2 befor App1 is expired.
   App2 calls just open and close routines in it's program.

I expect that App2 calls open and close routines, however App2 just calls
open, doesn't call close.

Why isn't the close routine called?
And are there any avoidance for this problem?

Thanks.
---
Chaki


[test driver]
----(/usr/srs/sys/dev/mypci/mypci.c start)----

#include <sys/param.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/bus.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <machine/resource.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>

/* Function prototypes */
static d_open_t    mypci_open;
static d_close_t   mypci_close;
static d_read_t    mypci_read;
static d_write_t   mypci_write;
static d_ioctl_t   mypci_ioctl;

/* Character device entry points */

static struct cdevsw mypci_cdevsw = {
	.d_version	= D_VERSION,
	.d_flags	= D_NEEDGIANT,
	.d_open		= mypci_open,
	.d_close	= mypci_close,
	.d_read		= mypci_read,
	.d_write	= mypci_write,
	.d_ioctl	= mypci_ioctl,
	.d_name		= "mypci",
};

static int
mypci_open(struct cdev *dev, int flags, int fmt, struct thread *td)
{
    int err = 0;

    printf("Opened device \"mypci\" successfully.\n");
    return 0;
}

static int
mypci_close(struct cdev *dev, int flags, int fmt, struct thread *td)
{
    int err = 0;

    printf("Closing device \"mypci.\"\n");
    return 0;
}

static int
mypci_read(struct cdev *dev, struct uio *uio, int ioflag)
{
    int err = 0;

    printf("mypci read!\n");
    return 0;
}

static int
mypci_write(struct cdev *dev, struct uio *uio, int ioflag)
{
    int err = 0;

    printf("mypci write!\n");
    return 0;
}


static int
mypci_probe(device_t dev)
{
    return (0);
}

static int
mypci_attach(device_t dev)
{

    make_dev(&mypci_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "mypci");
    printf("mypci device loaded.\n");
    return (0);
}


static	int
mypci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct
thread *td)
{
    printf("mypci ioctl!\n");
    return (0);
}


static device_method_t mypci_methods[] = {
    DEVMETHOD(device_probe,     mypci_probe),
    DEVMETHOD(device_attach,    mypci_attach),
    { 0, 0 }
};

static driver_t mypci_driver = {
    "mypci",
    mypci_methods,
    0,
};

static devclass_t mypci_devclass;

DRIVER_MODULE(mypci, pci, mypci_driver, mypci_devclass, 0, 0);

----(/usr/srs/sys/dev/mypci/mypci.c end)----


[test driver Makefile]
----(/usr/srs/sys/modules/mypci/Makefile start)----
.PATH: ${.CURDIR}/../../dev/mypci

SRCS=	bus_if.h device_if.h pci_if.h mypci.c
KMOD=	mypci


.include <bsd.kmod.mk>
----(/usr/srs/sys/modules/mypci/Makefile  end)----


[App1]
----(/tmp/App1.c start)----
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>

int main(void)
{
	int	fd;

	if ((fd = open("/dev/mypci", O_RDWR)) < 0){
		printf("open error\n");
		return -1;
	}
	printf("open OK\n");

	sleep(100000);

    return 0;
}
----(/tmp/App1.c end)----


[App2]
----(/tmp/App2.c start)----
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>

int main(void)
{

	int	fd;
	int	ret;

	if ((fd = open("/dev/mypci", O_RDWR)) < 0){
		printf("open error = %d\n", errno);
		return -1;
	}
	printf("open OK\n");

	if ((ret = close(fd)) < 0){
		printf("close errno = %d\n", errno);
		return -1;
	}
	printf("close OK\n");

	return 0;
}
----(/tmp/App2.c end)----




More information about the freebsd-drivers mailing list