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(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 msg_format(char *rtnbuff, int rtnbuffsize, int errval,
192 const char *fmt, va_list args)
197 vsnprintf(buff, sizeof(buff), fmt, args);
199 if ((n = strlen(buff)) > 0 && buff[n-1] == '\n')
202 snprintf(rtnbuff, rtnbuffsize, "%s: %s", buff, strerror(errval));
206 idmapd_warn(const char *fmt, ...)
208 int errval = errno; /* save this! */
213 msg_format(buff, sizeof(buff), errval, fmt, args);
216 syslog(LOG_WARNING, "%s", buff);
220 idmapd_warnx(const char *fmt, ...)
225 vsyslog(LOG_WARNING, fmt, args);
230 idmapd_err(int eval, const char *fmt, ...)
232 int errval = errno; /* save this! */
237 msg_format(buff, sizeof(buff), errval, fmt, args);
240 syslog(LOG_ERR, "%s", buff);
245 idmapd_errx(int eval, const char *fmt, ...)
250 vsyslog(LOG_ERR, fmt, args);
256 main(int argc, char **argv)
258 int fd = 0, opt, fg = 0, nfsdret = -1;
259 struct idmap_clientq icq;
260 struct event rootdirev, clntdirev, svrdirev;
261 struct event initialize;
265 char *xpipefsdir = NULL;
266 int serverstart = 1, clientstart = 1;
270 conf_path = _PATH_IDMAPDCONF;
271 nobodyuser = NFS4NOBODY_USER;
272 nobodygroup = NFS4NOBODY_GROUP;
273 strlcpy(pipefsdir, PIPEFS_DIR, sizeof(pipefsdir));
275 if ((progname = strrchr(argv[0], '/')))
279 openlog(progname, LOG_PID, LOG_DAEMON);
281 #define GETOPTSTR "vfd:p:U:G:c:CS"
282 opterr=0; /* Turn off error messages */
283 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1) {
287 if (strchr(GETOPTSTR, optopt))
288 errx(1, "'-%c' option requires an argument.", optopt);
290 errx(1, "'-%c' is an invalid argument.", optopt);
295 if (stat(conf_path, &sb) == -1 && (errno == ENOENT || errno == EACCES)) {
296 warn("Skipping configuration file \"%s\"", conf_path);
300 verbose = conf_get_num("General", "Verbosity", 0);
301 cache_entry_expiration = conf_get_num("General",
302 "Cache-Expiration", DEFAULT_IDMAP_CACHE_EXPIRY);
303 CONF_SAVE(xpipefsdir, conf_get_str("General", "Pipefs-Directory"));
304 if (xpipefsdir != NULL)
305 strlcpy(pipefsdir, xpipefsdir, sizeof(pipefsdir));
306 CONF_SAVE(nobodyuser, conf_get_str("Mapping", "Nobody-User"));
307 CONF_SAVE(nobodygroup, conf_get_str("Mapping", "Nobody-Group"));
310 while ((opt = getopt(argc, argv, GETOPTSTR)) != -1)
319 strlcpy(pipefsdir, optarg, sizeof(pipefsdir));
324 errx(1, "the -d, -U, and -G options have been removed;"
325 " please use the configuration file instead.");
336 if (!serverstart && !clientstart)
337 errx(1, "it is illegal to specify both -C and -S");
339 strncat(pipefsdir, "/nfs", sizeof(pipefsdir));
341 if ((pw = getpwnam(nobodyuser)) == NULL)
342 errx(1, "Could not find user \"%s\"", nobodyuser);
343 nobodyuid = pw->pw_uid;
345 if ((gr = getgrnam(nobodygroup)) == NULL)
346 errx(1, "Could not find group \"%s\"", nobodygroup);
347 nobodygid = gr->gr_gid;
349 #ifdef HAVE_NFS4_SET_DEBUG
350 nfs4_set_debug(verbose, idmapd_warnx);
352 if (conf_path == NULL)
353 conf_path = _PATH_IDMAPDCONF;
354 if (nfs4_init_name_mapping(conf_path))
355 errx(1, "Unable to create name to user id mappings.");
363 idmapd_warnx("Expiration time is %d seconds.",
364 cache_entry_expiration);
366 nfsdret = nfsdopen();
368 ret = flush_nfsd_idmap_cache();
371 "main: Failed to flush nfsd idmap cache\n");
376 struct timeval now = {
381 if (cache_entry_expiration != DEFAULT_IDMAP_CACHE_EXPIRY) {
383 char timeout_buf[12];
384 if ((timeout_fd = open(CLIENT_CACHE_TIMEOUT_FILE,
386 idmapd_warnx("Unable to open '%s' to set "
387 "client cache expiration time "
389 CLIENT_CACHE_TIMEOUT_FILE,
390 cache_entry_expiration);
392 len = snprintf(timeout_buf, sizeof(timeout_buf),
393 "%d", cache_entry_expiration);
394 if ((write(timeout_fd, timeout_buf, len)) != len)
395 idmapd_warnx("Error writing '%s' to "
396 "'%s' to set client "
397 "cache expiration time\n",
399 CLIENT_CACHE_TIMEOUT_FILE);
404 if ((fd = open(pipefsdir, O_RDONLY)) == -1)
405 idmapd_err(1, "main: open(%s)", pipefsdir);
407 if (fcntl(fd, F_SETSIG, SIGUSR1) == -1)
408 idmapd_err(1, "main: fcntl(%s)", pipefsdir);
410 if (fcntl(fd, F_NOTIFY,
411 DN_CREATE | DN_DELETE | DN_MODIFY | DN_MULTISHOT) == -1)
412 idmapd_err(1, "main: fcntl(%s)", pipefsdir);
416 /* These events are persistent */
417 signal_set(&rootdirev, SIGUSR1, dirscancb, &icq);
418 signal_add(&rootdirev, NULL);
419 signal_set(&clntdirev, SIGUSR2, clntscancb, &icq);
420 signal_add(&clntdirev, NULL);
421 signal_set(&svrdirev, SIGHUP, svrreopen, NULL);
422 signal_add(&svrdirev, NULL);
424 /* Fetch current state */
425 /* (Delay till start of event_dispatch to avoid possibly losing
426 * a SIGUSR1 between here and the call to event_dispatch().) */
427 evtimer_set(&initialize, dirscancb, &icq);
428 evtimer_add(&initialize, &now);
431 if (nfsdret != 0 && fd == 0)
432 idmapd_errx(1, "main: Neither NFS client nor NFSd found");
436 if (event_dispatch() < 0)
437 idmapd_errx(1, "main: event_dispatch returns errno %d (%s)",
438 errno, strerror(errno));
444 dirscancb(int fd, short which, void *data)
447 struct dirent **ents;
448 struct idmap_client *ic, *nextic;
450 struct idmap_clientq *icq = data;
452 nent = scandir(pipefsdir, &ents, NULL, alphasort);
454 idmapd_warn("dirscancb: scandir(%s)", pipefsdir);
458 for (i = 0; i < nent; i++) {
459 if (ents[i]->d_reclen > 4 &&
460 strncmp(ents[i]->d_name, "clnt", 4) == 0) {
461 TAILQ_FOREACH(ic, icq, ic_next)
462 if (strcmp(ents[i]->d_name + 4, ic->ic_clid) == 0)
467 if ((ic = calloc(1, sizeof(*ic))) == NULL)
469 strlcpy(ic->ic_clid, ents[i]->d_name + 4,
470 sizeof(ic->ic_clid));
472 snprintf(path, sizeof(path), "%s/%s",
473 pipefsdir, ents[i]->d_name);
475 if ((ic->ic_dirfd = open(path, O_RDONLY, 0)) == -1) {
476 idmapd_warn("dirscancb: open(%s)", path);
481 strlcat(path, "/idmap", sizeof(path));
482 strlcpy(ic->ic_path, path, sizeof(ic->ic_path));
485 idmapd_warnx("New client: %s", ic->ic_clid);
487 if (nfsopen(ic) == -1) {
493 ic->ic_id = "Client";
495 TAILQ_INSERT_TAIL(icq, ic, ic_next);
502 ic = TAILQ_FIRST(icq);
504 nextic=TAILQ_NEXT(ic, ic_next);
505 if (!ic->ic_scanned) {
506 event_del(&ic->ic_event);
509 TAILQ_REMOVE(icq, ic, ic_next);
511 idmapd_warnx("Stale client: %s", ic->ic_clid);
512 idmapd_warnx("\t-> closed %s", ic->ic_path);
521 for (i = 0; i < nent; i++)
528 svrreopen(int fd, short which, void *data)
534 clntscancb(int fd, short which, void *data)
536 struct idmap_clientq *icq = data;
537 struct idmap_client *ic;
539 TAILQ_FOREACH(ic, icq, ic_next)
540 if (ic->ic_fd == -1 && nfsopen(ic) == -1) {
542 TAILQ_REMOVE(icq, ic, ic_next);
548 nfsdcb(int fd, short which, void *data)
550 struct idmap_client *ic = data;
552 u_char buf[IDMAP_MAXMSGSZ + 1];
555 char *bp, typebuf[IDMAP_MAXMSGSZ],
556 buf1[IDMAP_MAXMSGSZ], authbuf[IDMAP_MAXMSGSZ], *p;
559 if (which != EV_READ)
562 if ((len = read(ic->ic_fd, buf, sizeof(buf))) <= 0) {
563 idmapd_warnx("nfsdcb: read(%s) failed: errno %d (%s)",
564 ic->ic_path, len?errno:0,
565 len?strerror(errno):"End of File");
569 /* Get rid of newline and terminate buffer*/
573 memset(&im, 0, sizeof(im));
575 /* Authentication name -- ignored for now*/
576 if (getfield(&bp, authbuf, sizeof(authbuf)) == -1) {
577 idmapd_warnx("nfsdcb: bad authentication name in upcall\n");
580 if (getfield(&bp, typebuf, sizeof(typebuf)) == -1) {
581 idmapd_warnx("nfsdcb: bad type in upcall\n");
585 idmapd_warnx("nfsdcb: authbuf=%s authtype=%s",
588 im.im_type = strcmp(typebuf, "user") == 0 ?
589 IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;
591 switch (ic->ic_which) {
593 im.im_conv = IDMAP_CONV_NAMETOID;
594 if (getfield(&bp, im.im_name, sizeof(im.im_name)) == -1) {
595 idmapd_warnx("nfsdcb: bad name in upcall\n");
600 im.im_conv = IDMAP_CONV_IDTONAME;
601 if (getfield(&bp, buf1, sizeof(buf1)) == -1) {
602 idmapd_warnx("nfsdcb: bad id in upcall\n");
605 tmp = strtoul(buf1, (char **)NULL, 10);
606 im.im_id = (u_int32_t)tmp;
607 if ((tmp == ULONG_MAX && errno == ERANGE)
608 || (unsigned long)im.im_id != tmp) {
609 idmapd_warnx("nfsdcb: id '%s' too big!\n", buf1);
614 idmapd_warnx("nfsdcb: Unknown which type %d", ic->ic_which);
624 /* Authentication name */
625 addfield(&bp, &bsiz, authbuf);
627 switch (ic->ic_which) {
630 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
631 addfield(&bp, &bsiz, p);
633 addfield(&bp, &bsiz, im.im_name);
635 snprintf(buf1, sizeof(buf1), "%lu",
636 time(NULL) + cache_entry_expiration);
637 addfield(&bp, &bsiz, buf1);
638 /* Note that we don't want to write the id if the mapping
639 * failed; instead, by leaving it off, we write a negative
640 * cache entry which will result in an error returned to
641 * the client. We don't want a chown or setacl referring
642 * to an unknown user to result in giving permissions to
644 if (im.im_status == IDMAP_STATUS_SUCCESS) {
646 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
647 addfield(&bp, &bsiz, buf1);
650 //if (bsiz == sizeof(buf)) /* XXX */
657 p = im.im_type == IDMAP_TYPE_USER ? "user" : "group";
658 addfield(&bp, &bsiz, p);
660 snprintf(buf1, sizeof(buf1), "%u", im.im_id);
661 addfield(&bp, &bsiz, buf1);
663 snprintf(buf1, sizeof(buf1), "%lu",
664 time(NULL) + cache_entry_expiration);
665 addfield(&bp, &bsiz, buf1);
666 /* Note we're ignoring the status field in this case; we'll
667 * just map to nobody instead. */
669 addfield(&bp, &bsiz, im.im_name);
675 idmapd_warnx("nfsdcb: Unknown which type %d", ic->ic_which);
679 bsiz = sizeof(buf) - bsiz;
681 if (atomicio((void*)write, ic->ic_fd, buf, bsiz) != bsiz)
682 idmapd_warnx("nfsdcb: write(%s) failed: errno %d (%s)",
683 ic->ic_path, errno, strerror(errno));
686 event_add(&ic->ic_event, NULL);
690 imconv(struct idmap_client *ic, struct idmap_msg *im)
692 switch (im->im_conv) {
693 case IDMAP_CONV_IDTONAME:
696 idmapd_warnx("%s %s: (%s) id \"%d\" -> name \"%s\"",
697 ic->ic_id, ic->ic_clid,
698 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
699 im->im_id, im->im_name);
701 case IDMAP_CONV_NAMETOID:
702 if (validateascii(im->im_name, sizeof(im->im_name)) == -1) {
703 im->im_status |= IDMAP_STATUS_INVALIDMSG;
708 idmapd_warnx("%s %s: (%s) name \"%s\" -> id \"%d\"",
709 ic->ic_id, ic->ic_clid,
710 im->im_type == IDMAP_TYPE_USER ? "user" : "group",
711 im->im_name, im->im_id);
714 idmapd_warnx("imconv: Invalid conversion type (%d) in message",
716 im->im_status |= IDMAP_STATUS_INVALIDMSG;
722 nfscb(int fd, short which, void *data)
724 struct idmap_client *ic = data;
727 if (which != EV_READ)
730 if (atomicio(read, ic->ic_fd, &im, sizeof(im)) != sizeof(im)) {
732 idmapd_warn("nfscb: read(%s)", ic->ic_path);
740 /* XXX: I don't like ignoring this error in the id->name case,
741 * but we've never returned it, and I need to check that the client
742 * can handle it gracefully before starting to return it now. */
744 if (im.im_status == IDMAP_STATUS_LOOKUPFAIL)
745 im.im_status = IDMAP_STATUS_SUCCESS;
747 if (atomicio((void*)write, ic->ic_fd, &im, sizeof(im)) != sizeof(im))
748 idmapd_warn("nfscb: write(%s)", ic->ic_path);
750 event_add(&ic->ic_event, NULL);
754 nfsdreopen_one(struct idmap_client *ic)
759 idmapd_warnx("ReOpening %s", ic->ic_path);
761 if ((fd = open(ic->ic_path, O_RDWR, 0)) != -1) {
762 if ((ic->ic_event.ev_flags & EVLIST_INIT))
763 event_del(&ic->ic_event);
767 ic->ic_event.ev_fd = ic->ic_fd = fd;
768 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
769 event_add(&ic->ic_event, NULL);
771 idmapd_warnx("nfsdreopen: Opening '%s' failed: errno %d (%s)",
772 ic->ic_path, errno, strerror(errno));
779 nfsdreopen_one(&nfsd_ic[IC_NAMEID]);
780 nfsdreopen_one(&nfsd_ic[IC_IDNAME]);
787 return ((nfsdopenone(&nfsd_ic[IC_NAMEID]) == 0 &&
788 nfsdopenone(&nfsd_ic[IC_IDNAME]) == 0) ? 0 : -1);
792 nfsdopenone(struct idmap_client *ic)
794 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
796 idmapd_warnx("nfsdopenone: Opening %s failed: "
798 ic->ic_path, errno, strerror(errno));
802 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfsdcb, ic);
803 event_add(&ic->ic_event, NULL);
806 idmapd_warnx("Opened %s", ic->ic_path);
812 nfsopen(struct idmap_client *ic)
814 if ((ic->ic_fd = open(ic->ic_path, O_RDWR, 0)) == -1) {
817 fcntl(ic->ic_dirfd, F_SETSIG, SIGUSR2);
818 fcntl(ic->ic_dirfd, F_NOTIFY,
819 DN_CREATE | DN_DELETE | DN_MULTISHOT);
822 idmapd_warn("nfsopen: open(%s)", ic->ic_path);
826 event_set(&ic->ic_event, ic->ic_fd, EV_READ, nfscb, ic);
827 event_add(&ic->ic_event, NULL);
828 fcntl(ic->ic_dirfd, F_SETSIG, 0);
829 fcntl(ic->ic_dirfd, F_NOTIFY, 0);
831 idmapd_warnx("Opened %s", ic->ic_path);
838 idtonameres(struct idmap_msg *im)
840 char domain[NFS4_MAX_DOMAIN_LEN];
843 ret = nfs4_get_default_domain(NULL, domain, sizeof(domain));
844 switch (im->im_type) {
845 case IDMAP_TYPE_USER:
846 ret = nfs4_uid_to_name(im->im_id, domain, im->im_name,
847 sizeof(im->im_name));
849 if (strlen(nobodyuser) < sizeof(im->im_name))
850 strcpy(im->im_name, nobodyuser);
852 strcpy(im->im_name, NFS4NOBODY_USER);
855 case IDMAP_TYPE_GROUP:
856 ret = nfs4_gid_to_name(im->im_id, domain, im->im_name,
857 sizeof(im->im_name));
859 if (strlen(nobodygroup) < sizeof(im->im_name))
860 strcpy(im->im_name, nobodygroup);
862 strcpy(im->im_name, NFS4NOBODY_GROUP);
867 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
869 im->im_status = IDMAP_STATUS_SUCCESS;
873 nametoidres(struct idmap_msg *im)
879 /* XXX: move nobody stuff to library calls
880 * (nfs4_get_nobody_user(domain), nfs4_get_nobody_group(domain)) */
882 im->im_status = IDMAP_STATUS_SUCCESS;
884 switch (im->im_type) {
885 case IDMAP_TYPE_USER:
886 ret = nfs4_name_to_uid(im->im_name, &uid);
887 im->im_id = (u_int32_t) uid;
889 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
890 im->im_id = nobodyuid;
893 case IDMAP_TYPE_GROUP:
894 ret = nfs4_name_to_gid(im->im_name, &gid);
895 im->im_id = (u_int32_t) gid;
897 im->im_status = IDMAP_STATUS_LOOKUPFAIL;
898 im->im_id = nobodygid;
905 validateascii(char *string, u_int32_t len)
909 for (i = 0; i < len; i++) {
910 if (string[i] == '\0')
913 if (string[i] & 0x80)
917 if (string[i] != '\0')
924 addfield(char **bpp, ssize_t *bsizp, char *fld)
927 ssize_t bsiz = *bsizp;
929 while ((ch = *fld++) != '\0' && bsiz > 0) {
936 bp += snprintf(bp, bsiz, "\\%03o", ch);
947 if (bsiz < 1 || ch != '\0')
960 getfield(char **bpp, char *fld, size_t fldsz)
965 while ((bp = strsep(bpp, " ")) != NULL && bp[0] == '\0')
968 if (bp == NULL || bp[0] == '\0' || bp[0] == '\n')
971 while (*bp != '\0' && fldsz > 1) {
973 if ((n = sscanf(bp, "\\%03o", &val)) != 1)
993 * mydaemon creates a pipe between the partent and child
994 * process. The parent process will wait until the
995 * child dies or writes a '1' on the pipe signaling
996 * that it started successfully.
998 int pipefds[2] = { -1, -1};
1001 mydaemon(int nochdir, int noclose)
1003 int pid, status, tempfd;
1005 if (pipe(pipefds) < 0)
1006 err(1, "mydaemon: pipe() failed: errno %d", errno);
1008 if ((pid = fork ()) < 0)
1009 err(1, "mydaemon: fork() failed: errno %d", errno);
1013 * Parent. Wait for status from child.
1016 if (read(pipefds[0], &status, 1) != 1)
1024 if (chdir ("/") == -1)
1025 err(1, "mydaemon: chdir() failed: errno %d", errno);
1028 while (pipefds[1] <= 2) {
1029 pipefds[1] = dup(pipefds[1]);
1031 err(1, "mydaemon: dup() failed: errno %d", errno);
1035 tempfd = open("/dev/null", O_RDWR);
1037 tempfd = open("/", O_RDONLY);
1050 release_parent(void)
1054 if (pipefds[1] > 0) {
1055 write(pipefds[1], &status, 1);