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 write(fd, stime, strlen(stime));
178 flush_nfsd_idmap_cache(void)
180 time_t now = time(NULL);
183 ret = flush_nfsd_cache(IC_IDNAME_FLUSH, now);
186 ret = flush_nfsd_cache(IC_NAMEID_FLUSH, now);
191 main(int argc, char **argv)
193 int fd = 0, opt, fg = 0, nfsdret = -1;
194 struct idmap_clientq icq;
195 struct event rootdirev, clntdirev, svrdirev;
196 struct event initialize;
200 char *xpipefsdir = NULL;
201 int serverstart = 1, clientstart = 1;
205 conf_path = _PATH_IDMAPDCONF;
206 nobodyuser = NFS4NOBODY_USER;
207 nobodygroup = NFS4NOBODY_GROUP;
208 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
210 if ((progname = strrchr(argv[0], '/')))
216 #define GETOPTSTR "vfd:p:U:G:c:CS"
217 opterr=0; /* Turn off error messages */
218 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
222 if (strchr(GETOPTSTR, optopt))
223 errx(1, "'-%c' option requires an argument.", optopt);
225 errx(1, "'-%c' is an invalid argument.", optopt);
230 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
231 warn("Skipping configuration file \"%s\"", conf_path);
235 verbose = conf_get_num("General", "Verbosity", 0);
236 cache_entry_expiration = conf_get_num("General",
237 "Cache-Expiration", DEFAULT_IDMAP_CACHE_EXPIRY);
238 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
239 if (xpipefsdir != NULL)
240 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
241 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
242 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
245 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
254 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
259 errx(1, "the -d, -U, and -G options have been removed;"
260 " please use the configuration file instead.");
271 if (!serverstart && !clientstart)
272 errx(1, "it is illegal to specify both -C and -S");
274 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
276 if ((pw = getpwnam(nobodyuser)) == NULL)
277 errx(1, "Could not find user \"%s\"", nobodyuser);
278 nobodyuid = pw->pw_uid;
280 if ((gr = getgrnam(nobodygroup)) == NULL)
281 errx(1, "Could not find group \"%s\"", nobodygroup);
282 nobodygid = gr->gr_gid;
284 #ifdef HAVE_NFS4_SET_DEBUG
285 nfs4_set_debug(verbose, xlog_warn);
287 if (conf_path == NULL)
288 conf_path = _PATH_IDMAPDCONF;
289 if (nfs4_init_name_mapping(conf_path))
290 errx(1, "Unable to create name to user id mappings.");
298 xlog_warn("Expiration time is %d seconds.",
299 cache_entry_expiration);
301 nfsdret = nfsdopen();
303 ret = flush_nfsd_idmap_cache();
305 xlog_err("main: Failed to flush nfsd idmap cache\n: %s", strerror(errno));
310 struct timeval now = {
315 if (cache_entry_expiration != DEFAULT_IDMAP_CACHE_EXPIRY) {
317 char timeout_buf[12];
318 if ((timeout_fd = open(CLIENT_CACHE_TIMEOUT_FILE,
320 xlog_warn("Unable to open '%s' to set "
321 "client cache expiration time "
323 CLIENT_CACHE_TIMEOUT_FILE,
324 cache_entry_expiration);
326 len = snprintf(timeout_buf, sizeof(timeout_buf),
327 "%d", cache_entry_expiration);
328 if ((write(timeout_fd, timeout_buf, len)) != len)
329 xlog_warn("Error writing '%s' to "
330 "'%s' to set client "
331 "cache expiration time\n",
333 CLIENT_CACHE_TIMEOUT_FILE);
338 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
339 xlog_err("main: open(%s): %s", pipefsdir, strerror(errno));
341 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
342 xlog_err("main: fcntl(%s): %s", pipefsdir, strerror(errno));
344 if (fcntl(fd, F_NOTIFY,
345 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1) {
346 xlog_err("main: fcntl(%s): %s", pipefsdir, strerror(errno));
348 xlog_err("main: Possibly no Dnotify support in kernel.");
352 /* These events are persistent */
353 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
354 signal_add(&rootdirev, NULL);
355 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
356 signal_add(&clntdirev, NULL);
357 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
358 signal_add(&svrdirev, NULL);
360 /* Fetch current state */
361 /* (Delay till start of event_dispatch to avoid possibly losing
362 * a SIGUSR1 between here and the call to event_dispatch().) */
363 evtimer_set(&initialize, dirscancb, &icq);
364 evtimer_add(&initialize, &now);
367 if (nfsdret != 0 && fd == 0)
368 xlog_err("main: Neither NFS client nor NFSd found");
372 if (event_dispatch() < 0)
373 xlog_err("main: event_dispatch returns errno %d (%s)",
374 errno, strerror(errno));
380 dirscancb(int fd, short which, void *data)
383 struct dirent **ents;
384 struct idmap_client *ic, *nextic;
386 struct idmap_clientq *icq = data;
388 nent = scandir(pipefsdir, &ents, NULL, alphasort);
390 xlog_warn("dirscancb: scandir(%s): %s", pipefsdir, strerror(errno));
394 for (i = 0; i < nent; i++) {
395 if (ents[i]->d_reclen > 4 &&
396 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
397 TAILQ_FOREACH(ic, icq, ic_next)
398 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
403 if ((ic = calloc(1, sizeof(*ic))) == NULL)
405 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
406 sizeof(ic->ic_clid));
408 snprintf(path, sizeof(path), "%s/%s",
409 pipefsdir, ents[i]->d_name);
411 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
412 xlog_warn("dirscancb: open(%s): %s", path, strerror(errno));
417 strlcat(path, "/idmap", sizeof(path));
418 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
421 xlog_warn("New client: %s", ic->ic_clid);
423 if (nfsopen(ic) == -1) {
429 ic->ic_id = "Client";
431 TAILQ_INSERT_TAIL(icq, ic, ic_next);
438 ic = TAILQ_FIRST(icq);
440 nextic=TAILQ_NEXT(ic, ic_next);
441 if (!ic->ic_scanned) {
442 event_del(&ic->ic_event);
445 TAILQ_REMOVE(icq, ic, ic_next);
447 xlog_warn("Stale client: %s", ic->ic_clid);
448 xlog_warn("\t-> closed %s", ic->ic_path);
457 for (i = 0; i < nent; i++)
464 svrreopen(int fd, short which, void *data)
470 clntscancb(int fd, short which, void *data)
472 struct idmap_clientq *icq = data;
473 struct idmap_client *ic;
475 TAILQ_FOREACH(ic, icq, ic_next)
476 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
478 TAILQ_REMOVE(icq, ic, ic_next);
484 nfsdcb(int fd, short which, void *data)
486 struct idmap_client *ic = data;
488 u_char buf[IDMAP_MAXMSGSZ + 1];
491 char *bp, typebuf[IDMAP_MAXMSGSZ],
492 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
495 if (which != EV_READ)
498 if ((len = read(ic->ic_fd, buf, sizeof(buf))) <= 0) {
499 xlog_warn("nfsdcb: read(%s) failed: errno %d (%s)",
500 ic->ic_path, len?errno:0,
501 len?strerror(errno):"End of File");
505 /* Get rid of newline and terminate buffer*/
509 memset(&im, 0, sizeof(im));
511 /* Authentication name -- ignored for now*/
512 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
513 xlog_warn("nfsdcb: bad authentication name in upcall\n");
516 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
517 xlog_warn("nfsdcb: bad type in upcall\n");
521 xlog_warn("nfsdcb: authbuf=%s authtype=%s",
524 im.im_type = strcmp(typebuf, "user") == 0 ?
525 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
527 switch (ic->ic_which) {
529 im.im_conv = IDMAP_CONV_NAMETOID;
530 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
531 xlog_warn("nfsdcb: bad name in upcall\n");
536 im.im_conv = IDMAP_CONV_IDTONAME;
537 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
538 xlog_warn("nfsdcb: bad id in upcall\n");
541 tmp = strtoul(buf1, (char **)NULL, 10);
542 im.im_id = (u_int32_t)tmp;
543 if ((tmp == ULONG_MAX && errno == ERANGE)
544 || (unsigned long)im.im_id != tmp) {
545 xlog_warn("nfsdcb: id '%s' too big!\n", buf1);
550 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
560 /* Authentication name */
561 addfield(&bp, &bsiz, authbuf);
563 switch (ic->ic_which) {
566 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
567 addfield(&bp, &bsiz, p);
569 addfield(&bp, &bsiz, im.im_name);
571 snprintf(buf1, sizeof(buf1), "%lu",
572 time(NULL) + cache_entry_expiration);
573 addfield(&bp, &bsiz, buf1);
574 /* Note that we don't want to write the id if the mapping
575 * failed; instead, by leaving it off, we write a negative
576 * cache entry which will result in an error returned to
577 * the client. We don't want a chown or setacl referring
578 * to an unknown user to result in giving permissions to
580 if (im.im_status == IDMAP_STATUS_SUCCESS) {
582 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
583 addfield(&bp, &bsiz, buf1);
586 //if (bsiz == sizeof(buf)) /* XXX */
593 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
594 addfield(&bp, &bsiz, p);
596 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
597 addfield(&bp, &bsiz, buf1);
599 snprintf(buf1, sizeof(buf1), "%lu",
600 time(NULL) + cache_entry_expiration);
601 addfield(&bp, &bsiz, buf1);
602 /* Note we're ignoring the status field in this case; we'll
603 * just map to nobody instead. */
605 addfield(&bp, &bsiz, im.im_name);
611 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
615 bsiz = sizeof(buf) - bsiz;
617 if (atomicio((void*)write, ic->ic_fd, buf, bsiz) != bsiz)
618 xlog_warn("nfsdcb: write(%s) failed: errno %d (%s)",
619 ic->ic_path, errno, strerror(errno));
622 event_add(&ic->ic_event, NULL);
626 imconv(struct idmap_client *ic, struct idmap_msg *im)
628 switch (im->im_conv) {
629 case IDMAP_CONV_IDTONAME:
632 xlog_warn("%s %s: (%s) id \"%d\" -> name \"%s\"",
633 ic->ic_id, ic->ic_clid,
634 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
635 im->im_id, im->im_name);
637 case IDMAP_CONV_NAMETOID:
638 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
639 im->im_status |= IDMAP_STATUS_INVALIDMSG;
644 xlog_warn("%s %s: (%s) name \"%s\" -> id \"%d\"",
645 ic->ic_id, ic->ic_clid,
646 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
647 im->im_name, im->im_id);
650 xlog_warn("imconv: Invalid conversion type (%d) in message",
652 im->im_status |= IDMAP_STATUS_INVALIDMSG;
658 nfscb(int fd, short which, void *data)
660 struct idmap_client *ic = data;
663 if (which != EV_READ)
666 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
668 xlog_warn("nfscb: read(%s): %s", ic->ic_path, strerror(errno));
676 /* XXX: I don't like ignoring this error in the id->name case,
677 * but we've never returned it, and I need to check that the client
678 * can handle it gracefully before starting to return it now. */
680 if (im.im_status == IDMAP_STATUS_LOOKUPFAIL)
681 im.im_status = IDMAP_STATUS_SUCCESS;
683 if (atomicio((void*)write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
684 xlog_warn("nfscb: write(%s): %s", ic->ic_path, strerror(errno));
686 event_add(&ic->ic_event, NULL);
690 nfsdreopen_one(struct idmap_client *ic)
695 xlog_warn("ReOpening %s", ic->ic_path);
697 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
698 if ((ic->ic_event.ev_flags & EVLIST_INIT))
699 event_del(&ic->ic_event);
703 ic->ic_event.ev_fd = ic->ic_fd = fd;
704 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
705 event_add(&ic->ic_event, NULL);
707 xlog_warn("nfsdreopen: Opening '%s' failed: errno %d (%s)",
708 ic->ic_path, errno, strerror(errno));
715 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
716 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
723 return ((nfsdopenone(&nfsd_ic[IC_NAMEID]) == 0 &&
724 nfsdopenone(&nfsd_ic[IC_IDNAME]) == 0) ? 0 : -1);
728 nfsdopenone(struct idmap_client *ic)
730 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
732 xlog_warn("nfsdopenone: Opening %s failed: "
734 ic->ic_path, errno, strerror(errno));
738 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
739 event_add(&ic->ic_event, NULL);
742 xlog_warn("Opened %s", ic->ic_path);
748 nfsopen(struct idmap_client *ic)
750 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
753 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
754 fcntl(ic->ic_dirfd, F_NOTIFY,
755 DN_CREATE | DN_DELETE | DN_MULTISHOT);
758 xlog_warn("nfsopen: open(%s): %s", ic->ic_path, strerror(errno));
762 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
763 event_add(&ic->ic_event, NULL);
764 fcntl(ic->ic_dirfd, F_SETSIG, 0);
765 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
767 xlog_warn("Opened %s", ic->ic_path);
774 idtonameres(struct idmap_msg *im)
776 char domain[NFS4_MAX_DOMAIN_LEN];
779 ret = nfs4_get_default_domain(NULL, domain, sizeof(domain));
780 switch (im->im_type) {
781 case IDMAP_TYPE_USER:
782 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
783 sizeof(im->im_name));
785 if (strlen(nobodyuser) < sizeof(im->im_name))
786 strcpy(im->im_name, nobodyuser);
788 strcpy(im->im_name, NFS4NOBODY_USER);
791 case IDMAP_TYPE_GROUP:
792 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
793 sizeof(im->im_name));
795 if (strlen(nobodygroup) < sizeof(im->im_name))
796 strcpy(im->im_name, nobodygroup);
798 strcpy(im->im_name, NFS4NOBODY_GROUP);
803 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
805 im->im_status = IDMAP_STATUS_SUCCESS;
809 nametoidres(struct idmap_msg *im)
815 /* XXX: move nobody stuff to library calls
816 * (nfs4_get_nobody_user(domain), nfs4_get_nobody_group(domain)) */
818 im->im_status = IDMAP_STATUS_SUCCESS;
820 switch (im->im_type) {
821 case IDMAP_TYPE_USER:
822 ret = nfs4_name_to_uid(im->im_name, &uid);
823 im->im_id = (u_int32_t) uid;
825 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
826 im->im_id = nobodyuid;
829 case IDMAP_TYPE_GROUP:
830 ret = nfs4_name_to_gid(im->im_name, &gid);
831 im->im_id = (u_int32_t) gid;
833 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
834 im->im_id = nobodygid;
841 validateascii(char *string, u_int32_t len)
845 for (i = 0; i < len; i++) {
846 if (string[i] == '\0')
849 if (string[i] & 0x80)
853 if ((i >= len) || string[i] != '\0')
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);
990 if (pipefds[1] > 0) {
991 write(pipefds[1], &status, 1);