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"
67 #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();
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 (*)(), int, void *, size_t);
147 void mydaemon(int, int);
148 void release_parent();
150 static int verbose = 0;
151 #define DEFAULT_IDMAP_CACHE_EXPIRY 600 /* seconds */
152 static int cache_entry_expiration = 0;
153 static char pipefsdir[PATH_MAX];
154 static char *nobodyuser, *nobodygroup;
155 static uid_t nobodyuid;
156 static gid_t nobodygid;
162 flush_nfsd_cache(char *path, time_t now)
167 sprintf(stime, "%ld\n", now);
168 fd = open(path, O_RDWR);
171 write(fd, stime, strlen(stime));
177 flush_nfsd_idmap_cache(void)
179 time_t now = time(NULL);
182 ret = flush_nfsd_cache(IC_IDNAME_FLUSH, now);
185 ret = flush_nfsd_cache(IC_NAMEID_FLUSH, now);
190 msg_format(char *rtnbuff, int rtnbuffsize, int errval,
191 const char *fmt, va_list args)
196 vsnprintf(buff, sizeof(buff), fmt, args);
198 if ((n = strlen(buff)) > 0 && buff[n-1] == '\n')
201 snprintf(rtnbuff, rtnbuffsize, "%s: %s", buff, strerror(errval));
205 idmapd_warn(const char *fmt, ...)
207 int errval = errno; /* save this! */
212 msg_format(buff, sizeof(buff), errval, fmt, args);
215 syslog(LOG_WARNING, "%s", buff);
219 idmapd_warnx(const char *fmt, ...)
224 vsyslog(LOG_WARNING, fmt, args);
229 idmapd_err(int eval, const char *fmt, ...)
231 int errval = errno; /* save this! */
236 msg_format(buff, sizeof(buff), errval, fmt, args);
239 syslog(LOG_ERR, "%s", buff);
244 idmapd_errx(int eval, const char *fmt, ...)
249 vsyslog(LOG_ERR, fmt, args);
255 main(int argc, char **argv)
257 int fd = 0, opt, fg = 0, nfsdret = -1;
258 struct idmap_clientq icq;
259 struct event rootdirev, clntdirev, svrdirev;
260 struct event initialize;
264 char *xpipefsdir = NULL;
265 int serverstart = 1, clientstart = 1;
269 conf_path = _PATH_IDMAPDCONF;
270 nobodyuser = NFS4NOBODY_USER;
271 nobodygroup = NFS4NOBODY_GROUP;
272 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
274 if ((progname = strrchr(argv[0], '/')))
278 openlog(progname, LOG_PID, LOG_DAEMON);
280 #define GETOPTSTR "vfd:p:U:G:c:CS"
281 opterr=0; /* Turn off error messages */
282 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
286 if (strchr(GETOPTSTR, optopt))
287 errx(1, "'-%c' option requires an argument.", optopt);
289 errx(1, "'-%c' is an invalid argument.", optopt);
294 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
295 warn("Skipping configuration file \"%s\"", conf_path);
299 verbose = conf_get_num("General", "Verbosity", 0);
300 cache_entry_expiration = conf_get_num("General",
301 "Cache-Expiration", DEFAULT_IDMAP_CACHE_EXPIRY);
302 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
303 if (xpipefsdir != NULL)
304 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
305 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
306 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
309 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
318 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
323 errx(1, "the -d, -U, and -G options have been removed;"
324 " please use the configuration file instead.");
335 if (!serverstart && !clientstart)
336 errx(1, "it is illegal to specify both -C and -S");
338 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
340 if ((pw = getpwnam(nobodyuser)) == NULL)
341 errx(1, "Could not find user \"%s\"", nobodyuser);
342 nobodyuid = pw->pw_uid;
344 if ((gr = getgrnam(nobodygroup)) == NULL)
345 errx(1, "Could not find group \"%s\"", nobodygroup);
346 nobodygid = gr->gr_gid;
348 #ifdef HAVE_NFS4_SET_DEBUG
349 nfs4_set_debug(verbose, idmapd_warnx);
351 if (conf_path == NULL)
352 conf_path = _PATH_IDMAPDCONF;
353 if (nfs4_init_name_mapping(conf_path))
354 errx(1, "Unable to create name to user id mappings.");
362 idmapd_warnx("Expiration time is %d seconds.",
363 cache_entry_expiration);
365 nfsdret = nfsdopen();
367 ret = flush_nfsd_idmap_cache();
370 "main: Failed to flush nfsd idmap cache\n");
375 struct timeval now = {
380 if (cache_entry_expiration != DEFAULT_IDMAP_CACHE_EXPIRY) {
382 char timeout_buf[12];
383 if ((timeout_fd = open(CLIENT_CACHE_TIMEOUT_FILE,
385 idmapd_warnx("Unable to open '%s' to set "
386 "client cache expiration time "
388 CLIENT_CACHE_TIMEOUT_FILE,
389 cache_entry_expiration);
391 len = snprintf(timeout_buf, sizeof(timeout_buf),
392 "%d", cache_entry_expiration);
393 if ((write(timeout_fd, timeout_buf, len)) != len)
394 idmapd_warnx("Error writing '%s' to "
395 "'%s' to set client "
396 "cache expiration time\n",
398 CLIENT_CACHE_TIMEOUT_FILE);
403 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
404 idmapd_err(1, "main: open(%s)", pipefsdir);
406 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
407 idmapd_err(1, "main: fcntl(%s)", pipefsdir);
409 if (fcntl(fd, F_NOTIFY,
410 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1)
411 idmapd_err(1, "main: fcntl(%s)", pipefsdir);
415 /* These events are persistent */
416 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
417 signal_add(&rootdirev, NULL);
418 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
419 signal_add(&clntdirev, NULL);
420 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
421 signal_add(&svrdirev, NULL);
423 /* Fetch current state */
424 /* (Delay till start of event_dispatch to avoid possibly losing
425 * a SIGUSR1 between here and the call to event_dispatch().) */
426 evtimer_set(&initialize, dirscancb, &icq);
427 evtimer_add(&initialize, &now);
430 if (nfsdret != 0 && fd == 0)
431 idmapd_errx(1, "main: Neither NFS client nor NFSd found");
435 if (event_dispatch() < 0)
436 idmapd_errx(1, "main: event_dispatch returns errno %d (%s)",
437 errno, strerror(errno));
443 dirscancb(int fd, short which, void *data)
446 struct dirent **ents;
447 struct idmap_client *ic;
449 struct idmap_clientq *icq = data;
451 nent = scandir(pipefsdir, &ents, NULL, alphasort);
453 idmapd_warn("dirscancb: scandir(%s)", pipefsdir);
457 for (i = 0; i < nent; i++) {
458 if (ents[i]->d_reclen > 4 &&
459 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
460 TAILQ_FOREACH(ic, icq, ic_next)
461 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
466 if ((ic = calloc(1, sizeof(*ic))) == NULL)
468 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
469 sizeof(ic->ic_clid));
471 snprintf(path, sizeof(path), "%s/%s",
472 pipefsdir, ents[i]->d_name);
474 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
475 idmapd_warn("dirscancb: open(%s)", path);
480 strlcat(path, "/idmap", sizeof(path));
481 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
484 idmapd_warnx("New client: %s", ic->ic_clid);
486 if (nfsopen(ic) == -1) {
492 ic->ic_id = "Client";
494 TAILQ_INSERT_TAIL(icq, ic, ic_next);
501 TAILQ_FOREACH(ic, icq, ic_next) {
502 if (!ic->ic_scanned) {
503 event_del(&ic->ic_event);
506 TAILQ_REMOVE(icq, ic, ic_next);
508 idmapd_warnx("Stale client: %s", ic->ic_clid);
509 idmapd_warnx("\t-> closed %s", ic->ic_path);
519 svrreopen(int fd, short which, void *data)
525 clntscancb(int fd, short which, void *data)
527 struct idmap_clientq *icq = data;
528 struct idmap_client *ic;
530 TAILQ_FOREACH(ic, icq, ic_next)
531 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
533 TAILQ_REMOVE(icq, ic, ic_next);
539 nfsdcb(int fd, short which, void *data)
541 struct idmap_client *ic = data;
543 u_char buf[IDMAP_MAXMSGSZ + 1];
546 char *bp, typebuf[IDMAP_MAXMSGSZ],
547 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
550 if (which != EV_READ)
553 if ((len = read(ic->ic_fd, buf, sizeof(buf))) <= 0) {
554 idmapd_warnx("nfsdcb: read(%s) failed: errno %d (%s)",
555 ic->ic_path, len?errno:0,
556 len?strerror(errno):"End of File");
560 /* Get rid of newline and terminate buffer*/
564 memset(&im, 0, sizeof(im));
566 /* Authentication name -- ignored for now*/
567 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
568 idmapd_warnx("nfsdcb: bad authentication name in upcall\n");
571 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
572 idmapd_warnx("nfsdcb: bad type in upcall\n");
576 idmapd_warnx("nfsdcb: authbuf=%s authtype=%s",
579 im.im_type = strcmp(typebuf, "user") == 0 ?
580 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
582 switch (ic->ic_which) {
584 im.im_conv = IDMAP_CONV_NAMETOID;
585 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
586 idmapd_warnx("nfsdcb: bad name in upcall\n");
591 im.im_conv = IDMAP_CONV_IDTONAME;
592 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
593 idmapd_warnx("nfsdcb: bad id in upcall\n");
596 tmp = strtoul(buf1, (char **)NULL, 10);
597 im.im_id = (u_int32_t)tmp;
598 if ((tmp == ULONG_MAX && errno == ERANGE)
599 || (unsigned long)im.im_id != tmp) {
600 idmapd_warnx("nfsdcb: id '%s' too big!\n", buf1);
605 idmapd_warnx("nfsdcb: Unknown which type %d", ic->ic_which);
615 /* Authentication name */
616 addfield(&bp, &bsiz, authbuf);
618 switch (ic->ic_which) {
621 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
622 addfield(&bp, &bsiz, p);
624 addfield(&bp, &bsiz, im.im_name);
626 snprintf(buf1, sizeof(buf1), "%lu",
627 time(NULL) + cache_entry_expiration);
628 addfield(&bp, &bsiz, buf1);
629 /* Note that we don't want to write the id if the mapping
630 * failed; instead, by leaving it off, we write a negative
631 * cache entry which will result in an error returned to
632 * the client. We don't want a chown or setacl referring
633 * to an unknown user to result in giving permissions to
635 if (im.im_status == IDMAP_STATUS_SUCCESS) {
637 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
638 addfield(&bp, &bsiz, buf1);
641 //if (bsiz == sizeof(buf)) /* XXX */
648 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
649 addfield(&bp, &bsiz, p);
651 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
652 addfield(&bp, &bsiz, buf1);
654 snprintf(buf1, sizeof(buf1), "%lu",
655 time(NULL) + cache_entry_expiration);
656 addfield(&bp, &bsiz, buf1);
657 /* Note we're ignoring the status field in this case; we'll
658 * just map to nobody instead. */
660 addfield(&bp, &bsiz, im.im_name);
666 idmapd_warnx("nfsdcb: Unknown which type %d", ic->ic_which);
670 bsiz = sizeof(buf) - bsiz;
672 if (atomicio(write, ic->ic_fd, buf, bsiz) != bsiz)
673 idmapd_warnx("nfsdcb: write(%s) failed: errno %d (%s)",
674 ic->ic_path, errno, strerror(errno));
677 event_add(&ic->ic_event, NULL);
681 imconv(struct idmap_client *ic, struct idmap_msg *im)
683 switch (im->im_conv) {
684 case IDMAP_CONV_IDTONAME:
687 idmapd_warnx("%s %s: (%s) id \"%d\" -> name \"%s\"",
688 ic->ic_id, ic->ic_clid,
689 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
690 im->im_id, im->im_name);
692 case IDMAP_CONV_NAMETOID:
693 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
694 im->im_status |= IDMAP_STATUS_INVALIDMSG;
699 idmapd_warnx("%s %s: (%s) name \"%s\" -> id \"%d\"",
700 ic->ic_id, ic->ic_clid,
701 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
702 im->im_name, im->im_id);
705 idmapd_warnx("imconv: Invalid conversion type (%d) in message",
707 im->im_status |= IDMAP_STATUS_INVALIDMSG;
713 nfscb(int fd, short which, void *data)
715 struct idmap_client *ic = data;
718 if (which != EV_READ)
721 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
723 idmapd_warn("nfscb: read(%s)", ic->ic_path);
731 /* XXX: I don't like ignoring this error in the id->name case,
732 * but we've never returned it, and I need to check that the client
733 * can handle it gracefully before starting to return it now. */
735 if (im.im_status == IDMAP_STATUS_LOOKUPFAIL)
736 im.im_status = IDMAP_STATUS_SUCCESS;
738 if (atomicio(write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
739 idmapd_warn("nfscb: write(%s)", ic->ic_path);
741 event_add(&ic->ic_event, NULL);
745 nfsdreopen_one(struct idmap_client *ic)
750 idmapd_warnx("ReOpening %s", ic->ic_path);
752 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
753 if ((ic->ic_event.ev_flags & EVLIST_INIT))
754 event_del(&ic->ic_event);
758 ic->ic_event.ev_fd = ic->ic_fd = fd;
759 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
760 event_add(&ic->ic_event, NULL);
762 idmapd_warnx("nfsdreopen: Opening '%s' failed: errno %d (%s)",
763 ic->ic_path, errno, strerror(errno));
770 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
771 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
778 return ((nfsdopenone(&nfsd_ic[IC_NAMEID]) == 0 &&
779 nfsdopenone(&nfsd_ic[IC_IDNAME]) == 0) ? 0 : -1);
783 nfsdopenone(struct idmap_client *ic)
785 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
787 idmapd_warnx("nfsdopenone: Opening %s failed: "
789 ic->ic_path, errno, strerror(errno));
793 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
794 event_add(&ic->ic_event, NULL);
797 idmapd_warnx("Opened %s", ic->ic_path);
803 nfsopen(struct idmap_client *ic)
805 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
808 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
809 fcntl(ic->ic_dirfd, F_NOTIFY,
810 DN_CREATE | DN_DELETE | DN_MULTISHOT);
813 idmapd_warn("nfsopen: open(%s)", ic->ic_path);
817 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
818 event_add(&ic->ic_event, NULL);
819 fcntl(ic->ic_dirfd, F_SETSIG, 0);
820 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
822 idmapd_warnx("Opened %s", ic->ic_path);
829 idtonameres(struct idmap_msg *im)
831 char domain[NFS4_MAX_DOMAIN_LEN];
834 ret = nfs4_get_default_domain(NULL, domain, sizeof(domain));
835 switch (im->im_type) {
836 case IDMAP_TYPE_USER:
837 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
838 sizeof(im->im_name));
840 if (strlen(nobodyuser) < sizeof(im->im_name))
841 strcpy(im->im_name, nobodyuser);
843 strcpy(im->im_name, NFS4NOBODY_USER);
846 case IDMAP_TYPE_GROUP:
847 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
848 sizeof(im->im_name));
850 if (strlen(nobodygroup) < sizeof(im->im_name))
851 strcpy(im->im_name, nobodygroup);
853 strcpy(im->im_name, NFS4NOBODY_GROUP);
858 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
860 im->im_status = IDMAP_STATUS_SUCCESS;
864 nametoidres(struct idmap_msg *im)
870 /* XXX: move nobody stuff to library calls
871 * (nfs4_get_nobody_user(domain), nfs4_get_nobody_group(domain)) */
873 im->im_status = IDMAP_STATUS_SUCCESS;
875 switch (im->im_type) {
876 case IDMAP_TYPE_USER:
877 ret = nfs4_name_to_uid(im->im_name, &uid);
878 im->im_id = (u_int32_t) uid;
880 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
881 im->im_id = nobodyuid;
884 case IDMAP_TYPE_GROUP:
885 ret = nfs4_name_to_gid(im->im_name, &gid);
886 im->im_id = (u_int32_t) gid;
888 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
889 im->im_id = nobodygid;
896 validateascii(char *string, u_int32_t len)
900 for (i = 0; i < len; i++) {
901 if (string[i] == '\0')
904 if (string[i] & 0x80)
908 if (string[i] != '\0')
915 addfield(char **bpp, ssize_t *bsizp, char *fld)
918 ssize_t bsiz = *bsizp;
920 while ((ch = *fld++) != '\0' && bsiz > 0) {
927 bp += snprintf(bp, bsiz, "\\%03o", ch);
938 if (bsiz < 1 || ch != '\0')
951 getfield(char **bpp, char *fld, size_t fldsz)
956 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
959 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
962 while (*bp != '\0' && fldsz > 1) {
964 if ((n = sscanf(bp, "\\%03o", &val)) != 1)
984 * mydaemon creates a pipe between the partent and child
985 * process. The parent process will wait until the
986 * child dies or writes a '1' on the pipe signaling
987 * that it started successfully.
989 int pipefds[2] = { -1, -1};
992 mydaemon(int nochdir, int noclose)
994 int pid, status, tempfd;
996 if (pipe(pipefds) < 0)
997 err(1, "mydaemon: pipe() failed: errno %d", errno);
999 if ((pid = fork ()) < 0)
1000 err(1, "mydaemon: fork() failed: errno %d", errno);
1004 * Parent. Wait for status from child.
1007 if (read(pipefds[0], &status, 1) != 1)
1015 if (chdir ("/") == -1)
1016 err(1, "mydaemon: chdir() failed: errno %d", errno);
1019 while (pipefds[1] <= 2) {
1020 pipefds[1] = dup(pipefds[1]);
1022 err(1, "mydaemon: dup() failed: errno %d", errno);
1026 tempfd = open("/dev/null", O_RDWR);
1028 tempfd = open("/", O_RDONLY);
1045 if (pipefds[1] > 0) {
1046 write(pipefds[1], &status, 1);