MFC r1.6 r1.30 r1.28 (HEAD):
[dragonfly.git] / usr.sbin / i4b / isdnd / process.c
blob5266dedefa55c26329183a1d947c289eaae6b176
1 /*
2 * Copyright (c) 1997, 1999 Hellmuth Michaelis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
25 *---------------------------------------------------------------------------
27 * i4b daemon - process handling routines
28 * --------------------------------------
30 * $Id: process.c,v 1.8 1999/12/13 21:25:25 hm Exp $
32 * $FreeBSD: src/usr.sbin/i4b/isdnd/process.c,v 1.6.2.1 2001/08/01 17:45:03 obrien Exp $
33 * $DragonFly: src/usr.sbin/i4b/isdnd/process.c,v 1.2 2003/06/17 04:29:54 dillon Exp $
35 * last edit-date: [Mon Dec 13 21:48:19 1999]
37 *---------------------------------------------------------------------------*/
39 #include "isdnd.h"
41 /*---------------------------------------------------------------------------*
42 * check if another instance of us is already running
43 *---------------------------------------------------------------------------*/
44 void
45 check_pid(void)
47 FILE *fp;
49 /* check if another lock-file already exists */
51 if((fp = fopen(PIDFILE, "r")) != NULL)
53 /* lockfile found, check */
55 int oldpid;
57 /* read pid from file */
59 if((fscanf(fp, "%d", &oldpid)) != 1)
61 log(LL_ERR, "ERROR, reading pid from lockfile failed, terminating!");
62 exit(1);
65 /* check if process got from file is still alive */
67 if((kill(oldpid, 0)) != 0)
69 /* process does not exist */
71 /* close file */
73 fclose(fp);
75 DBGL(DL_PROC, (log(LL_DBG, "removing old lock-file %s", PIDFILE)));
77 /* remove file */
79 unlink(PIDFILE);
81 else
83 /* process is still alive */
85 log(LL_ERR, "ERROR, another daemon is already running, pid = %d, terminating!", oldpid);
86 exit(1);
91 /*---------------------------------------------------------------------------*
92 * establish and init process lock file
93 *---------------------------------------------------------------------------*/
94 void
95 write_pid(void)
97 FILE *fp;
99 /* write my pid into lock-file */
101 if((fp = fopen(PIDFILE, "w")) == NULL)
103 log(LL_ERR, "ERROR, can't open lockfile for writing, terminating");
104 do_exit(1);
107 if((fprintf(fp, "%d", (int)getpid())) == EOF)
109 log(LL_ERR, "ERROR, can't write pid to lockfile, terminating");
110 do_exit(1);
113 fsync(fileno(fp));
115 fclose(fp);
118 /*---------------------------------------------------------------------------*
119 * become a daemon
120 *---------------------------------------------------------------------------*/
121 void
122 daemonize(void)
124 int fd;
126 switch (fork())
128 case -1: /* error */
129 log(LL_ERR, "ERROR, daemonize/fork: %s", strerror(errno));
130 exit(1);
131 case 0: /* child */
132 break;
133 default: /* parent */
134 exit(0);
137 /* new session / no control tty */
139 if(setsid() == -1)
141 log(LL_ERR, "ERROR, setsid returns: %s", strerror(errno));
142 exit(1);
145 /* go away from mounted dir */
147 chdir("/");
149 /* move i/o to another device ? */
151 if(do_fullscreen && do_rdev)
153 char *tp;
155 if((fd = open(rdev, O_RDWR, 0)) != -1)
157 if(!isatty(fd))
159 log(LL_ERR, "ERROR, device %s is not a tty!", rdev);
160 exit(1);
162 if((dup2(fd, STDIN_FILENO)) == -1)
164 log(LL_ERR, "ERROR, dup2 stdin: %s", strerror(errno));
165 exit(1);
167 if((dup2(fd, STDOUT_FILENO)) == -1)
169 log(LL_ERR, "ERROR, dup2 stdout: %s", strerror(errno));
170 exit(1);
172 if((dup2(fd, STDERR_FILENO)) == -1)
174 log(LL_ERR, "ERROR, dup2 stderr: %s", strerror(errno));
175 exit(1);
178 else
180 log(LL_ERR, "ERROR, cannot open redirected device: %s", strerror(errno));
181 exit(1);
184 if(fd > 2)
186 if((close(fd)) == -1)
188 log(LL_ERR, "ERROR, close in daemonize: %s", strerror(errno));
189 exit(1);
193 /* curses output && fork NEEDS controlling tty */
195 if((ioctl(STDIN_FILENO, TIOCSCTTY, (char *)NULL)) < 0)
197 log(LL_ERR, "ERROR, cannot setup tty as controlling terminal: %s", strerror(errno));
198 exit(1);
201 /* in case there is no environment ... */
203 if(((tp = getenv("TERM")) == NULL) || (*tp == '\0'))
205 if(do_ttytype == 0)
207 log(LL_ERR, "ERROR, no environment variable TERM found and -t not specified!");
208 exit(1);
211 if((setenv("TERM", ttype, 1)) != 0)
213 log(LL_ERR, "ERROR, setenv TERM=%s failed: %s", ttype, strerror(errno));
214 exit(1);
220 /* EOF */