int mkfifo(FAR const char *pathname, mode_t mode) { struct pipe_dev_s *dev; int ret; /* Allocate and initialize a new device structure instance */ dev = pipecommon_allocdev(); if (!dev) { return -ENOMEM; } ret = register_driver(pathname, &fifo_fops, mode, (void*)dev); if (ret != 0) { pipecommon_freedev(dev); } return ret; }
int pipe(int fd[2]) { FAR struct pipe_dev_s *dev = NULL; char devname[16]; int pipeno; int err; int ret; /* Get exclusive access to the pipe allocation data */ ret = sem_wait(&g_pipesem); if (ret < 0) { /* sem_wait() will have already set errno */ return ERROR; } /* Allocate a minor number for the pipe device */ pipeno = pipe_allocate(); if (pipeno < 0) { (void)sem_post(&g_pipesem); err = -pipeno; goto errout; } /* Create a pathname to the pipe device */ sprintf(devname, "/dev/pipe%d", pipeno); /* Check if the pipe device has already been created */ if ((g_pipecreated & (1 << pipeno)) == 0) { /* No.. Allocate and initialize a new device structure instance */ dev = pipecommon_allocdev(); if (!dev) { (void)sem_post(&g_pipesem); err = ENOMEM; goto errout_with_pipe; } dev->d_pipeno = pipeno; /* Register the pipe device */ ret = register_driver(devname, &pipe_fops, 0666, (FAR void *)dev); if (ret != 0) { (void)sem_post(&g_pipesem); err = -ret; goto errout_with_dev; } /* Remember that we created this device */ g_pipecreated |= (1 << pipeno); } (void)sem_post(&g_pipesem); /* Get a write file descriptor */ fd[1] = open(devname, O_WRONLY); if (fd[1] < 0) { err = -fd[1]; goto errout_with_driver; } /* Get a read file descriptor */ fd[0] = open(devname, O_RDONLY); if (fd[0] < 0) { err = -fd[0]; goto errout_with_wrfd; } return OK; errout_with_wrfd: close(fd[1]); errout_with_driver: unregister_driver(devname); errout_with_dev: if (dev) { pipecommon_freedev(dev); } errout_with_pipe: pipe_free(pipeno); errout: set_errno(err); return ERROR; }