4 * Userland daemon for idmap.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Marius Aamodt Eriksen <marius@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
40 #include <sys/socket.h>
44 #include "nfs_idmap.h"
66 #endif /* HAVE_CONFIG_H */
74 #define PIPEFS_DIR "/var/lib/nfs/rpc_pipefs/"
78 #define NFSD_DIR "/proc/net/rpc"
81 #ifndef CLIENT_CACHE_TIMEOUT_FILE
82 #define CLIENT_CACHE_TIMEOUT_FILE "/proc/sys/fs/nfs/idmap_cache_timeout"
85 #ifndef NFS4NOBODY_USER
86 #define NFS4NOBODY_USER "nobody"
89 #ifndef NFS4NOBODY_GROUP
90 #define NFS4NOBODY_GROUP "nobody"
94 #define CONF_SAVE(w, f) do { \
101 #define IC_IDNAME_CHAN NFSD_DIR "/nfs4.idtoname/channel"
102 #define IC_IDNAME_FLUSH NFSD_DIR "/nfs4.idtoname/flush"
105 #define IC_NAMEID_CHAN NFSD_DIR "/nfs4.nametoid/channel"
106 #define IC_NAMEID_FLUSH NFSD_DIR "/nfs4.nametoid/flush"
108 struct idmap_client {
112 char ic_path[PATH_MAX];
116 struct event ic_event;
117 TAILQ_ENTRY(idmap_client) ic_next;
119 static struct idmap_client nfsd_ic[2] = {
121 .ic_which = IC_IDNAME,
124 .ic_path = IC_IDNAME_CHAN,
130 .ic_which = IC_NAMEID,
133 .ic_path = IC_NAMEID_CHAN,
140 TAILQ_HEAD(idmap_clientq, idmap_client);
142 static void dirscancb(int, short, void *);
143 static void clntscancb(int, short, void *);
144 static void svrreopen(int, short, void *);
145 static int nfsopen(struct idmap_client *);
146 static void nfscb(int, short, void *);
147 static void nfsdcb(int, short, void *);
148 static int addfield(char **, ssize_t *, char *);
149 static int getfield(char **, char *, size_t);
151 static void imconv(struct idmap_client *, struct idmap_msg *);
152 static void idtonameres(struct idmap_msg *);
153 static void nametoidres(struct idmap_msg *);
155 static int nfsdopen(void);
156 static int nfsdopenone(struct idmap_client *);
157 static void nfsdreopen_one(struct idmap_client *);
158 static void nfsdreopen(void);
160 void mydaemon(int, int);
161 void release_parent(void);
163 static int verbose = 0;
164 #define DEFAULT_IDMAP_CACHE_EXPIRY 600 /* seconds */
165 static int cache_entry_expiration = 0;
166 static char pipefsdir[PATH_MAX];
167 static char *nobodyuser, *nobodygroup;
168 static uid_t nobodyuid;
169 static gid_t nobodygid;
171 /* Used by conffile.c in libnfs.a */
175 flush_nfsd_cache(char *path, time_t now)
180 sprintf(stime, "%ld\n", now);
181 fd = open(path, O_RDWR);
184 if (write(fd, stime, strlen(stime)) != (ssize_t)strlen(stime)) {
185 errx(1, "Flushing nfsd cache failed: errno %d (%s)",
186 errno, strerror(errno));
193 flush_nfsd_idmap_cache(void)
195 time_t now = time(NULL);
198 ret = flush_nfsd_cache(IC_IDNAME_FLUSH, now);
201 ret = flush_nfsd_cache(IC_NAMEID_FLUSH, now);
206 main(int argc, char **argv)
208 int fd = 0, opt, fg = 0, nfsdret = -1;
209 struct idmap_clientq icq;
210 struct event rootdirev, clntdirev, svrdirev;
211 struct event initialize;
215 char *xpipefsdir = NULL;
216 int serverstart = 1, clientstart = 1;
220 conf_path = _PATH_IDMAPDCONF;
221 nobodyuser = NFS4NOBODY_USER;
222 nobodygroup = NFS4NOBODY_GROUP;
223 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
225 if ((progname = strrchr(argv[0], '/')))
231 #define GETOPTSTR "vfd:p:U:G:c:CS"
232 opterr=0; /* Turn off error messages */
233 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
237 if (strchr(GETOPTSTR, optopt))
238 errx(1, "'-%c' option requires an argument.", optopt);
240 errx(1, "'-%c' is an invalid argument.", optopt);
245 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
246 warn("Skipping configuration file \"%s\"", conf_path);
250 verbose = conf_get_num("General", "Verbosity", 0);
251 cache_entry_expiration = conf_get_num("General",
252 "Cache-Expiration", DEFAULT_IDMAP_CACHE_EXPIRY);
253 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
254 if (xpipefsdir != NULL)
255 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
256 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
257 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
260 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
269 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
274 errx(1, "the -d, -U, and -G options have been removed;"
275 " please use the configuration file instead.");
286 if (!serverstart && !clientstart)
287 errx(1, "it is illegal to specify both -C and -S");
289 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
291 if ((pw = getpwnam(nobodyuser)) == NULL)
292 errx(1, "Could not find user \"%s\"", nobodyuser);
293 nobodyuid = pw->pw_uid;
295 if ((gr = getgrnam(nobodygroup)) == NULL)
296 errx(1, "Could not find group \"%s\"", nobodygroup);
297 nobodygid = gr->gr_gid;
299 #ifdef HAVE_NFS4_SET_DEBUG
300 nfs4_set_debug(verbose, xlog_warn);
302 if (conf_path == NULL)
303 conf_path = _PATH_IDMAPDCONF;
304 if (nfs4_init_name_mapping(conf_path))
305 errx(1, "Unable to create name to user id mappings.");
313 xlog_warn("Expiration time is %d seconds.",
314 cache_entry_expiration);
316 nfsdret = nfsdopen();
318 ret = flush_nfsd_idmap_cache();
320 xlog_err("main: Failed to flush nfsd idmap cache\n: %s", strerror(errno));
325 struct timeval now = {
330 if (cache_entry_expiration != DEFAULT_IDMAP_CACHE_EXPIRY) {
332 char timeout_buf[12];
333 if ((timeout_fd = open(CLIENT_CACHE_TIMEOUT_FILE,
335 xlog_warn("Unable to open '%s' to set "
336 "client cache expiration time "
338 CLIENT_CACHE_TIMEOUT_FILE,
339 cache_entry_expiration);
341 len = snprintf(timeout_buf, sizeof(timeout_buf),
342 "%d", cache_entry_expiration);
343 if ((write(timeout_fd, timeout_buf, len)) != len)
344 xlog_warn("Error writing '%s' to "
345 "'%s' to set client "
346 "cache expiration time\n",
348 CLIENT_CACHE_TIMEOUT_FILE);
353 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
354 xlog_err("main: open(%s): %s", pipefsdir, strerror(errno));
356 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
357 xlog_err("main: fcntl(%s): %s", pipefsdir, strerror(errno));
359 if (fcntl(fd, F_NOTIFY,
360 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1) {
361 xlog_err("main: fcntl(%s): %s", pipefsdir, strerror(errno));
363 xlog_err("main: Possibly no Dnotify support in kernel.");
367 /* These events are persistent */
368 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
369 signal_add(&rootdirev, NULL);
370 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
371 signal_add(&clntdirev, NULL);
372 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
373 signal_add(&svrdirev, NULL);
375 /* Fetch current state */
376 /* (Delay till start of event_dispatch to avoid possibly losing
377 * a SIGUSR1 between here and the call to event_dispatch().) */
378 evtimer_set(&initialize, dirscancb, &icq);
379 evtimer_add(&initialize, &now);
382 if (nfsdret != 0 && fd == 0)
383 xlog_err("main: Neither NFS client nor NFSd found");
387 if (event_dispatch() < 0)
388 xlog_err("main: event_dispatch returns errno %d (%s)",
389 errno, strerror(errno));
395 dirscancb(int UNUSED(fd), short UNUSED(which), void *data)
398 struct dirent **ents;
399 struct idmap_client *ic, *nextic;
401 struct idmap_clientq *icq = data;
403 nent = scandir(pipefsdir, &ents, NULL, alphasort);
405 xlog_warn("dirscancb: scandir(%s): %s", pipefsdir, strerror(errno));
409 for (i = 0; i < nent; i++) {
410 if (ents[i]->d_reclen > 4 &&
411 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
412 TAILQ_FOREACH(ic, icq, ic_next)
413 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
418 if ((ic = calloc(1, sizeof(*ic))) == NULL)
420 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
421 sizeof(ic->ic_clid));
423 snprintf(path, sizeof(path), "%s/%s",
424 pipefsdir, ents[i]->d_name);
426 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
428 xlog_warn("dirscancb: open(%s): %s", path, strerror(errno));
433 strlcat(path, "/idmap", sizeof(path));
434 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
437 xlog_warn("New client: %s", ic->ic_clid);
439 if (nfsopen(ic) == -1) {
445 ic->ic_id = "Client";
447 TAILQ_INSERT_TAIL(icq, ic, ic_next);
454 ic = TAILQ_FIRST(icq);
456 nextic=TAILQ_NEXT(ic, ic_next);
457 if (!ic->ic_scanned) {
458 event_del(&ic->ic_event);
461 TAILQ_REMOVE(icq, ic, ic_next);
463 xlog_warn("Stale client: %s", ic->ic_clid);
464 xlog_warn("\t-> closed %s", ic->ic_path);
473 for (i = 0; i < nent; i++)
480 svrreopen(int UNUSED(fd), short UNUSED(which), void *UNUSED(data))
486 clntscancb(int UNUSED(fd), short UNUSED(which), void *data)
488 struct idmap_clientq *icq = data;
489 struct idmap_client *ic;
491 TAILQ_FOREACH(ic, icq, ic_next)
492 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
494 TAILQ_REMOVE(icq, ic, ic_next);
500 nfsdcb(int UNUSED(fd), short which, void *data)
502 struct idmap_client *ic = data;
504 u_char buf[IDMAP_MAXMSGSZ + 1];
507 char *bp, typebuf[IDMAP_MAXMSGSZ],
508 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
511 if (which != EV_READ)
514 if ((len = read(ic->ic_fd, buf, sizeof(buf))) <= 0) {
515 xlog_warn("nfsdcb: read(%s) failed: errno %d (%s)",
516 ic->ic_path, len?errno:0,
517 len?strerror(errno):"End of File");
522 /* Get rid of newline and terminate buffer*/
526 memset(&im, 0, sizeof(im));
528 /* Authentication name -- ignored for now*/
529 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
530 xlog_warn("nfsdcb: bad authentication name in upcall\n");
533 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
534 xlog_warn("nfsdcb: bad type in upcall\n");
538 xlog_warn("nfsdcb: authbuf=%s authtype=%s",
541 im.im_type = strcmp(typebuf, "user") == 0 ?
542 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
544 switch (ic->ic_which) {
546 im.im_conv = IDMAP_CONV_NAMETOID;
547 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
548 xlog_warn("nfsdcb: bad name in upcall\n");
553 im.im_conv = IDMAP_CONV_IDTONAME;
554 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
555 xlog_warn("nfsdcb: bad id in upcall\n");
558 tmp = strtoul(buf1, (char **)NULL, 10);
559 im.im_id = (u_int32_t)tmp;
560 if ((tmp == ULONG_MAX && errno == ERANGE)
561 || (unsigned long)im.im_id != tmp) {
562 xlog_warn("nfsdcb: id '%s' too big!\n", buf1);
567 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
577 /* Authentication name */
578 addfield(&bp, &bsiz, authbuf);
580 switch (ic->ic_which) {
583 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
584 addfield(&bp, &bsiz, p);
586 addfield(&bp, &bsiz, im.im_name);
588 snprintf(buf1, sizeof(buf1), "%lu",
589 time(NULL) + cache_entry_expiration);
590 addfield(&bp, &bsiz, buf1);
591 /* Note that we don't want to write the id if the mapping
592 * failed; instead, by leaving it off, we write a negative
593 * cache entry which will result in an error returned to
594 * the client. We don't want a chown or setacl referring
595 * to an unknown user to result in giving permissions to
597 if (im.im_status == IDMAP_STATUS_SUCCESS) {
599 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
600 addfield(&bp, &bsiz, buf1);
603 //if (bsiz == sizeof(buf)) /* XXX */
610 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
611 addfield(&bp, &bsiz, p);
613 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
614 addfield(&bp, &bsiz, buf1);
616 snprintf(buf1, sizeof(buf1), "%lu",
617 time(NULL) + cache_entry_expiration);
618 addfield(&bp, &bsiz, buf1);
619 /* Note we're ignoring the status field in this case; we'll
620 * just map to nobody instead. */
622 addfield(&bp, &bsiz, im.im_name);
628 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
632 bsiz = sizeof(buf) - bsiz;
634 if (atomicio((void*)write, ic->ic_fd, buf, bsiz) != bsiz)
635 xlog_warn("nfsdcb: write(%s) failed: errno %d (%s)",
636 ic->ic_path, errno, strerror(errno));
639 event_add(&ic->ic_event, NULL);
643 imconv(struct idmap_client *ic, struct idmap_msg *im)
647 switch (im->im_conv) {
648 case IDMAP_CONV_IDTONAME:
651 xlog_warn("%s %s: (%s) id \"%d\" -> name \"%s\"",
652 ic->ic_id, ic->ic_clid,
653 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
654 im->im_id, im->im_name);
656 case IDMAP_CONV_NAMETOID:
657 len = strnlen(im->im_name, IDMAP_NAMESZ - 1);
658 /* Check for NULL termination just to be careful */
659 if (im->im_name[len+1] != '\0')
663 xlog_warn("%s %s: (%s) name \"%s\" -> id \"%d\"",
664 ic->ic_id, ic->ic_clid,
665 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
666 im->im_name, im->im_id);
669 xlog_warn("imconv: Invalid conversion type (%d) in message",
671 im->im_status |= IDMAP_STATUS_INVALIDMSG;
677 nfscb(int UNUSED(fd), short which, void *data)
679 struct idmap_client *ic = data;
682 if (which != EV_READ)
685 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
687 xlog_warn("nfscb: read(%s): %s", ic->ic_path, strerror(errno));
695 /* XXX: I don't like ignoring this error in the id->name case,
696 * but we've never returned it, and I need to check that the client
697 * can handle it gracefully before starting to return it now. */
699 if (im.im_status == IDMAP_STATUS_LOOKUPFAIL)
700 im.im_status = IDMAP_STATUS_SUCCESS;
702 if (atomicio((void*)write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
703 xlog_warn("nfscb: write(%s): %s", ic->ic_path, strerror(errno));
705 event_add(&ic->ic_event, NULL);
709 nfsdreopen_one(struct idmap_client *ic)
714 xlog_warn("ReOpening %s", ic->ic_path);
716 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
717 if ((ic->ic_event.ev_flags & EVLIST_INIT))
718 event_del(&ic->ic_event);
722 ic->ic_event.ev_fd = ic->ic_fd = fd;
723 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
724 event_add(&ic->ic_event, NULL);
726 xlog_warn("nfsdreopen: Opening '%s' failed: errno %d (%s)",
727 ic->ic_path, errno, strerror(errno));
734 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
735 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
742 return ((nfsdopenone(&nfsd_ic[IC_NAMEID]) == 0 &&
743 nfsdopenone(&nfsd_ic[IC_IDNAME]) == 0) ? 0 : -1);
747 nfsdopenone(struct idmap_client *ic)
749 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
751 xlog_warn("nfsdopenone: Opening %s failed: "
753 ic->ic_path, errno, strerror(errno));
757 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
758 event_add(&ic->ic_event, NULL);
761 xlog_warn("Opened %s", ic->ic_path);
767 nfsopen(struct idmap_client *ic)
769 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
772 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
773 fcntl(ic->ic_dirfd, F_NOTIFY,
774 DN_CREATE | DN_DELETE | DN_MULTISHOT);
777 xlog_warn("nfsopen: open(%s): %s", ic->ic_path, strerror(errno));
781 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
782 event_add(&ic->ic_event, NULL);
783 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
784 fcntl(ic->ic_dirfd, F_SETSIG, 0);
786 xlog_warn("Opened %s", ic->ic_path);
793 idtonameres(struct idmap_msg *im)
795 char domain[NFS4_MAX_DOMAIN_LEN];
798 ret = nfs4_get_default_domain(NULL, domain, sizeof(domain));
799 switch (im->im_type) {
800 case IDMAP_TYPE_USER:
801 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
802 sizeof(im->im_name));
804 if (strlen(nobodyuser) < sizeof(im->im_name))
805 strcpy(im->im_name, nobodyuser);
807 strcpy(im->im_name, NFS4NOBODY_USER);
810 case IDMAP_TYPE_GROUP:
811 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
812 sizeof(im->im_name));
814 if (strlen(nobodygroup) < sizeof(im->im_name))
815 strcpy(im->im_name, nobodygroup);
817 strcpy(im->im_name, NFS4NOBODY_GROUP);
822 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
824 im->im_status = IDMAP_STATUS_SUCCESS;
828 nametoidres(struct idmap_msg *im)
834 /* XXX: move nobody stuff to library calls
835 * (nfs4_get_nobody_user(domain), nfs4_get_nobody_group(domain)) */
837 im->im_status = IDMAP_STATUS_SUCCESS;
839 switch (im->im_type) {
840 case IDMAP_TYPE_USER:
841 ret = nfs4_name_to_uid(im->im_name, &uid);
842 im->im_id = (u_int32_t) uid;
844 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
845 im->im_id = nobodyuid;
848 case IDMAP_TYPE_GROUP:
849 ret = nfs4_name_to_gid(im->im_name, &gid);
850 im->im_id = (u_int32_t) gid;
852 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
853 im->im_id = nobodygid;
860 addfield(char **bpp, ssize_t *bsizp, char *fld)
863 ssize_t bsiz = *bsizp;
865 while ((ch = *fld++) != '\0' && bsiz > 0) {
872 bp += snprintf(bp, bsiz, "\\%03o", ch);
883 if (bsiz < 1 || ch != '\0')
896 getfield(char **bpp, char *fld, size_t fldsz)
901 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
904 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
907 while (*bp != '\0' && fldsz > 1) {
909 if ((n = sscanf(bp, "\\%03o", &val)) != 1)
929 * mydaemon creates a pipe between the partent and child
930 * process. The parent process will wait until the
931 * child dies or writes a '1' on the pipe signaling
932 * that it started successfully.
934 int pipefds[2] = { -1, -1};
937 mydaemon(int nochdir, int noclose)
939 int pid, status, tempfd;
941 if (pipe(pipefds) < 0)
942 err(1, "mydaemon: pipe() failed: errno %d", errno);
944 if ((pid = fork ()) < 0)
945 err(1, "mydaemon: fork() failed: errno %d", errno);
949 * Parent. Wait for status from child.
952 if (read(pipefds[0], &status, 1) != 1)
960 if (chdir ("/") == -1)
961 err(1, "mydaemon: chdir() failed: errno %d", errno);
964 while (pipefds[1] <= 2) {
965 pipefds[1] = dup(pipefds[1]);
967 err(1, "mydaemon: dup() failed: errno %d", errno);
971 tempfd = open("/dev/null", O_RDWR);
973 tempfd = open("/", O_RDONLY);
980 err(1, "mydaemon: can't open /dev/null: errno %d",
993 if (pipefds[1] > 0) {
994 if (write(pipefds[1], &status, 1) != 1) {
995 err(1, "Writing to parent pipe failed: errno %d (%s)\n",
996 errno, strerror(errno));