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] = {
120 {IC_IDNAME, "Server", "", IC_IDNAME_CHAN, -1, -1, 0},
121 {IC_NAMEID, "Server", "", IC_NAMEID_CHAN, -1, -1, 0},
124 TAILQ_HEAD(idmap_clientq, idmap_client);
126 static void dirscancb(int, short, void *);
127 static void clntscancb(int, short, void *);
128 static void svrreopen(int, short, void *);
129 static int nfsopen(struct idmap_client *);
130 static void nfscb(int, short, void *);
131 static void nfsdcb(int, short, void *);
132 static int validateascii(char *, u_int32_t);
133 static int addfield(char **, ssize_t *, char *);
134 static int getfield(char **, char *, size_t);
136 static void imconv(struct idmap_client *, struct idmap_msg *);
137 static void idtonameres(struct idmap_msg *);
138 static void nametoidres(struct idmap_msg *);
140 static int nfsdopen(void);
141 static int nfsdopenone(struct idmap_client *);
142 static void nfsdreopen_one(struct idmap_client *);
143 static void nfsdreopen(void);
145 size_t strlcat(char *, const char *, size_t);
146 size_t strlcpy(char *, const char *, size_t);
147 ssize_t atomicio(ssize_t (*f) (int, void*, size_t),
148 int, void *, size_t);
149 void mydaemon(int, int);
150 void release_parent(void);
152 static int verbose = 0;
153 #define DEFAULT_IDMAP_CACHE_EXPIRY 600 /* seconds */
154 static int cache_entry_expiration = 0;
155 static char pipefsdir[PATH_MAX];
156 static char *nobodyuser, *nobodygroup;
157 static uid_t nobodyuid;
158 static gid_t nobodygid;
160 /* Used by conffile.c in libnfs.a */
164 flush_nfsd_cache(char *path, time_t now)
169 sprintf(stime, "%ld\n", now);
170 fd = open(path, O_RDWR);
173 if (write(fd, stime, strlen(stime)) != strlen(stime)) {
174 errx(1, "Flushing nfsd cache failed: errno %d (%s)",
175 errno, strerror(errno));
182 flush_nfsd_idmap_cache(void)
184 time_t now = time(NULL);
187 ret = flush_nfsd_cache(IC_IDNAME_FLUSH, now);
190 ret = flush_nfsd_cache(IC_NAMEID_FLUSH, now);
195 main(int argc, char **argv)
197 int fd = 0, opt, fg = 0, nfsdret = -1;
198 struct idmap_clientq icq;
199 struct event rootdirev, clntdirev, svrdirev;
200 struct event initialize;
204 char *xpipefsdir = NULL;
205 int serverstart = 1, clientstart = 1;
209 conf_path = _PATH_IDMAPDCONF;
210 nobodyuser = NFS4NOBODY_USER;
211 nobodygroup = NFS4NOBODY_GROUP;
212 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
214 if ((progname = strrchr(argv[0], '/')))
220 #define GETOPTSTR "vfd:p:U:G:c:CS"
221 opterr=0; /* Turn off error messages */
222 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
226 if (strchr(GETOPTSTR, optopt))
227 errx(1, "'-%c' option requires an argument.", optopt);
229 errx(1, "'-%c' is an invalid argument.", optopt);
234 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
235 warn("Skipping configuration file \"%s\"", conf_path);
239 verbose = conf_get_num("General", "Verbosity", 0);
240 cache_entry_expiration = conf_get_num("General",
241 "Cache-Expiration", DEFAULT_IDMAP_CACHE_EXPIRY);
242 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
243 if (xpipefsdir != NULL)
244 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
245 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
246 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
249 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
258 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
263 errx(1, "the -d, -U, and -G options have been removed;"
264 " please use the configuration file instead.");
275 if (!serverstart && !clientstart)
276 errx(1, "it is illegal to specify both -C and -S");
278 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
280 if ((pw = getpwnam(nobodyuser)) == NULL)
281 errx(1, "Could not find user \"%s\"", nobodyuser);
282 nobodyuid = pw->pw_uid;
284 if ((gr = getgrnam(nobodygroup)) == NULL)
285 errx(1, "Could not find group \"%s\"", nobodygroup);
286 nobodygid = gr->gr_gid;
288 #ifdef HAVE_NFS4_SET_DEBUG
289 nfs4_set_debug(verbose, xlog_warn);
291 if (conf_path == NULL)
292 conf_path = _PATH_IDMAPDCONF;
293 if (nfs4_init_name_mapping(conf_path))
294 errx(1, "Unable to create name to user id mappings.");
302 xlog_warn("Expiration time is %d seconds.",
303 cache_entry_expiration);
305 nfsdret = nfsdopen();
307 ret = flush_nfsd_idmap_cache();
309 xlog_err("main: Failed to flush nfsd idmap cache\n: %s", strerror(errno));
314 struct timeval now = {
319 if (cache_entry_expiration != DEFAULT_IDMAP_CACHE_EXPIRY) {
321 char timeout_buf[12];
322 if ((timeout_fd = open(CLIENT_CACHE_TIMEOUT_FILE,
324 xlog_warn("Unable to open '%s' to set "
325 "client cache expiration time "
327 CLIENT_CACHE_TIMEOUT_FILE,
328 cache_entry_expiration);
330 len = snprintf(timeout_buf, sizeof(timeout_buf),
331 "%d", cache_entry_expiration);
332 if ((write(timeout_fd, timeout_buf, len)) != len)
333 xlog_warn("Error writing '%s' to "
334 "'%s' to set client "
335 "cache expiration time\n",
337 CLIENT_CACHE_TIMEOUT_FILE);
342 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
343 xlog_err("main: open(%s): %s", pipefsdir, strerror(errno));
345 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
346 xlog_err("main: fcntl(%s): %s", pipefsdir, strerror(errno));
348 if (fcntl(fd, F_NOTIFY,
349 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1) {
350 xlog_err("main: fcntl(%s): %s", pipefsdir, strerror(errno));
352 xlog_err("main: Possibly no Dnotify support in kernel.");
356 /* These events are persistent */
357 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
358 signal_add(&rootdirev, NULL);
359 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
360 signal_add(&clntdirev, NULL);
361 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
362 signal_add(&svrdirev, NULL);
364 /* Fetch current state */
365 /* (Delay till start of event_dispatch to avoid possibly losing
366 * a SIGUSR1 between here and the call to event_dispatch().) */
367 evtimer_set(&initialize, dirscancb, &icq);
368 evtimer_add(&initialize, &now);
371 if (nfsdret != 0 && fd == 0)
372 xlog_err("main: Neither NFS client nor NFSd found");
376 if (event_dispatch() < 0)
377 xlog_err("main: event_dispatch returns errno %d (%s)",
378 errno, strerror(errno));
384 dirscancb(int fd, short which, void *data)
387 struct dirent **ents;
388 struct idmap_client *ic, *nextic;
390 struct idmap_clientq *icq = data;
392 nent = scandir(pipefsdir, &ents, NULL, alphasort);
394 xlog_warn("dirscancb: scandir(%s): %s", pipefsdir, strerror(errno));
398 for (i = 0; i < nent; i++) {
399 if (ents[i]->d_reclen > 4 &&
400 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
401 TAILQ_FOREACH(ic, icq, ic_next)
402 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
407 if ((ic = calloc(1, sizeof(*ic))) == NULL)
409 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
410 sizeof(ic->ic_clid));
412 snprintf(path, sizeof(path), "%s/%s",
413 pipefsdir, ents[i]->d_name);
415 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
416 xlog_warn("dirscancb: open(%s): %s", path, strerror(errno));
421 strlcat(path, "/idmap", sizeof(path));
422 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
425 xlog_warn("New client: %s", ic->ic_clid);
427 if (nfsopen(ic) == -1) {
433 ic->ic_id = "Client";
435 TAILQ_INSERT_TAIL(icq, ic, ic_next);
442 ic = TAILQ_FIRST(icq);
444 nextic=TAILQ_NEXT(ic, ic_next);
445 if (!ic->ic_scanned) {
446 event_del(&ic->ic_event);
449 TAILQ_REMOVE(icq, ic, ic_next);
451 xlog_warn("Stale client: %s", ic->ic_clid);
452 xlog_warn("\t-> closed %s", ic->ic_path);
461 for (i = 0; i < nent; i++)
468 svrreopen(int fd, short which, void *data)
474 clntscancb(int fd, short which, void *data)
476 struct idmap_clientq *icq = data;
477 struct idmap_client *ic;
479 TAILQ_FOREACH(ic, icq, ic_next)
480 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
482 TAILQ_REMOVE(icq, ic, ic_next);
488 nfsdcb(int fd, short which, void *data)
490 struct idmap_client *ic = data;
492 u_char buf[IDMAP_MAXMSGSZ + 1];
495 char *bp, typebuf[IDMAP_MAXMSGSZ],
496 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
499 if (which != EV_READ)
502 if ((len = read(ic->ic_fd, buf, sizeof(buf))) <= 0) {
503 xlog_warn("nfsdcb: read(%s) failed: errno %d (%s)",
504 ic->ic_path, len?errno:0,
505 len?strerror(errno):"End of File");
510 /* Get rid of newline and terminate buffer*/
514 memset(&im, 0, sizeof(im));
516 /* Authentication name -- ignored for now*/
517 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
518 xlog_warn("nfsdcb: bad authentication name in upcall\n");
521 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
522 xlog_warn("nfsdcb: bad type in upcall\n");
526 xlog_warn("nfsdcb: authbuf=%s authtype=%s",
529 im.im_type = strcmp(typebuf, "user") == 0 ?
530 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
532 switch (ic->ic_which) {
534 im.im_conv = IDMAP_CONV_NAMETOID;
535 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
536 xlog_warn("nfsdcb: bad name in upcall\n");
541 im.im_conv = IDMAP_CONV_IDTONAME;
542 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
543 xlog_warn("nfsdcb: bad id in upcall\n");
546 tmp = strtoul(buf1, (char **)NULL, 10);
547 im.im_id = (u_int32_t)tmp;
548 if ((tmp == ULONG_MAX && errno == ERANGE)
549 || (unsigned long)im.im_id != tmp) {
550 xlog_warn("nfsdcb: id '%s' too big!\n", buf1);
555 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
565 /* Authentication name */
566 addfield(&bp, &bsiz, authbuf);
568 switch (ic->ic_which) {
571 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
572 addfield(&bp, &bsiz, p);
574 addfield(&bp, &bsiz, im.im_name);
576 snprintf(buf1, sizeof(buf1), "%lu",
577 time(NULL) + cache_entry_expiration);
578 addfield(&bp, &bsiz, buf1);
579 /* Note that we don't want to write the id if the mapping
580 * failed; instead, by leaving it off, we write a negative
581 * cache entry which will result in an error returned to
582 * the client. We don't want a chown or setacl referring
583 * to an unknown user to result in giving permissions to
585 if (im.im_status == IDMAP_STATUS_SUCCESS) {
587 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
588 addfield(&bp, &bsiz, buf1);
591 //if (bsiz == sizeof(buf)) /* XXX */
598 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
599 addfield(&bp, &bsiz, p);
601 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
602 addfield(&bp, &bsiz, buf1);
604 snprintf(buf1, sizeof(buf1), "%lu",
605 time(NULL) + cache_entry_expiration);
606 addfield(&bp, &bsiz, buf1);
607 /* Note we're ignoring the status field in this case; we'll
608 * just map to nobody instead. */
610 addfield(&bp, &bsiz, im.im_name);
616 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
620 bsiz = sizeof(buf) - bsiz;
622 if (atomicio((void*)write, ic->ic_fd, buf, bsiz) != bsiz)
623 xlog_warn("nfsdcb: write(%s) failed: errno %d (%s)",
624 ic->ic_path, errno, strerror(errno));
627 event_add(&ic->ic_event, NULL);
631 imconv(struct idmap_client *ic, struct idmap_msg *im)
633 switch (im->im_conv) {
634 case IDMAP_CONV_IDTONAME:
637 xlog_warn("%s %s: (%s) id \"%d\" -> name \"%s\"",
638 ic->ic_id, ic->ic_clid,
639 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
640 im->im_id, im->im_name);
642 case IDMAP_CONV_NAMETOID:
643 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
644 im->im_status |= IDMAP_STATUS_INVALIDMSG;
649 xlog_warn("%s %s: (%s) name \"%s\" -> id \"%d\"",
650 ic->ic_id, ic->ic_clid,
651 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
652 im->im_name, im->im_id);
655 xlog_warn("imconv: Invalid conversion type (%d) in message",
657 im->im_status |= IDMAP_STATUS_INVALIDMSG;
663 nfscb(int fd, short which, void *data)
665 struct idmap_client *ic = data;
668 if (which != EV_READ)
671 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
673 xlog_warn("nfscb: read(%s): %s", ic->ic_path, strerror(errno));
681 /* XXX: I don't like ignoring this error in the id->name case,
682 * but we've never returned it, and I need to check that the client
683 * can handle it gracefully before starting to return it now. */
685 if (im.im_status == IDMAP_STATUS_LOOKUPFAIL)
686 im.im_status = IDMAP_STATUS_SUCCESS;
688 if (atomicio((void*)write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
689 xlog_warn("nfscb: write(%s): %s", ic->ic_path, strerror(errno));
691 event_add(&ic->ic_event, NULL);
695 nfsdreopen_one(struct idmap_client *ic)
700 xlog_warn("ReOpening %s", ic->ic_path);
702 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
703 if ((ic->ic_event.ev_flags & EVLIST_INIT))
704 event_del(&ic->ic_event);
708 ic->ic_event.ev_fd = ic->ic_fd = fd;
709 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
710 event_add(&ic->ic_event, NULL);
712 xlog_warn("nfsdreopen: Opening '%s' failed: errno %d (%s)",
713 ic->ic_path, errno, strerror(errno));
720 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
721 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
728 return ((nfsdopenone(&nfsd_ic[IC_NAMEID]) == 0 &&
729 nfsdopenone(&nfsd_ic[IC_IDNAME]) == 0) ? 0 : -1);
733 nfsdopenone(struct idmap_client *ic)
735 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
737 xlog_warn("nfsdopenone: Opening %s failed: "
739 ic->ic_path, errno, strerror(errno));
743 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
744 event_add(&ic->ic_event, NULL);
747 xlog_warn("Opened %s", ic->ic_path);
753 nfsopen(struct idmap_client *ic)
755 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
758 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
759 fcntl(ic->ic_dirfd, F_NOTIFY,
760 DN_CREATE | DN_DELETE | DN_MULTISHOT);
763 xlog_warn("nfsopen: open(%s): %s", ic->ic_path, strerror(errno));
767 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
768 event_add(&ic->ic_event, NULL);
769 fcntl(ic->ic_dirfd, F_SETSIG, 0);
770 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
772 xlog_warn("Opened %s", ic->ic_path);
779 idtonameres(struct idmap_msg *im)
781 char domain[NFS4_MAX_DOMAIN_LEN];
784 ret = nfs4_get_default_domain(NULL, domain, sizeof(domain));
785 switch (im->im_type) {
786 case IDMAP_TYPE_USER:
787 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
788 sizeof(im->im_name));
790 if (strlen(nobodyuser) < sizeof(im->im_name))
791 strcpy(im->im_name, nobodyuser);
793 strcpy(im->im_name, NFS4NOBODY_USER);
796 case IDMAP_TYPE_GROUP:
797 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
798 sizeof(im->im_name));
800 if (strlen(nobodygroup) < sizeof(im->im_name))
801 strcpy(im->im_name, nobodygroup);
803 strcpy(im->im_name, NFS4NOBODY_GROUP);
808 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
810 im->im_status = IDMAP_STATUS_SUCCESS;
814 nametoidres(struct idmap_msg *im)
820 /* XXX: move nobody stuff to library calls
821 * (nfs4_get_nobody_user(domain), nfs4_get_nobody_group(domain)) */
823 im->im_status = IDMAP_STATUS_SUCCESS;
825 switch (im->im_type) {
826 case IDMAP_TYPE_USER:
827 ret = nfs4_name_to_uid(im->im_name, &uid);
828 im->im_id = (u_int32_t) uid;
830 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
831 im->im_id = nobodyuid;
834 case IDMAP_TYPE_GROUP:
835 ret = nfs4_name_to_gid(im->im_name, &gid);
836 im->im_id = (u_int32_t) gid;
838 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
839 im->im_id = nobodygid;
846 validateascii(char *string, u_int32_t len)
850 for (i = 0; i < len; i++) {
851 if (string[i] == '\0')
854 if (string[i] & 0x80)
858 if ((i >= len) || string[i] != '\0')
865 addfield(char **bpp, ssize_t *bsizp, char *fld)
868 ssize_t bsiz = *bsizp;
870 while ((ch = *fld++) != '\0' && bsiz > 0) {
877 bp += snprintf(bp, bsiz, "\\%03o", ch);
888 if (bsiz < 1 || ch != '\0')
901 getfield(char **bpp, char *fld, size_t fldsz)
906 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
909 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
912 while (*bp != '\0' && fldsz > 1) {
914 if ((n = sscanf(bp, "\\%03o", &val)) != 1)
934 * mydaemon creates a pipe between the partent and child
935 * process. The parent process will wait until the
936 * child dies or writes a '1' on the pipe signaling
937 * that it started successfully.
939 int pipefds[2] = { -1, -1};
942 mydaemon(int nochdir, int noclose)
944 int pid, status, tempfd;
946 if (pipe(pipefds) < 0)
947 err(1, "mydaemon: pipe() failed: errno %d", errno);
949 if ((pid = fork ()) < 0)
950 err(1, "mydaemon: fork() failed: errno %d", errno);
954 * Parent. Wait for status from child.
957 if (read(pipefds[0], &status, 1) != 1)
965 if (chdir ("/") == -1)
966 err(1, "mydaemon: chdir() failed: errno %d", errno);
969 while (pipefds[1] <= 2) {
970 pipefds[1] = dup(pipefds[1]);
972 err(1, "mydaemon: dup() failed: errno %d", errno);
976 tempfd = open("/dev/null", O_RDWR);
978 tempfd = open("/", O_RDONLY);
985 err(1, "mydaemon: can't open /dev/null: errno %d",
998 if (pipefds[1] > 0) {
999 if (write(pipefds[1], &status, 1) != 1) {
1000 err(1, "Writing to parent pipe failed: errno %d (%s)\n",
1001 errno, strerror(errno));