/[cvs]/eggdrop1.9/src/bg.c
ViewVC logotype

Contents of /eggdrop1.9/src/bg.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.22 - (show annotations) (download) (as text)
Sun Oct 17 05:14:07 2004 UTC (14 years, 8 months ago) by stdarg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.21: +3 -9 lines
File MIME type: text/x-chdr
* Added a few includs to common.h
* Removed duplicate includes from a bunch of files
* Added tcl date parsing a la "clock scan"
* Fixed some issues with configure (maybe)

1 /* bg.c: handles moving the process to the background and forking, while
2 * keeping threads happy.
3 *
4 * Copyright (C) 1997 Robey Pointer
5 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Eggheads Development Team
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 #ifndef lint
23 static const char rcsid[] = "$Id: bg.c,v 1.21 2004/09/26 09:42:09 stdarg Exp $";
24 #endif
25
26 #include <eggdrop/eggdrop.h>
27
28 #ifdef CYGWIN_HACKS
29 # include <windows.h>
30 #endif
31
32 #include <unistd.h> /* fork(), setpgid() */
33 #include <sys/wait.h> /* waitpid() */
34 #include <signal.h> /* kill() */
35 #include "main.h" /* fatal()*/
36 #include "bg.h"
37
38 /* When threads are started during eggdrop's init phase, we can't simply
39 * fork() later on, because that only copies the VM space over and
40 * doesn't actually duplicate the threads.
41 *
42 * To work around this, we fork() very early and let the parent process
43 * wait in an event loop. As soon as the init phase is completed and we
44 * would normally move to the background, the child process simply
45 * signals its parent that it may now quit. This allows us to control
46 * the terminal long enough to, e.g. properly feed error messages to
47 * cron scripts and let the user abort the loading process by hitting
48 * CTRL+C.
49 *
50 */
51
52 /* The child has to keep the parent's pid so it can send it a signal when it's
53 * time to exit. */
54 static int pipefd[2];
55
56 void bg_begin_split()
57 {
58 int parent_pid = -1, child_pid = -1;
59 int result;
60 char temp = 0;
61
62 parent_pid = getpid();
63
64 pipe(pipefd);
65
66 child_pid = fork();
67 if (child_pid == -1) fatal("CANNOT FORK PROCESS.");
68
69 /* Are we the child? */
70 if (child_pid == 0) {
71 /* Yes. Continue as normal. */
72 return;
73 }
74 /* We are the parent. Just hang around until the child is done. When
75 * the child wants us to exit, it will send us the signal and trigger
76 * wait_for_child. */
77 close(pipefd[1]);
78 result = read(pipefd[0], &temp, 1);
79
80 if (result <= 0) {
81 printf("Eggdrop exited abnormally!\n");
82 exit(1);
83 }
84 else {
85 printf("Eggdrop launched successfully into the background, pid = %d.\n", child_pid);
86 exit(0);
87 }
88 }
89
90 void bg_finish_split()
91 {
92 char temp = 0;
93 write(pipefd[1], &temp, 1);
94 close(pipefd[1]);
95 close(pipefd[0]);
96
97 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
98 setpgid(0, 0);
99 #endif
100 freopen("/dev/null", "r", stdin);
101 freopen("/dev/null", "w", stdout);
102 freopen("/dev/null", "w", stderr);
103
104 #ifdef CYGWIN_HACKS
105 FreeConsole();
106 #endif
107 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23