/* Make a link to FROM relative to FROMFD called TO relative to TOFD. */ int linkat (int fromfd, const char *from, int tofd, const char *to, int flags) { error_t err; file_t oldfile, linknode, todir; char *toname; /* POSIX says linkat doesn't follow symlinks by default, so pass O_NOLINK. That can be overridden by AT_SYMLINK_FOLLOW in FLAGS. */ oldfile = __file_name_lookup_at (fromfd, flags, from, O_NOLINK, 0); if (oldfile == MACH_PORT_NULL) return -1; /* The file_getlinknode RPC returns the port that should be passed to the receiving filesystem (the one containing TODIR) in dir_link. */ err = __file_getlinknode (oldfile, &linknode); __mach_port_deallocate (__mach_task_self (), oldfile); if (err) return __hurd_fail (err); todir = __file_name_split_at (tofd, to, &toname); if (todir != MACH_PORT_NULL) { err = __dir_link (todir, linknode, toname, 1); __mach_port_deallocate (__mach_task_self (), todir); } __mach_port_deallocate (__mach_task_self (), linknode); if (todir == MACH_PORT_NULL) return -1; if (err) return __hurd_fail (err); return 0; }
/* Make a link to FROM called TO. */ int __symlink (const char *from, const char *to) { error_t err; file_t dir, node; char *name; const size_t len = strlen (from) + 1; char buf[sizeof (_HURD_SYMLINK) + len]; /* A symlink is a file whose translator is "/hurd/symlink\0target\0". */ memcpy (buf, _HURD_SYMLINK, sizeof (_HURD_SYMLINK)); memcpy (&buf[sizeof (_HURD_SYMLINK)], from, len); dir = __file_name_split (to, &name); if (dir == MACH_PORT_NULL) return -1; /* Create a new, unlinked node in the target directory. */ err = __dir_mkfile (dir, O_WRITE, 0777 & ~_hurd_umask, &node); if (! err) { /* Set the node's translator to make it a symlink. */ err = __file_set_translator (node, FS_TRANS_EXCL|FS_TRANS_SET, FS_TRANS_EXCL|FS_TRANS_SET, 0, buf, sizeof (_HURD_SYMLINK) + len, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND); if (! err) /* Link the node, now a valid symlink, into the target directory. */ err = __dir_link (dir, node, name, 1); __mach_port_deallocate (__mach_task_self (), node); } __mach_port_deallocate (__mach_task_self (), dir); if (err) return __hurd_fail (err); return 0; }
/* Open the shared memory segment *R_KEY and return a file descriptor to it in R_FD. If KEY is IPC_PRIVATE, use a private key and return it in R_KEY. */ static error_t get_exclusive (int shmflags, size_t size, key_t *r_key, int *r_fd) { error_t err; file_t dir; file_t file; char filename[SHM_NAMEMAX]; key_t key = *r_key; bool is_private; /* Create the shared memory segment. */ err = create_shm_file (size, shmflags, &dir, &file); if (err) return err; if (key == IPC_PRIVATE) { is_private = true; key = SHM_PRIV_KEY_START; /* Try to link the shared memory segment into the filesystem (exclusively). Private segments have negative keys. */ do { sprintf (filename, SHM_NAMEPRI, key); err = __dir_link (dir, file, filename, 1); if (!err) { /* We are done. */ *r_key = key; break; } else if (err == EEXIST) { /* Check if we ran out of keys. If not, try again with new key. */ if (key == SHM_PRIV_KEY_END) err = ENOSPC; else err = 0; key--; } } while (!err); } else { /* Try to link the shared memory segment into the filesystem (exclusively) under the given key. */ sprintf (filename, SHM_NAMEPRI, key); err = __dir_link (dir, file, filename, 1); } __mach_port_deallocate (__mach_task_self (), dir); if (!err) { int fd; /* Get a file descriptor for that port. */ fd = _hurd_intern_fd (file, O_RDWR, 1); /* dealloc on error */ if (fd < 0) err = errno; else *r_fd = fd; } return err; }
/* Give the socket FD the local address ADDR (which is LEN bytes long). */ int __bind (int fd, __CONST_SOCKADDR_ARG addrarg, socklen_t len) { addr_port_t aport; error_t err; const struct sockaddr_un *addr = addrarg.__sockaddr_un__; if (addr->sun_family == AF_LOCAL) { /* For the local domain, we must create a node in the filesystem using the ifsock translator and then fetch the address from it. */ file_t dir, node; char name[len - offsetof (struct sockaddr_un, sun_path) + 1], *n; strncpy (name, addr->sun_path, sizeof name - 1); name[sizeof name - 1] = '\0'; /* Make sure */ dir = __file_name_split (name, &n); if (dir == MACH_PORT_NULL) return -1; /* Create a new, unlinked node in the target directory. */ err = __dir_mkfile (dir, O_CREAT, 0666 & ~_hurd_umask, &node); if (! err) { /* Set the node's translator to make it a local-domain socket. */ err = __file_set_translator (node, FS_TRANS_EXCL | FS_TRANS_SET, FS_TRANS_EXCL | FS_TRANS_SET, 0, _HURD_IFSOCK, sizeof _HURD_IFSOCK, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND); if (! err) { /* Link the node, now a socket, into the target directory. */ err = __dir_link (dir, node, n, 1); if (err == EEXIST) err = EADDRINUSE; } __mach_port_deallocate (__mach_task_self (), node); if (! err) { /* Get a port to the ifsock translator. */ file_t ifsock = __file_name_lookup_under (dir, n, 0, 0); if (ifsock == MACH_PORT_NULL) { err = errno; /* If we failed, get rid of the node we created. */ __dir_unlink (dir, n); } else { /* Get the address port. */ err = __ifsock_getsockaddr (ifsock, &aport); if (err == MIG_BAD_ID || err == EOPNOTSUPP) /* We are not talking to /hurd/ifsock. Probably someone came in after we linked our node, unlinked it, and replaced it with a different node, before we did our lookup. Treat it as if our link had failed with EEXIST. */ err = EADDRINUSE; } __mach_port_deallocate (__mach_task_self (), ifsock); } } __mach_port_deallocate (__mach_task_self (), dir); if (err) return __hurd_fail (err); }
/* Give the socket FD the local address ADDR (which is LEN bytes long). */ int __bind (int fd, __CONST_SOCKADDR_ARG addrarg, socklen_t len) { addr_port_t aport; error_t err; const struct sockaddr_un *addr = addrarg.__sockaddr_un__; if (addr->sun_family == AF_LOCAL) { /* For the local domain, we must create a node in the filesystem using the ifsock translator and then fetch the address from it. */ file_t dir, node, ifsock; char name[len - offsetof (struct sockaddr_un, sun_path) + 1], *n; strncpy (name, addr->sun_path, sizeof name - 1); name[sizeof name - 1] = '\0'; /* Make sure */ dir = __file_name_split (name, &n); if (dir == MACH_PORT_NULL) return -1; /* Create a new, unlinked node in the target directory. */ err = __dir_mkfile (dir, O_CREAT, 0666 & ~_hurd_umask, &node); if (! err) { /* Set the node's translator to make it a local-domain socket. */ err = __file_set_translator (node, FS_TRANS_EXCL | FS_TRANS_SET, FS_TRANS_EXCL | FS_TRANS_SET, 0, _HURD_IFSOCK, sizeof _HURD_IFSOCK, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND); if (! err) { enum retry_type doretry; char retryname[1024]; /* Get a port to the ifsock translator. */ err = __dir_lookup (node, "", 0, 0, &doretry, retryname, &ifsock); if (! err && (doretry != FS_RETRY_NORMAL || retryname[0] != '\0')) err = EADDRINUSE; } if (! err) { /* Get the address port. */ err = __ifsock_getsockaddr (ifsock, &aport); if (err == MIG_BAD_ID || err == EOPNOTSUPP) err = EGRATUITOUS; if (! err) { /* Link the node, now a socket with proper mode, into the target directory. */ err = __dir_link (dir, node, n, 1); if (err == EEXIST) err = EADDRINUSE; if (err) __mach_port_deallocate (__mach_task_self (), aport); } __mach_port_deallocate (__mach_task_self (), ifsock); } __mach_port_deallocate (__mach_task_self (), node); } __mach_port_deallocate (__mach_task_self (), dir); if (err) return __hurd_fail (err); }
/* Call the crash dump server to mummify us before we die. Returns nonzero if a core file was written. */ static int write_corefile (int signo, const struct hurd_signal_detail *detail) { error_t err; mach_port_t coreserver; file_t file, coredir; const char *name; /* Don't bother locking since we just read the one word. */ rlim_t corelimit = _hurd_rlimits[RLIMIT_CORE].rlim_cur; if (corelimit == 0) /* No core dumping, thank you very much. Note that this makes `ulimit -c 0' prevent crash-suspension too, which is probably what the user wanted. */ return 0; /* XXX RLIMIT_CORE: When we have a protocol to make the server return an error for RLIMIT_FSIZE, then tell the corefile fs server the RLIMIT_CORE value in place of the RLIMIT_FSIZE value. */ /* First get a port to the core dumping server. */ coreserver = MACH_PORT_NULL; name = _hurdsig_getenv ("CRASHSERVER"); if (name != NULL) coreserver = __file_name_lookup (name, 0, 0); if (coreserver == MACH_PORT_NULL) coreserver = __file_name_lookup (_SERVERS_CRASH, 0, 0); if (coreserver == MACH_PORT_NULL) return 0; /* Get a port to the directory where the new core file will reside. */ file = MACH_PORT_NULL; name = _hurdsig_getenv ("COREFILE"); if (name == NULL) name = "core"; coredir = __file_name_split (name, (char **) &name); if (coredir != MACH_PORT_NULL) /* Create the new file, but don't link it into the directory yet. */ __dir_mkfile (coredir, O_WRONLY|O_CREAT, 0600 & ~_hurd_umask, /* XXX ? */ &file); /* Call the core dumping server to write the core file. */ err = __crash_dump_task (coreserver, __mach_task_self (), file, signo, detail->code, detail->error, detail->exc, detail->exc_code, detail->exc_subcode, _hurd_ports[INIT_PORT_CTTYID].port, MACH_MSG_TYPE_COPY_SEND); __mach_port_deallocate (__mach_task_self (), coreserver); if (! err && file != MACH_PORT_NULL) /* The core dump into FILE succeeded, so now link it into the directory. */ err = __dir_link (coredir, file, name, 1); __mach_port_deallocate (__mach_task_self (), file); __mach_port_deallocate (__mach_task_self (), coredir); return !err && file != MACH_PORT_NULL; }
/* Create a device file named FILE_NAME, with permission and special bits MODE and device number DEV (which can be constructed from major and minor device numbers with the `makedev' macro above). */ int __xmknod (int vers, const char *file_name, mode_t mode, dev_t *dev) { error_t err; file_t dir, node; char *name; char buf[100], *bp; const char *translator; size_t len; if (vers != _MKNOD_VER) return __hurd_fail (EINVAL); if (S_ISCHR (mode)) { translator = _HURD_CHRDEV; len = sizeof (_HURD_CHRDEV); } else if (S_ISBLK (mode)) { translator = _HURD_BLKDEV; len = sizeof (_HURD_BLKDEV); } else if (S_ISFIFO (mode)) { translator = _HURD_FIFO; len = sizeof (_HURD_FIFO); } else { errno = EINVAL; return -1; } if (! S_ISFIFO (mode)) { /* We set the translator to "ifmt\0major\0minor\0", where IFMT depends on the S_IFMT bits of our MODE argument, and MAJOR and MINOR are ASCII decimal (octal or hex would do as well) representations of our arguments. Thus the convention is that CHRDEV and BLKDEV translators are invoked with two non-switch arguments, giving the major and minor device numbers in %i format. */ bp = buf + sizeof (buf); *--bp = '\0'; bp = _itoa (minor (*dev), bp, 10, 0); *--bp = '\0'; bp = _itoa (major (*dev), bp, 10, 0); memcpy (bp - len, translator, len); translator = bp - len; len = buf + sizeof (buf) - translator; } dir = __file_name_split (file_name, &name); if (dir == MACH_PORT_NULL) return -1; /* Create a new, unlinked node in the target directory. */ err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node); if (! err) /* Set the node's translator to make it a device. */ err = __file_set_translator (node, FS_TRANS_EXCL | FS_TRANS_SET, FS_TRANS_EXCL | FS_TRANS_SET, 0, translator, len, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND); if (! err) /* Link the node, now a valid device, into the target directory. */ err = __dir_link (dir, node, name, 1); __mach_port_deallocate (__mach_task_self (), dir); __mach_port_deallocate (__mach_task_self (), node); if (err) return __hurd_fail (err); return 0; }