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
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
25 *---------------------------------------------------------------------------
27 * i4b daemon - process handling routines
28 * --------------------------------------
32 * last edit-date: [Sun Feb 14 10:11:54 1999]
34 * -hm debugging processhandling
36 *---------------------------------------------------------------------------*/
40 /*---------------------------------------------------------------------------*
41 * check if another instance of us is already running
42 *---------------------------------------------------------------------------*/
48 /* check if another lock-file already exists */
50 if((fp = fopen(PIDFILE, "r")) != NULL)
52 /* lockfile found, check */
56 /* read pid from file */
58 if((fscanf(fp, "%d", &oldpid)) != 1)
60 log(LL_ERR, "ERROR, reading pid from lockfile failed, terminating!");
64 /* check if process got from file is still alive */
66 if((kill(oldpid, 0)) != 0)
68 /* process does not exist */
74 DBGL(DL_PROC, (log(LL_DBG, "removing old lock-file %s", PIDFILE)));
82 /* process is still alive */
84 log(LL_ERR, "ERROR, another daemon is already running, pid = %d, terminating!", oldpid);
90 /*---------------------------------------------------------------------------*
91 * establish and init process lock file
92 *---------------------------------------------------------------------------*/
98 /* write my pid into lock-file */
100 if((fp = fopen(PIDFILE, "w")) == NULL)
102 log(LL_ERR, "ERROR, can't open lockfile for writing, terminating");
106 if((fprintf(fp, "%d", (int)getpid())) == EOF)
108 log(LL_ERR, "ERROR, can't write pid to lockfile, terminating");
117 /*---------------------------------------------------------------------------*
119 *---------------------------------------------------------------------------*/
128 log(LL_ERR, "ERROR, daemonize/fork: %s", strerror(errno));
132 default: /* parent */
136 /* new session / no control tty */
140 log(LL_ERR, "ERROR, setsid returns: %s", strerror(errno));
144 /* go away from mounted dir */
148 /* move i/o to another device ? */
150 if(do_fullscreen && do_rdev)
154 if((fd = open(rdev, O_RDWR, 0)) != -1)
158 log(LL_ERR, "ERROR, device %s is not a tty!", rdev);
161 if((dup2(fd, STDIN_FILENO)) == -1)
163 log(LL_ERR, "ERROR, dup2 stdin: %s", strerror(errno));
166 if((dup2(fd, STDOUT_FILENO)) == -1)
168 log(LL_ERR, "ERROR, dup2 stdout: %s", strerror(errno));
171 if((dup2(fd, STDERR_FILENO)) == -1)
173 log(LL_ERR, "ERROR, dup2 stderr: %s", strerror(errno));
179 log(LL_ERR, "ERROR, cannot open redirected device: %s", strerror(errno));
185 if((close(fd)) == -1)
187 log(LL_ERR, "ERROR, close in daemonize: %s", strerror(errno));
192 /* curses output && fork NEEDS controlling tty */
194 if((ioctl(STDIN_FILENO, TIOCSCTTY, (char *)NULL)) < 0)
196 log(LL_ERR, "ERROR, cannot setup tty as controlling terminal: %s", strerror(errno));
200 /* in case there is no environment ... */
202 if(((tp = getenv("TERM")) == NULL) || (*tp == '\0'))
206 log(LL_ERR, "ERROR, no environment variable TERM found and -t not specified!");
210 if((setenv("TERM", ttype, 1)) != 0)
212 log(LL_ERR, "ERROR, setenv TERM=%s failed: %s", ttype, strerror(errno));