source: unix.c @ a312b6b

Last change on this file since a312b6b was ba9edaa, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-05-10T17:34:46Z

Moved everything to the BitlBee event handling API.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Main file (Unix specific part)                                       */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#include "bitlbee.h"
27#include "commands.h"
28#include "crypting.h"
29#include "protocols/nogaim.h"
30#include "help.h"
31#include "ipc.h"
32#include <signal.h>
33#include <unistd.h>
34#include <sys/time.h>
35#include <sys/wait.h>
36
37global_t global;        /* Against global namespace pollution */
38
39static void sighandler( int signal );
40
41int main( int argc, char *argv[], char **envp )
42{
43        int i = 0;
44        char *old_cwd = NULL;
45        struct sigaction sig, old;
46       
47        memset( &global, 0, sizeof( global_t ) );
48       
49        b_main_init();
50       
51        log_init();
52
53        nogaim_init();
54
55        CONF_FILE = g_strdup( CONF_FILE_DEF );
56       
57        global.helpfile = g_strdup( HELP_FILE );
58
59        global.conf = conf_load( argc, argv );
60        if( global.conf == NULL )
61                return( 1 );
62
63
64        if( global.conf->runmode == RUNMODE_INETD )
65        {
66                i = bitlbee_inetd_init();
67                log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION );
68
69        }
70        else if( global.conf->runmode == RUNMODE_DAEMON )
71        {
72                i = bitlbee_daemon_init();
73                log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION );
74        }
75        else if( global.conf->runmode == RUNMODE_FORKDAEMON )
76        {
77                /* In case the operator requests a restart, we need this. */
78                old_cwd = g_malloc( 256 );
79                if( getcwd( old_cwd, 255 ) == NULL )
80                {
81                        log_message( LOGLVL_WARNING, "Could not save current directory: %s", strerror( errno ) );
82                        g_free( old_cwd );
83                        old_cwd = NULL;
84                }
85               
86                i = bitlbee_daemon_init();
87                log_message( LOGLVL_INFO, "Bitlbee %s starting in forking daemon mode.", BITLBEE_VERSION );
88        }
89        if( i != 0 )
90                return( i );
91
92        global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage );
93        if ( global.storage == NULL) {
94                log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage );
95                return( 1 );
96        }
97       
98       
99        /* Catch some signals to tell the user what's happening before quitting */
100        memset( &sig, 0, sizeof( sig ) );
101        sig.sa_handler = sighandler;
102        sigaction( SIGCHLD, &sig, &old );
103        sigaction( SIGPIPE, &sig, &old );
104        sig.sa_flags = SA_RESETHAND;
105        sigaction( SIGINT,  &sig, &old );
106        sigaction( SIGILL,  &sig, &old );
107        sigaction( SIGBUS,  &sig, &old );
108        sigaction( SIGFPE,  &sig, &old );
109        sigaction( SIGSEGV, &sig, &old );
110        sigaction( SIGTERM, &sig, &old );
111        sigaction( SIGQUIT, &sig, &old );
112        sigaction( SIGXCPU, &sig, &old );
113       
114        if( !getuid() || !geteuid() )
115                log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" );
116        if( help_init( &(global.help) ) == NULL )
117                log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE );
118       
119        b_main_run();
120       
121        if( global.restart )
122        {
123                char *fn = ipc_master_save_state();
124                char **args;
125                int n, i;
126               
127                chdir( old_cwd );
128               
129                n = 0;
130                args = g_new0( char *, argc + 3 );
131                args[n++] = argv[0];
132                if( fn )
133                {
134                        args[n++] = "-R";
135                        args[n++] = fn;
136                }
137                for( i = 1; argv[i] && i < argc; i ++ )
138                {
139                        if( strcmp( argv[i], "-R" ) == 0 )
140                                i += 2;
141                       
142                        args[n++] = argv[i];
143                }
144               
145                close( global.listen_socket );
146               
147                execve( args[0], args, envp );
148        }
149       
150        return( 0 );
151}
152
153static void sighandler( int signal )
154{
155        /* FIXME: Calling log_message() here is not a very good idea! */
156       
157        if( signal == SIGTERM )
158        {
159                static int first = 1;
160               
161                if( first )
162                {
163                        /* We don't know what we were doing when this signal came in. It's not safe to touch
164                           the user data now (not to mention writing them to disk), so add a timer. */
165                       
166                        log_message( LOGLVL_ERROR, "SIGTERM received, cleaning up process." );
167                        b_timeout_add( 1, (b_event_handler) bitlbee_shutdown, NULL );
168                       
169                        first = 0;
170                }
171                else
172                {
173                        /* Well, actually, for now we'll never need this part because this signal handler
174                           will never be called more than once in a session for a non-SIGPIPE signal...
175                           But just in case we decide to change that: */
176                       
177                        log_message( LOGLVL_ERROR, "SIGTERM received twice, so long for a clean shutdown." );
178                        raise( signal );
179                }
180        }
181        else if( signal == SIGCHLD )
182        {
183                pid_t pid;
184                int st;
185               
186                while( ( pid = waitpid( 0, &st, WNOHANG ) ) > 0 )
187                {
188                        if( WIFSIGNALED( st ) )
189                                log_message( LOGLVL_INFO, "Client %d terminated normally. (status = %d)", pid, WEXITSTATUS( st ) );
190                        else if( WIFEXITED( st ) )
191                                log_message( LOGLVL_INFO, "Client %d killed by signal %d.", pid, WTERMSIG( st ) );
192                }
193        }
194        else if( signal != SIGPIPE )
195        {
196                log_message( LOGLVL_ERROR, "Fatal signal received: %d. That's probably a bug.", signal );
197                raise( signal );
198        }
199}
200
201double gettime()
202{
203        struct timeval time[1];
204
205        gettimeofday( time, 0 );
206        return( (double) time->tv_sec + (double) time->tv_usec / 1000000 );
207}
Note: See TracBrowser for help on using the repository browser.