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));
350 /* These events are persistent */
351 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
352 signal_add(&rootdirev, NULL);
353 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
354 signal_add(&clntdirev, NULL);
355 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
356 signal_add(&svrdirev, NULL);
358 /* Fetch current state */
359 /* (Delay till start of event_dispatch to avoid possibly losing
360 * a SIGUSR1 between here and the call to event_dispatch().) */
361 evtimer_set(&initialize, dirscancb, &icq);
362 evtimer_add(&initialize, &now);
365 if (nfsdret != 0 && fd == 0)
366 xlog_err("main: Neither NFS client nor NFSd found");
370 if (event_dispatch() < 0)
371 xlog_err("main: event_dispatch returns errno %d (%s)",
372 errno, strerror(errno));
378 dirscancb(int fd, short which, void *data)
381 struct dirent **ents;
382 struct idmap_client *ic, *nextic;
384 struct idmap_clientq *icq = data;
386 nent = scandir(pipefsdir, &ents, NULL, alphasort);
388 xlog_warn("dirscancb: scandir(%s): %s", pipefsdir, strerror(errno));
392 for (i = 0; i < nent; i++) {
393 if (ents[i]->d_reclen > 4 &&
394 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
395 TAILQ_FOREACH(ic, icq, ic_next)
396 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
401 if ((ic = calloc(1, sizeof(*ic))) == NULL)
403 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
404 sizeof(ic->ic_clid));
406 snprintf(path, sizeof(path), "%s/%s",
407 pipefsdir, ents[i]->d_name);
409 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
410 xlog_warn("dirscancb: open(%s): %s", path, strerror(errno));
415 strlcat(path, "/idmap", sizeof(path));
416 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
419 xlog_warn("New client: %s", ic->ic_clid);
421 if (nfsopen(ic) == -1) {
427 ic->ic_id = "Client";
429 TAILQ_INSERT_TAIL(icq, ic, ic_next);
436 ic = TAILQ_FIRST(icq);
438 nextic=TAILQ_NEXT(ic, ic_next);
439 if (!ic->ic_scanned) {
440 event_del(&ic->ic_event);
443 TAILQ_REMOVE(icq, ic, ic_next);
445 xlog_warn("Stale client: %s", ic->ic_clid);
446 xlog_warn("\t-> closed %s", ic->ic_path);
455 for (i = 0; i < nent; i++)
462 svrreopen(int fd, short which, void *data)
468 clntscancb(int fd, short which, void *data)
470 struct idmap_clientq *icq = data;
471 struct idmap_client *ic;
473 TAILQ_FOREACH(ic, icq, ic_next)
474 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
476 TAILQ_REMOVE(icq, ic, ic_next);
482 nfsdcb(int fd, short which, void *data)
484 struct idmap_client *ic = data;
486 u_char buf[IDMAP_MAXMSGSZ + 1];
489 char *bp, typebuf[IDMAP_MAXMSGSZ],
490 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
493 if (which != EV_READ)
496 if ((len = read(ic->ic_fd, buf, sizeof(buf))) <= 0) {
497 xlog_warn("nfsdcb: read(%s) failed: errno %d (%s)",
498 ic->ic_path, len?errno:0,
499 len?strerror(errno):"End of File");
503 /* Get rid of newline and terminate buffer*/
507 memset(&im, 0, sizeof(im));
509 /* Authentication name -- ignored for now*/
510 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
511 xlog_warn("nfsdcb: bad authentication name in upcall\n");
514 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
515 xlog_warn("nfsdcb: bad type in upcall\n");
519 xlog_warn("nfsdcb: authbuf=%s authtype=%s",
522 im.im_type = strcmp(typebuf, "user") == 0 ?
523 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
525 switch (ic->ic_which) {
527 im.im_conv = IDMAP_CONV_NAMETOID;
528 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
529 xlog_warn("nfsdcb: bad name in upcall\n");
534 im.im_conv = IDMAP_CONV_IDTONAME;
535 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
536 xlog_warn("nfsdcb: bad id in upcall\n");
539 tmp = strtoul(buf1, (char **)NULL, 10);
540 im.im_id = (u_int32_t)tmp;
541 if ((tmp == ULONG_MAX && errno == ERANGE)
542 || (unsigned long)im.im_id != tmp) {
543 xlog_warn("nfsdcb: id '%s' too big!\n", buf1);
548 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
558 /* Authentication name */
559 addfield(&bp, &bsiz, authbuf);
561 switch (ic->ic_which) {
564 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
565 addfield(&bp, &bsiz, p);
567 addfield(&bp, &bsiz, im.im_name);
569 snprintf(buf1, sizeof(buf1), "%lu",
570 time(NULL) + cache_entry_expiration);
571 addfield(&bp, &bsiz, buf1);
572 /* Note that we don't want to write the id if the mapping
573 * failed; instead, by leaving it off, we write a negative
574 * cache entry which will result in an error returned to
575 * the client. We don't want a chown or setacl referring
576 * to an unknown user to result in giving permissions to
578 if (im.im_status == IDMAP_STATUS_SUCCESS) {
580 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
581 addfield(&bp, &bsiz, buf1);
584 //if (bsiz == sizeof(buf)) /* XXX */
591 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
592 addfield(&bp, &bsiz, p);
594 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
595 addfield(&bp, &bsiz, buf1);
597 snprintf(buf1, sizeof(buf1), "%lu",
598 time(NULL) + cache_entry_expiration);
599 addfield(&bp, &bsiz, buf1);
600 /* Note we're ignoring the status field in this case; we'll
601 * just map to nobody instead. */
603 addfield(&bp, &bsiz, im.im_name);
609 xlog_warn("nfsdcb: Unknown which type %d", ic->ic_which);
613 bsiz = sizeof(buf) - bsiz;
615 if (atomicio((void*)write, ic->ic_fd, buf, bsiz) != bsiz)
616 xlog_warn("nfsdcb: write(%s) failed: errno %d (%s)",
617 ic->ic_path, errno, strerror(errno));
620 event_add(&ic->ic_event, NULL);
624 imconv(struct idmap_client *ic, struct idmap_msg *im)
626 switch (im->im_conv) {
627 case IDMAP_CONV_IDTONAME:
630 xlog_warn("%s %s: (%s) id \"%d\" -> name \"%s\"",
631 ic->ic_id, ic->ic_clid,
632 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
633 im->im_id, im->im_name);
635 case IDMAP_CONV_NAMETOID:
636 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
637 im->im_status |= IDMAP_STATUS_INVALIDMSG;
642 xlog_warn("%s %s: (%s) name \"%s\" -> id \"%d\"",
643 ic->ic_id, ic->ic_clid,
644 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
645 im->im_name, im->im_id);
648 xlog_warn("imconv: Invalid conversion type (%d) in message",
650 im->im_status |= IDMAP_STATUS_INVALIDMSG;
656 nfscb(int fd, short which, void *data)
658 struct idmap_client *ic = data;
661 if (which != EV_READ)
664 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
666 xlog_warn("nfscb: read(%s): %s", ic->ic_path, strerror(errno));
674 /* XXX: I don't like ignoring this error in the id->name case,
675 * but we've never returned it, and I need to check that the client
676 * can handle it gracefully before starting to return it now. */
678 if (im.im_status == IDMAP_STATUS_LOOKUPFAIL)
679 im.im_status = IDMAP_STATUS_SUCCESS;
681 if (atomicio((void*)write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
682 xlog_warn("nfscb: write(%s): %s", ic->ic_path, strerror(errno));
684 event_add(&ic->ic_event, NULL);
688 nfsdreopen_one(struct idmap_client *ic)
693 xlog_warn("ReOpening %s", ic->ic_path);
695 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
696 if ((ic->ic_event.ev_flags & EVLIST_INIT))
697 event_del(&ic->ic_event);
701 ic->ic_event.ev_fd = ic->ic_fd = fd;
702 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
703 event_add(&ic->ic_event, NULL);
705 xlog_warn("nfsdreopen: Opening '%s' failed: errno %d (%s)",
706 ic->ic_path, errno, strerror(errno));
713 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
714 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
721 return ((nfsdopenone(&nfsd_ic[IC_NAMEID]) == 0 &&
722 nfsdopenone(&nfsd_ic[IC_IDNAME]) == 0) ? 0 : -1);
726 nfsdopenone(struct idmap_client *ic)
728 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
730 xlog_warn("nfsdopenone: Opening %s failed: "
732 ic->ic_path, errno, strerror(errno));
736 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
737 event_add(&ic->ic_event, NULL);
740 xlog_warn("Opened %s", ic->ic_path);
746 nfsopen(struct idmap_client *ic)
748 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
751 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
752 fcntl(ic->ic_dirfd, F_NOTIFY,
753 DN_CREATE | DN_DELETE | DN_MULTISHOT);
756 xlog_warn("nfsopen: open(%s): %s", ic->ic_path, strerror(errno));
760 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
761 event_add(&ic->ic_event, NULL);
762 fcntl(ic->ic_dirfd, F_SETSIG, 0);
763 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
765 xlog_warn("Opened %s", ic->ic_path);
772 idtonameres(struct idmap_msg *im)
774 char domain[NFS4_MAX_DOMAIN_LEN];
777 ret = nfs4_get_default_domain(NULL, domain, sizeof(domain));
778 switch (im->im_type) {
779 case IDMAP_TYPE_USER:
780 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
781 sizeof(im->im_name));
783 if (strlen(nobodyuser) < sizeof(im->im_name))
784 strcpy(im->im_name, nobodyuser);
786 strcpy(im->im_name, NFS4NOBODY_USER);
789 case IDMAP_TYPE_GROUP:
790 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
791 sizeof(im->im_name));
793 if (strlen(nobodygroup) < sizeof(im->im_name))
794 strcpy(im->im_name, nobodygroup);
796 strcpy(im->im_name, NFS4NOBODY_GROUP);
801 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
803 im->im_status = IDMAP_STATUS_SUCCESS;
807 nametoidres(struct idmap_msg *im)
813 /* XXX: move nobody stuff to library calls
814 * (nfs4_get_nobody_user(domain), nfs4_get_nobody_group(domain)) */
816 im->im_status = IDMAP_STATUS_SUCCESS;
818 switch (im->im_type) {
819 case IDMAP_TYPE_USER:
820 ret = nfs4_name_to_uid(im->im_name, &uid);
821 im->im_id = (u_int32_t) uid;
823 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
824 im->im_id = nobodyuid;
827 case IDMAP_TYPE_GROUP:
828 ret = nfs4_name_to_gid(im->im_name, &gid);
829 im->im_id = (u_int32_t) gid;
831 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
832 im->im_id = nobodygid;
839 validateascii(char *string, u_int32_t len)
843 for (i = 0; i < len; i++) {
844 if (string[i] == '\0')
847 if (string[i] & 0x80)
851 if (string[i] != '\0')
858 addfield(char **bpp, ssize_t *bsizp, char *fld)
861 ssize_t bsiz = *bsizp;
863 while ((ch = *fld++) != '\0' && bsiz > 0) {
870 bp += snprintf(bp, bsiz, "\\%03o", ch);
881 if (bsiz < 1 || ch != '\0')
894 getfield(char **bpp, char *fld, size_t fldsz)
899 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
902 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
905 while (*bp != '\0' && fldsz > 1) {
907 if ((n = sscanf(bp, "\\%03o", &val)) != 1)
927 * mydaemon creates a pipe between the partent and child
928 * process. The parent process will wait until the
929 * child dies or writes a '1' on the pipe signaling
930 * that it started successfully.
932 int pipefds[2] = { -1, -1};
935 mydaemon(int nochdir, int noclose)
937 int pid, status, tempfd;
939 if (pipe(pipefds) < 0)
940 err(1, "mydaemon: pipe() failed: errno %d", errno);
942 if ((pid = fork ()) < 0)
943 err(1, "mydaemon: fork() failed: errno %d", errno);
947 * Parent. Wait for status from child.
950 if (read(pipefds[0], &status, 1) != 1)
958 if (chdir ("/") == -1)
959 err(1, "mydaemon: chdir() failed: errno %d", errno);
962 while (pipefds[1] <= 2) {
963 pipefds[1] = dup(pipefds[1]);
965 err(1, "mydaemon: dup() failed: errno %d", errno);
969 tempfd = open("/dev/null", O_RDWR);
971 tempfd = open("/", O_RDONLY);
988 if (pipefds[1] > 0) {
989 write(pipefds[1], &status, 1);