3 * $Id: pixie.c,v 1.3 1999/12/22 22:14:40 mdw Exp $
5 * Passphrase pixie for Catacomb
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 1999/12/22 22:14:40 mdw
34 * Only produce initialization message if verbose.
36 * Revision 1.2 1999/12/22 22:13:42 mdw
37 * Fix bug in passphrase flushing loop.
39 * Revision 1.1 1999/12/22 15:58:41 mdw
40 * Passphrase pixie support.
44 /*----- Header files ------------------------------------------------------*/
59 #include <sys/types.h>
66 #include <sys/ioctl.h>
70 #include <sys/socket.h>
73 #include <mLib/alloc.h>
74 #include <mLib/dstr.h>
75 #include <mLib/fdflags.h>
76 #include <mLib/mdwopt.h>
77 #include <mLib/quis.h>
78 #include <mLib/report.h>
80 #include <mLib/selbuf.h>
87 #include "passphrase.h"
90 /*----- Static variables --------------------------------------------------*/
92 static unsigned long timeout
= 300;
94 static unsigned verbose
= 1;
95 static const char *command
= 0;
97 static unsigned flags
= 0;
103 /*----- Event logging -----------------------------------------------------*/
107 * Arguments: @const char *p@ = @printf@-style format string
108 * @...@ = extra arguments to fill in
112 * Use: Writes out a timestamped log message.
115 static void log(const char *p
, ...)
120 if (!(flags
& F_SYSLOG
)) {
122 struct tm
*tm
= localtime(&t
);
124 d
.len
+= strftime(d
.buf
, d
.sz
, "%Y-%m-%d %H:%M:%S ", tm
);
127 dstr_vputf(&d
, p
, ap
);
130 if (flags
& F_SYSLOG
)
131 syslog(LOG_NOTICE
, "%s", d
.buf
);
134 dstr_write(&d
, stderr
);
139 /*----- Passphrase management ---------------------------------------------*/
141 /* --- Data structures --- */
143 typedef struct phrase
{
153 /* --- Variables --- */
155 #define P_ROOT ((phrase *)&p_root)
156 static struct { phrase
*next
; phrase
*prev
; } p_root
= { P_ROOT
, P_ROOT
};
158 /* --- @p_free@ --- *
160 * Arguments: @phrase *p@ = pointer to phrase block
164 * Use: Frees a phrase block.
167 static void p_free(phrase
*p
)
170 sel_rmtimer(&p
->timer
);
173 p
->next
->prev
= p
->prev
;
174 p
->prev
->next
= p
->next
;
178 /* --- @p_timer@ --- *
180 * Arguments: @struct timeval *tv@ = current time
181 * @void *p@ = pointer to phrase
185 * Use: Expires a passphrase.
188 static void p_timer(struct timeval
*tv
, void *p
)
192 log("expiring passphrase `%s'", pp
->tag
);
196 /* --- @p_alloc@ --- *
198 * Arguments: @size_t sz@ = amount of memory required
200 * Returns: Pointer to allocated memory, or null.
202 * Use: Allocates some locked memory, flushing old passphrases if
203 * there's not enough space.
206 static void *p_alloc(size_t sz
)
210 if ((p
= l_alloc(&lm
, sz
)) != 0)
212 if (P_ROOT
->next
== P_ROOT
)
215 log("flushing passphrase `%s' to free up needed space",
218 p_free(P_ROOT
->next
);
222 /* --- @p_find@ --- *
224 * Arguments: @const char *tag@ = pointer to tag to find
226 * Returns: Pointer to passphrase block, or null.
228 * Use: Finds a passphrase with a given tag.
231 static phrase
*p_find(const char *tag
)
235 for (p
= P_ROOT
->next
; p
!= P_ROOT
; p
= p
->next
) {
236 if (strcmp(p
->tag
, tag
) == 0) {
239 sel_rmtimer(&p
->timer
);
240 gettimeofday(&tv
, 0);
242 sel_addtimer(&sel
, &p
->timer
, &tv
, p_timer
, p
);
244 p
->next
->prev
= p
->prev
;
245 p
->prev
->next
= p
->next
;
247 p
->prev
= P_ROOT
->prev
;
248 P_ROOT
->prev
->next
= p
;
258 * Arguments: @const char *tag@ = pointer to tag string
259 * @const char *p@ = pointer to passphrase
260 * @unsigned long t@ = expiry timeout
262 * Returns: Pointer to newly-added passphrase.
264 * Use: Adds a new passphrase. The tag must not already exist.
267 static phrase
*p_add(const char *tag
, const char *p
, unsigned long t
)
269 size_t sz
= strlen(p
) + 1;
270 char *l
= p_alloc(sz
);
273 /* --- Make sure the locked memory was allocated --- */
278 /* --- Fill in some other bits of the block --- */
283 pp
->tag
= xstrdup(tag
);
286 /* --- Set the timer --- */
291 gettimeofday(&tv
, 0);
293 sel_addtimer(&sel
, &pp
->timer
, &tv
, p_timer
, pp
);
296 /* --- Link the block into the chain --- */
299 pp
->prev
= P_ROOT
->prev
;
300 P_ROOT
->prev
->next
= pp
;
305 /* --- @p_flush@ --- *
307 * Arguments: @const char *tag@ = pointer to tag string, or zero for all
311 * Use: Immediately flushes either a single phrase or all of them.
314 static void p_flush(const char *tag
)
318 if (!tag
&& verbose
> 1)
319 log("flushing all passphrases");
321 while (p
!= P_ROOT
) {
322 phrase
*pp
= p
->next
;
325 else if (strcmp(p
->tag
, tag
) == 0) {
327 log("flushing passphrase `%s'", tag
);
335 /*----- Reading passphrases -----------------------------------------------*/
337 /* --- @p_request@ --- *
339 * Arguments: @const char *msg@ = message string
340 * @const char *tag@ = pointer to tag string
341 * @char *buf@ = pointer to (locked) buffer
342 * @size_t sz@ = size of buffer
344 * Returns: Zero if all went well, nonzero otherwise.
346 * Use: Requests a passphrase from the user.
349 static int p_request(const char *msg
, const char *tag
, char *buf
, size_t sz
)
351 /* --- If there's a passphrase-fetching command, run it --- */
360 /* --- Substitute the prompt string into the command --- */
364 const char *q
= strchr(p
, '%');
387 /* --- Create a pipe and start a child process --- */
391 if ((kid
= fork()) < 0)
394 /* --- Child process --- */
398 if (dup2(fd
[1], STDOUT_FILENO
) < 0)
401 execl("/bin/sh", "sh", "-c", d
.buf
, (void *)0);
405 /* --- Read the data back into my buffer --- */
408 if ((r
= read(fd
[0], buf
, sz
- 1)) >= 0) {
409 char *q
= memchr(buf
, '\n', r
);
421 /* --- Tidy up when things go wrong --- */
432 /* --- Read a passphrase from the terminal --- *
434 * Use the standard Catacomb passphrase-reading function, so it'll read the
435 * passphrase from a file descriptor or something if the appropriate
436 * environment variable is set.
442 dstr_putf(&d
, "%s %s: ", msg
, tag
);
443 rc
= pixie_getpass(d
.buf
, buf
, sz
);
451 * Arguments: @const char *tag@ = pointer to tag string
452 * @unsigned mode@ = reading mode (verify?)
453 * @time_t exp@ = expiry time suggestion
455 * Returns: Pointer to passphrase, or zero.
457 * Use: Reads a passphrase from somewhere.
460 static const char *p_get(const char *tag
, unsigned mode
, time_t exp
)
467 /* --- Write a log message --- */
470 log("passphrase `%s' requested", tag
);
472 /* --- Try to find the phrase --- */
474 if ((p
= p_find(tag
)) == 0) {
475 if ((pp
= p_alloc(LBUFSZ
)) == 0)
477 if (p_request("Passphrase", tag
, pp
, LBUFSZ
) < 0)
479 p
= p_add(tag
, pp
, exp
);
484 /* --- If verification is requested, verify the passphrase --- */
486 if (mode
== PMODE_VERIFY
) {
487 if (!pp
&& (pp
= p_alloc(LBUFSZ
)) == 0)
489 if (p_request("Verify passphrase", tag
, pp
, LBUFSZ
) < 0)
491 if (strcmp(pp
, p
->p
) != 0) {
493 log("passphrases for `%s' don't match", tag
);
499 /* --- Tidy up and return the passphrase --- */
502 memset(pp
, 0, LBUFSZ
);
507 /* --- Tidy up if things went wrong --- */
511 memset(pp
, 0, LBUFSZ
);
519 /*----- Server command parsing --------------------------------------------*/
521 /* --- Data structures --- */
523 typedef struct pixserv
{
529 #define PIXSERV_TIMEOUT 300
531 /* --- @pixserv_expire@ --- *
533 * Arguments: @struct timeval *tv@ = pointer to current time
534 * @void *p@ = pointer to server block
538 * Use: Expires a pixie connection if the remote end decides he's not
539 * interested any more.
542 static void pixserv_expire(struct timeval
*tv
, void *p
)
545 if (px
->fd
!= px
->b
.reader
.fd
)
547 selbuf_disable(&px
->b
);
548 close(px
->b
.reader
.fd
);
552 /* --- @pixserv_write@ --- *
554 * Arguments: @pixserv *px@ = pointer to server block
555 * @const char *p@ = pointer to skeleton string
556 * @...@ = other arguments to fill in
560 * Use: Formats a string and emits it to the output file.
563 static void pixserv_write(pixserv
*px
, const char *p
, ...)
569 dstr_vputf(&d
, p
, ap
);
570 write(px
->fd
, d
.buf
, d
.len
);
575 /* --- @pixserv_timeout@ --- *
577 * Arguments: @const char *p@ = pointer to timeout string
579 * Returns: Timeout in seconds.
581 * Use: Translates a string to a timeout value in seconds.
584 static unsigned long pixserv_timeout(const char *p
)
592 t
= strtoul(p
, &q
, 0);
597 case 's': if (q
[1] != 0)
604 /* --- @pixserv_line@ --- *
606 * Arguments: @char *s@ = pointer to the line read
607 * @void *p@ = pointer to server block
611 * Use: Handles a line read from the client.
614 static void pixserv_line(char *s
, void *p
)
620 /* --- Handle an end-of-file --- */
622 sel_rmtimer(&px
->timer
);
624 if (px
->fd
!= px
->b
.reader
.fd
)
626 selbuf_disable(&px
->b
);
627 close(px
->b
.reader
.fd
);
631 /* --- Fiddle the timeout --- */
635 gettimeofday(&tv
, 0);
636 tv
.tv_sec
+= PIXSERV_TIMEOUT
;
637 sel_addtimer(&sel
, &px
->timer
, &tv
, pixserv_expire
, px
);
640 /* --- Scan out the first word --- */
642 if ((q
= str_getword(&s
)) == 0)
644 for (qq
= q
; *qq
; qq
++)
645 *qq
= tolower((unsigned char)*qq
);
647 /* --- Handle a help request --- */
649 if (strcmp(q
, "help") == 0) {
651 INFO Commands supported:\n\
654 INFO PASS tag [expire]\n\
655 INFO VERIFY tag [expire]\n\
662 /* --- List the passphrases --- */
664 else if (strcmp(q
, "list") == 0) {
667 for (p
= P_ROOT
->next
; p
!= P_ROOT
; p
= p
->next
) {
669 pixserv_write(px
, "ITEM %s no-expire\n", p
->tag
);
672 gettimeofday(&tv
, 0);
673 TV_SUB(&tv
, &p
->timer
.tv
, &tv
);
674 pixserv_write(px
, "ITEM %s %i\n", p
->tag
, tv
.tv_sec
);
677 pixserv_write(px
, "OK\n");
680 /* --- Request a passphrase --- */
682 else if ((mode
= PMODE_READ
, strcmp(q
, "pass") == 0) ||
683 (mode
= PMODE_VERIFY
, strcmp(q
, "verify") == 0)) {
687 if ((q
= str_getword(&s
)) == 0)
688 pixserv_write(px
, "FAIL missing tag\n");
689 else if ((t
= pixserv_timeout(s
)) == 0)
690 pixserv_write(px
, "FAIL bad timeout\n");
691 else if ((p
= p_get(q
, mode
, t
> timeout ? timeout
: t
)) == 0)
692 pixserv_write(px
, "FAIL error reading passphrase\n");
694 pixserv_write(px
, "OK %s\n", p
);
697 /* --- Flush existing passphrases --- */
699 else if (strcmp(q
, "flush") == 0) {
702 pixserv_write(px
, "OK\n");
705 /* --- Shut the server down --- */
707 else if (strcmp(q
, "quit") == 0) {
709 log("client requested shutdown");
710 pixserv_write(px
, "OK\n");
714 /* --- Report an error for other commands --- */
717 pixserv_write(px
, "FAIL unknown command `%s'\n", q
);
720 /* --- @pixserv_create@ --- *
722 * Arguments: @int fd@ = file descriptor to read from
723 * @int ofd@ = file descriptor to write to
727 * Use: Creates a new Pixie server instance for a new connection.
730 static void pixserv_create(int fd
, int ofd
)
732 pixserv
*px
= CREATE(pixserv
);
734 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
736 fdflags(ofd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
738 selbuf_init(&px
->b
, &sel
, fd
, pixserv_line
, px
);
739 gettimeofday(&tv
, 0);
740 tv
.tv_sec
+= PIXSERV_TIMEOUT
;
741 sel_addtimer(&sel
, &px
->timer
, &tv
, pixserv_expire
, px
);
744 /* --- @pixserv_accept@ --- *
746 * Arguments: @int fd@ = file descriptor
747 * @unsigned mode@ = what's happened
748 * @void *p@ = an uninteresting argument
752 * Use: Accepts a new connection.
755 static void pixserv_accept(int fd
, unsigned mode
, void *p
)
758 struct sockaddr_un sun
;
759 int sunsz
= sizeof(sun
);
761 if (mode
!= SEL_READ
)
763 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &sunsz
)) < 0) {
765 log("new connection failed: %s", strerror(errno
));
768 pixserv_create(nfd
, nfd
);
771 /*----- Setting up the server ---------------------------------------------*/
773 /* --- @unlinksocket@ --- *
779 * Use: Tidies up the socket when it's finished with.
782 static char *sockpath
;
784 static void unlinksocket(void)
790 /* --- @pix_sigdie@ --- *
792 * Arguments: @int sig@ = signal number
793 * @void *p@ = uninteresting argument
797 * Use: Shuts down the program after a fatal signal.
800 static void pix_sigdie(int sig
, void *p
)
807 case SIGTERM
: p
= "SIGTERM"; break;
808 case SIGINT
: p
= "SIGINT"; break;
810 sprintf(buf
, "signal %i", sig
);
814 log("shutting down on %s", p
);
819 /* --- @pix_sigflush@ --- *
821 * Arguments: @int sig@ = signal number
822 * @void *p@ = uninteresting argument
826 * Use: Flushes the passphrase cache on receipt of a signal.
829 static void pix_sigflush(int sig
, void *p
)
836 case SIGHUP
: p
= "SIGHUP"; break;
837 case SIGQUIT
: p
= "SIGQUIT"; break;
839 sprintf(buf
, "signal %i", sig
);
843 log("received %s; flushing passphrases", p
);
848 /* --- @pix_setup@ --- *
850 * Arguments: @struct sockaddr_un *sun@ = pointer to address to use
851 * @size_t sz@ = size of socket address
855 * Use: Sets up the pixie's Unix-domain socket.
858 static void pix_setup(struct sockaddr_un
*sun
, size_t sz
)
862 /* --- Set up the parent directory --- */
866 char *p
= sun
->sun_path
;
867 char *q
= strrchr(p
, '/');
876 if (stat(d
.buf
, &st
))
877 die(1, "couldn't stat `%s': %s", d
.buf
, strerror(errno
));
878 if (st
.st_mode
& 0077)
879 die(1, "parent directory `%s' has group or world access", d
.buf
);
883 /* --- Initialize the socket --- */
891 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
892 die(1, "couldn't create socket: %s", strerror(errno
));
893 if (bind(fd
, (struct sockaddr
*)sun
, sz
) < 0) {
895 if (errno
!= EADDRINUSE
)
896 die(1, "couldn't bind to address: %s", strerror(e
));
898 die(1, "too many retries; giving up");
900 if (connect(fd
, (struct sockaddr
*)sun
, sz
)) {
901 if (errno
!= ECONNREFUSED
)
902 die(1, "couldn't bind to address: %s", strerror(e
));
904 log("stale socket found; removing it");
905 unlink(sun
->sun_path
);
909 log("server already running; shutting it down");
910 write(fd
, "QUIT\n", 5);
916 chmod(sun
->sun_path
, 0600);
917 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
919 die(1, "couldn't listen on socket: %s", strerror(errno
));
922 /* --- Set up the rest of the server --- */
925 static sel_file serv
;
926 sockpath
= sun
->sun_path
;
927 atexit(unlinksocket
);
928 sel_initfile(&sel
, &serv
, fd
, SEL_READ
, pixserv_accept
, 0);
933 /*----- Client support code -----------------------------------------------*/
935 /* --- Variables --- */
937 static selbuf c_server
, c_client
;;
939 /* --- Line handler functions --- */
941 static void c_uline(char *s
, void *p
)
948 write(c_server
.reader
.fd
, s
, sz
);
951 static void c_sline(char *s
, void *p
)
955 moan("server closed the connection");
961 /* --- @pix_client@ --- *
963 * Arguments: @struct sockaddr_un *sun@ = pointer to socket address
964 * @size_t sz@ = size of socket address
965 * @char *argv[]@ = pointer to arguments to send
969 * Use: Performs client-side actions for the passphrase pixie.
972 static void pix_client(struct sockaddr_un
*sun
, size_t sz
, char *argv
[])
976 /* --- Open the socket --- */
978 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
979 die(1, "couldn't create socket: %s", strerror(errno
));
980 if (connect(fd
, (struct sockaddr
*)sun
, sz
))
981 die(1, "couldn't connect to server: %s", strerror(errno
));
982 selbuf_init(&c_server
, &sel
, fd
, c_sline
, 0);
984 /* --- If there are any arguments, turn them into a string --- */
987 selbuf_init(&c_client
, &sel
, STDIN_FILENO
, c_uline
, 0);
996 write(fd
, d
.buf
, d
.len
);
1001 /* --- And repeat --- */
1007 /*----- Main code ---------------------------------------------------------*/
1009 /* --- @help@, @version@, @usage@ --- *
1011 * Arguments: @FILE *fp@ = stream to write on
1015 * Use: Emit helpful messages.
1018 static void usage(FILE *fp
)
1022 $ [-qvidl] [-c command] [-t timeout] [-s socket]\n\
1023 $ [-s socket] -C [command args...]\n\
1027 static void version(FILE *fp
)
1029 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1032 static void help(FILE *fp
)
1038 The Catacomb passphrase pixie collects and caches passphrases used to\n\
1039 protect important keys. Options provided:\n\
1041 -h, --help Show this help text.\n\
1042 -V, --version Show the program's version number.\n\
1043 -u, --usage Show a (very) terse usage summary.\n\
1045 -C, --client Connect to a running pixie as a client.\n\
1047 -q, --quiet Emit fewer log messages.\n\
1048 -v, --version Emit more log messages.\n\
1049 -s, --socket=FILE Name the pixie's socket.\n\
1050 -c, --command=COMMAND Shell command to read a passphrase.\n\
1051 -t, --timeout=TIMEOUT Length of time to retain a passphrase in memory.\n\
1052 -i, --interactive Allow commands to be typed interactively.\n\
1053 -l, --syslog Emit log messages to the system log.\n\
1055 The COMMAND may contain `%m' and `%t' markers which are replaced by a\n\
1056 prompt message and the passphrase tag respectively. The TIMEOUT is an\n\
1057 integer, optionally followed by `d', `h', `m' or `s' to specify units of\n\
1058 days, hours, minutes or seconds respectively.\n\
1060 In client mode, if a command is specified on the command line, it is sent\n\
1061 to the running server; otherwise the program reads requests from stdin.\n\
1062 Responses from the pixie are written to stdout. Send a HELP request for\n\
1063 a quick summary of the pixie communication protocol.\n\
1069 * Arguments: @int argc@ = number of arguments
1070 * @char *argv[]@ = vector of argument values
1072 * Returns: Zero if OK.
1074 * Use: Main program. Listens on a socket and responds with a PGP
1075 * passphrase when asked.
1078 int main(int argc
, char *argv
[])
1081 struct sockaddr_un
*sun
;
1093 /* --- Initialize libraries --- */
1098 /* --- Set up the locked memory area --- */
1103 /* --- Parse command line arguments --- */
1106 static struct option opts
[] = {
1108 /* --- Standard GNUy help options --- */
1110 { "help", 0, 0, 'h' },
1111 { "version", 0, 0, 'V' },
1112 { "usage", 0, 0, 'u' },
1114 /* --- Other options --- */
1116 { "quiet", 0, 0, 'q' },
1117 { "verbose", 0, 0, 'v' },
1118 { "client", 0, 0, 'C' },
1119 { "socket", OPTF_ARGREQ
, 0, 's' },
1120 { "command", OPTF_ARGREQ
, 0, 'c' },
1121 { "timeout", OPTF_ARGREQ
, 0, 't' },
1122 { "interactive", 0, 0, 'i' },
1123 { "stdin", 0, 0, 'i' },
1124 { "daemon", 0, 0, 'd' },
1125 { "log", 0, 0, 'l' },
1126 { "syslog", 0, 0, 'l' },
1128 /* --- Magic terminator --- */
1133 int i
= mdwopt(argc
, argv
, "hVuqvCs:c:t:idl", opts
, 0, 0, 0);
1139 /* --- GNUy help options --- */
1151 /* --- Other interesting things --- */
1167 if ((timeout
= pixserv_timeout(optarg
)) == 0)
1168 die(1, "bad timeout `%s'", optarg
);
1183 /* --- Something else --- */
1191 if (f
& f_bogus
|| (optind
< argc
&& !(f
& f_client
))) {
1196 /* --- Set up the socket address --- */
1198 sun
= pixie_address(path
, &sz
);
1200 /* --- Initialize selectory --- */
1203 signal(SIGPIPE
, SIG_IGN
);
1205 /* --- Be a client if a client's wanted --- */
1208 pix_client(sun
, sz
, argv
+ optind
);
1210 /* --- Open the syslog if requested --- */
1214 openlog(QUIS
, 0, LOG_DAEMON
);
1217 /* --- Check on the locked memory area --- */
1221 int rc
= l_report(&lm
, &d
);
1223 die(EXIT_FAILURE
, d
.buf
);
1224 else if (rc
&& verbose
) {
1226 log("couldn't lock passphrase buffer");
1231 /* --- Set signal behaviours --- */
1234 static sig sigint
, sigterm
, sigquit
, sighup
;
1236 sig_add(&sigint
, SIGINT
, pix_sigdie
, 0);
1237 sig_add(&sigterm
, SIGTERM
, pix_sigdie
, 0);
1238 sig_add(&sigquit
, SIGQUIT
, pix_sigflush
, 0);
1239 sig_add(&sighup
, SIGHUP
, pix_sigflush
, 0);
1242 /* --- Set up the server --- */
1246 pixserv_create(STDIN_FILENO
, STDOUT_FILENO
);
1248 /* --- Fork into the background if requested --- */
1253 if (((f
& f_stdin
) &&
1254 (isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
))) ||
1256 die(1, "can't become a daemon if terminal required");
1258 if ((kid
= fork()) < 0)
1259 die(1, "fork failed: %s", strerror(errno
));
1265 if ((fd
= open("/dev/tty", O_RDONLY
)) >= 0) {
1266 ioctl(fd
, TIOCNOTTY
);
1278 log("initialized ok");
1284 /*----- That's all, folks -------------------------------------------------*/