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(void);
144 size_t strlcat(char *, const char *, size_t);
145 size_t strlcpy(char *, const char *, size_t);
146 ssize_t atomicio(ssize_t (*f) (int, void*, size_t),
147 int, void *, size_t);
148 void mydaemon(int, int);
149 void release_parent(void);
151 static int verbose = 0;
152 #define DEFAULT_IDMAP_CACHE_EXPIRY 600 /* seconds */
153 static int cache_entry_expiration = 0;
154 static char pipefsdir[PATH_MAX];
155 static char *nobodyuser, *nobodygroup;
156 static uid_t nobodyuid;
157 static gid_t nobodygid;
163 flush_nfsd_cache(char *path, time_t now)
168 sprintf(stime, "%ld\n", now);
169 fd = open(path, O_RDWR);
172 if (write(fd, stime, strlen(stime)) != strlen(stime)) {
173 errx(1, "Flushing nfsd cache failed: errno %d (%s)",
174 errno, strerror(errno));
181 flush_nfsd_idmap_cache(void)
183 time_t now = time(NULL);
186 ret = flush_nfsd_cache(IC_IDNAME_FLUSH, now);
189 ret = flush_nfsd_cache(IC_NAMEID_FLUSH, now);
194 main(int argc, char **argv)
196 int fd = 0, opt, fg = 0, nfsdret = -1;
197 struct idmap_clientq icq;
198 struct event rootdirev, clntdirev, svrdirev;
199 struct event initialize;
203 char *xpipefsdir = NULL;
204 int serverstart = 1, clientstart = 1;
208 conf_path = _PATH_IDMAPDCONF;
209 nobodyuser = NFS4NOBODY_USER;
210 nobodygroup = NFS4NOBODY_GROUP;
211 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
213 if ((progname = strrchr(argv[0], '/')))
219 #define GETOPTSTR "vfd:p:U:G:c:CS"
220 opterr=0; /* Turn off error messages */
221 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
225 if (strchr(GETOPTSTR, optopt))
226 errx(1, "'-%c' option requires an argument.", optopt);
228 errx(1, "'-%c' is an invalid argument.", optopt);
233 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
234 warn("Skipping configuration file \"%s\"", conf_path);
238 verbose = conf_get_num("General", "Verbosity", 0);
239 cache_entry_expiration = conf_get_num("General",
240 "Cache-Expiration", DEFAULT_IDMAP_CACHE_EXPIRY);
241 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
242 if (xpipefsdir != NULL)
243 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
244 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
245 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
248 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
257 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
262 errx(1, "the -d, -U, and -G options have been removed;"
263 " please use the configuration file instead.");
274 if (!serverstart && !clientstart)
275 errx(1, "it is illegal to specify both -C and -S");
277 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
279 if ((pw = getpwnam(nobodyuser)) == NULL)
280 errx(1, "Could not find user \"%s\"", nobodyuser);
281 nobodyuid = pw->pw_uid;
283 if ((gr = getgrnam(nobodygroup)) == NULL)
284 errx(1, "Could not find group \"%s\"", nobodygroup);
285 nobodygid = gr->gr_gid;
287 #ifdef HAVE_NFS4_SET_DEBUG
288 nfs4_set_debug(verbose, xlog_warn);
290 if (conf_path == NULL)
291 conf_path = _PATH_IDMAPDCONF;
292 if (nfs4_init_name_mapping(conf_path))
293 errx(1, "Unable to create name to user id mappings.");
301 xlog_warn("Expiration time is %d seconds.",
302 cache_entry_expiration);
304 nfsdret = nfsdopen();
306 ret = flush_nfsd_idmap_cache();
308 xlog_err("main: Failed to flush nfsd idmap cache\n: %s", strerror(errno));
313 struct timeval now = {
318 if (cache_entry_expiration != DEFAULT_IDMAP_CACHE_EXPIRY) {
320 char timeout_buf[12];
321 if ((timeout_fd = open(CLIENT_CACHE_TIMEOUT_FILE,
323 xlog_warn("Unable to open '%s' to set "
324 "client cache expiration time "
326 CLIENT_CACHE_TIMEOUT_FILE,
327 cache_entry_expiration);
329 len = snprintf(timeout_buf, sizeof(timeout_buf),
330 "%d", cache_entry_expiration);
331 if ((write(timeout_fd, timeout_buf, len)) != len)
332 xlog_warn("Error writing '%s' to "
333 "'%s' to set client "
334 "cache expiration time\n",
336 CLIENT_CACHE_TIMEOUT_FILE);
341 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
342 xlog_err("main: open(%s): %s", pipefsdir, strerror(errno));
344 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
345 xlog_err("main: fcntl(%s): %s", pipefsdir, strerror(errno));
347 if (fcntl(fd, F_NOTIFY,
348 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1) {
349 xlog_err("main: fcntl(%s): %s", pipefsdir, strerror(errno));
351 xlog_err("main: Possibly no Dnotify support in kernel.");
355 /* These events are persistent */
356 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
357 signal_add(&rootdirev, NULL);
358 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
359 signal_add(&clntdirev, NULL);
360 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
361 signal_add(&svrdirev, NULL);
363 /* Fetch current state */
364 /* (Delay till start of event_dispatch to avoid possibly losing
365 * a SIGUSR1 between here and the call to event_dispatch().) */
366 evtimer_set(&initialize, dirscancb, &icq);
367 evtimer_add(&initialize, &now);
370 if (nfsdret != 0 && fd == 0)
371 xlog_err("main: Neither NFS client nor NFSd found");
375 if (event_dispatch() < 0)
376 xlog_err("main: event_dispatch returns errno %d (%s)",
377 errno, strerror(errno));
383 dirscancb(int fd, short which, void *data)
386 struct dirent **ents;
387 struct idmap_client *ic, *nextic;
389 struct idmap_clientq *icq = data;
391 nent = scandir(pipefsdir, &ents, NULL, alphasort);
393 xlog_warn("dirscancb: scandir(%s): %s", pipefsdir, strerror(errno));
397 for (i = 0; i < nent; i++) {
398 if (ents[i]->d_reclen > 4 &&
399 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
400 TAILQ_FOREACH(ic, icq, ic_next)
401 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
406 if ((ic = calloc(1, sizeof(*ic))) == NULL)
408 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
409 sizeof(ic->ic_clid));
411 snprintf(path, sizeof(path), "%s/%s",
412 pipefsdir, ents[i]->d_name);
414 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
415 xlog_warn("dirscancb: open(%s): %s", path, strerror(errno));
420 strlcat(path, "/idmap", sizeof(path));
421 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
424 xlog_warn("New client: %s", ic->ic_clid);
426 if (nfsopen(ic) == -1) {
432 ic->ic_id = "Client";
434 TAILQ_INSERT_TAIL(icq, ic, ic_next);
441 ic = TAILQ_FIRST(icq);
443 nextic=TAILQ_NEXT(ic, ic_next);
444 if (!ic->ic_scanned) {
445 event_del(&ic->ic_event);
448 TAILQ_REMOVE(icq, ic, ic_next);
450 xlog_warn("Stale client: %s", ic->ic_clid);
451 xlog_warn("\t-> closed %s", ic->ic_path);
460 for (i = 0; i < nent; i++)
467 svrreopen(int fd, short which, void *data)
473 clntscancb(int fd, short which, void *data)
475 struct idmap_clientq *icq = data;
476 struct idmap_client *ic;
478 TAILQ_FOREACH(ic, icq, ic_next)
479 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
481 TAILQ_REMOVE(icq, ic, ic_next);
487 nfsdcb(int fd, short which, void *data)
489 struct idmap_client *ic = data;
491 u_char buf[IDMAP_MAXMSGSZ + 1];
494 char *bp, typebuf[IDMAP_MAXMSGSZ],
495 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
498 if (which != EV_READ)
501 if ((len = read(ic->ic_fd, buf, sizeof(buf))) <= 0) {
502 xlog_warn("nfsdcb: read(%s) failed: errno %d (%s)",
503 ic->ic_path, len?errno:0,
504 len?strerror(errno):"End of File");
508 /* Get rid of newline and terminate buffer*/
512 memset(&im, 0, sizeof(im));
514 /* Authentication name -- ignored for now*/
515 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
516 xlog_warn("nfsdcb: bad authentication name in upcall\n");
519 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
520 xlog_warn("nfsdcb: bad type in upcall\n");
524 xlog_warn("nfsdcb: authbuf=%s authtype=%s",
527 im.im_type = strcmp(typebuf, "user") == 0 ?
528 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
530 switch (ic->ic_which) {
532 im.im_conv = IDMAP_CONV_NAMETOID;
533 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
534 xlog_warn("nfsdcb: bad name in upcall\n");
539 im.im_conv = IDMAP_CONV_IDTONAME;
540 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
541 xlog_warn("nfsdcb: bad id in upcall\n");
544 tmp = strtoul(buf1, (char **)NULL, 10);
545 im.im_id = (u_int32_t)tmp;
546 if ((tmp == ULONG_MAX && errno == ERANGE)
547 || (unsigned long)im.im_id != tmp) {
548 xlog_warn("nfsdcb: id '%s' too big!\n", buf1);
553 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
563 /* Authentication name */
564 addfield(&bp, &bsiz, authbuf);
566 switch (ic->ic_which) {
569 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
570 addfield(&bp, &bsiz, p);
572 addfield(&bp, &bsiz, im.im_name);
574 snprintf(buf1, sizeof(buf1), "%lu",
575 time(NULL) + cache_entry_expiration);
576 addfield(&bp, &bsiz, buf1);
577 /* Note that we don't want to write the id if the mapping
578 * failed; instead, by leaving it off, we write a negative
579 * cache entry which will result in an error returned to
580 * the client. We don't want a chown or setacl referring
581 * to an unknown user to result in giving permissions to
583 if (im.im_status == IDMAP_STATUS_SUCCESS) {
585 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
586 addfield(&bp, &bsiz, buf1);
589 //if (bsiz == sizeof(buf)) /* XXX */
596 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
597 addfield(&bp, &bsiz, p);
599 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
600 addfield(&bp, &bsiz, buf1);
602 snprintf(buf1, sizeof(buf1), "%lu",
603 time(NULL) + cache_entry_expiration);
604 addfield(&bp, &bsiz, buf1);
605 /* Note we're ignoring the status field in this case; we'll
606 * just map to nobody instead. */
608 addfield(&bp, &bsiz, im.im_name);
614 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
618 bsiz = sizeof(buf) - bsiz;
620 if (atomicio((void*)write, ic->ic_fd, buf, bsiz) != bsiz)
621 xlog_warn("nfsdcb: write(%s) failed: errno %d (%s)",
622 ic->ic_path, errno, strerror(errno));
625 event_add(&ic->ic_event, NULL);
629 imconv(struct idmap_client *ic, struct idmap_msg *im)
631 switch (im->im_conv) {
632 case IDMAP_CONV_IDTONAME:
635 xlog_warn("%s %s: (%s) id \"%d\" -> name \"%s\"",
636 ic->ic_id, ic->ic_clid,
637 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
638 im->im_id, im->im_name);
640 case IDMAP_CONV_NAMETOID:
641 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
642 im->im_status |= IDMAP_STATUS_INVALIDMSG;
647 xlog_warn("%s %s: (%s) name \"%s\" -> id \"%d\"",
648 ic->ic_id, ic->ic_clid,
649 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
650 im->im_name, im->im_id);
653 xlog_warn("imconv: Invalid conversion type (%d) in message",
655 im->im_status |= IDMAP_STATUS_INVALIDMSG;
661 nfscb(int fd, short which, void *data)
663 struct idmap_client *ic = data;
666 if (which != EV_READ)
669 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
671 xlog_warn("nfscb: read(%s): %s", ic->ic_path, strerror(errno));
679 /* XXX: I don't like ignoring this error in the id->name case,
680 * but we've never returned it, and I need to check that the client
681 * can handle it gracefully before starting to return it now. */
683 if (im.im_status == IDMAP_STATUS_LOOKUPFAIL)
684 im.im_status = IDMAP_STATUS_SUCCESS;
686 if (atomicio((void*)write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
687 xlog_warn("nfscb: write(%s): %s", ic->ic_path, strerror(errno));
689 event_add(&ic->ic_event, NULL);
693 nfsdreopen_one(struct idmap_client *ic)
698 xlog_warn("ReOpening %s", ic->ic_path);
700 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
701 if ((ic->ic_event.ev_flags & EVLIST_INIT))
702 event_del(&ic->ic_event);
706 ic->ic_event.ev_fd = ic->ic_fd = fd;
707 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
708 event_add(&ic->ic_event, NULL);
710 xlog_warn("nfsdreopen: Opening '%s' failed: errno %d (%s)",
711 ic->ic_path, errno, strerror(errno));
718 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
719 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
726 return ((nfsdopenone(&nfsd_ic[IC_NAMEID]) == 0 &&
727 nfsdopenone(&nfsd_ic[IC_IDNAME]) == 0) ? 0 : -1);
731 nfsdopenone(struct idmap_client *ic)
733 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
735 xlog_warn("nfsdopenone: Opening %s failed: "
737 ic->ic_path, errno, strerror(errno));
741 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
742 event_add(&ic->ic_event, NULL);
745 xlog_warn("Opened %s", ic->ic_path);
751 nfsopen(struct idmap_client *ic)
753 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
756 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
757 fcntl(ic->ic_dirfd, F_NOTIFY,
758 DN_CREATE | DN_DELETE | DN_MULTISHOT);
761 xlog_warn("nfsopen: open(%s): %s", ic->ic_path, strerror(errno));
765 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
766 event_add(&ic->ic_event, NULL);
767 fcntl(ic->ic_dirfd, F_SETSIG, 0);
768 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
770 xlog_warn("Opened %s", ic->ic_path);
777 idtonameres(struct idmap_msg *im)
779 char domain[NFS4_MAX_DOMAIN_LEN];
782 ret = nfs4_get_default_domain(NULL, domain, sizeof(domain));
783 switch (im->im_type) {
784 case IDMAP_TYPE_USER:
785 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
786 sizeof(im->im_name));
788 if (strlen(nobodyuser) < sizeof(im->im_name))
789 strcpy(im->im_name, nobodyuser);
791 strcpy(im->im_name, NFS4NOBODY_USER);
794 case IDMAP_TYPE_GROUP:
795 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
796 sizeof(im->im_name));
798 if (strlen(nobodygroup) < sizeof(im->im_name))
799 strcpy(im->im_name, nobodygroup);
801 strcpy(im->im_name, NFS4NOBODY_GROUP);
806 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
808 im->im_status = IDMAP_STATUS_SUCCESS;
812 nametoidres(struct idmap_msg *im)
818 /* XXX: move nobody stuff to library calls
819 * (nfs4_get_nobody_user(domain), nfs4_get_nobody_group(domain)) */
821 im->im_status = IDMAP_STATUS_SUCCESS;
823 switch (im->im_type) {
824 case IDMAP_TYPE_USER:
825 ret = nfs4_name_to_uid(im->im_name, &uid);
826 im->im_id = (u_int32_t) uid;
828 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
829 im->im_id = nobodyuid;
832 case IDMAP_TYPE_GROUP:
833 ret = nfs4_name_to_gid(im->im_name, &gid);
834 im->im_id = (u_int32_t) gid;
836 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
837 im->im_id = nobodygid;
844 validateascii(char *string, u_int32_t len)
848 for (i = 0; i < len; i++) {
849 if (string[i] == '\0')
852 if (string[i] & 0x80)
856 if ((i >= len) || string[i] != '\0')
863 addfield(char **bpp, ssize_t *bsizp, char *fld)
866 ssize_t bsiz = *bsizp;
868 while ((ch = *fld++) != '\0' && bsiz > 0) {
875 bp += snprintf(bp, bsiz, "\\%03o", ch);
886 if (bsiz < 1 || ch != '\0')
899 getfield(char **bpp, char *fld, size_t fldsz)
904 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
907 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
910 while (*bp != '\0' && fldsz > 1) {
912 if ((n = sscanf(bp, "\\%03o", &val)) != 1)
932 * mydaemon creates a pipe between the partent and child
933 * process. The parent process will wait until the
934 * child dies or writes a '1' on the pipe signaling
935 * that it started successfully.
937 int pipefds[2] = { -1, -1};
940 mydaemon(int nochdir, int noclose)
942 int pid, status, tempfd;
944 if (pipe(pipefds) < 0)
945 err(1, "mydaemon: pipe() failed: errno %d", errno);
947 if ((pid = fork ()) < 0)
948 err(1, "mydaemon: fork() failed: errno %d", errno);
952 * Parent. Wait for status from child.
955 if (read(pipefds[0], &status, 1) != 1)
963 if (chdir ("/") == -1)
964 err(1, "mydaemon: chdir() failed: errno %d", errno);
967 while (pipefds[1] <= 2) {
968 pipefds[1] = dup(pipefds[1]);
970 err(1, "mydaemon: dup() failed: errno %d", errno);
974 tempfd = open("/dev/null", O_RDWR);
976 tempfd = open("/", O_RDONLY);
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));