source: unix.c @ 7448e1b

Last change on this file since 7448e1b was 60c1a4e, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-08-30T22:30:27Z

Changed the order in which things are loaded a little bit so
global.conf->plugindir is actually filled in before we call nogaim_init().

  • 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        log_init();
50        CONF_FILE = g_strdup( CONF_FILE_DEF );
51        global.conf = conf_load( argc, argv );
52        if( global.conf == NULL )
53                return( 1 );
54       
55        b_main_init();
56        nogaim_init();
57       
58        srand( time( NULL ) ^ getpid() );
59        global.helpfile = g_strdup( HELP_FILE );
60       
61        if( global.conf->runmode == RUNMODE_INETD )
62        {
63                i = bitlbee_inetd_init();
64                log_message( LOGLVL_INFO, "Bitlbee %s starting in inetd mode.", BITLBEE_VERSION );
65
66        }
67        else if( global.conf->runmode == RUNMODE_DAEMON )
68        {
69                i = bitlbee_daemon_init();
70                log_message( LOGLVL_INFO, "Bitlbee %s starting in daemon mode.", BITLBEE_VERSION );
71        }
72        else if( global.conf->runmode == RUNMODE_FORKDAEMON )
73        {
74                /* In case the operator requests a restart, we need this. */
75                old_cwd = g_malloc( 256 );
76                if( getcwd( old_cwd, 255 ) == NULL )
77                {
78                        log_message( LOGLVL_WARNING, "Could not save current directory: %s", strerror( errno ) );
79                        g_free( old_cwd );
80                        old_cwd = NULL;
81                }
82               
83                i = bitlbee_daemon_init();
84                log_message( LOGLVL_INFO, "Bitlbee %s starting in forking daemon mode.", BITLBEE_VERSION );
85        }
86        if( i != 0 )
87                return( i );
88       
89        global.storage = storage_init( global.conf->primary_storage, global.conf->migrate_storage );
90        if ( global.storage == NULL) {
91                log_message( LOGLVL_ERROR, "Unable to load storage backend '%s'", global.conf->primary_storage );
92                return( 1 );
93        }
94       
95       
96        /* Catch some signals to tell the user what's happening before quitting */
97        memset( &sig, 0, sizeof( sig ) );
98        sig.sa_handler = sighandler;
99        sigaction( SIGCHLD, &sig, &old );
100        sigaction( SIGPIPE, &sig, &old );
101        sig.sa_flags = SA_RESETHAND;
102        sigaction( SIGINT,  &sig, &old );
103        sigaction( SIGILL,  &sig, &old );
104        sigaction( SIGBUS,  &sig, &old );
105        sigaction( SIGFPE,  &sig, &old );
106        sigaction( SIGSEGV, &sig, &old );
107        sigaction( SIGTERM, &sig, &old );
108        sigaction( SIGQUIT, &sig, &old );
109        sigaction( SIGXCPU, &sig, &old );
110       
111        if( !getuid() || !geteuid() )
112                log_message( LOGLVL_WARNING, "BitlBee is running with root privileges. Why?" );
113        if( help_init( &(global.help), global.helpfile ) == NULL )
114                log_message( LOGLVL_WARNING, "Error opening helpfile %s.", HELP_FILE );
115       
116        b_main_run();
117       
118        if( global.restart )
119        {
120                char *fn = ipc_master_save_state();
121                char **args;
122                int n, i;
123               
124                chdir( old_cwd );
125               
126                n = 0;
127                args = g_new0( char *, argc + 3 );
128                args[n++] = argv[0];
129                if( fn )
130                {
131                        args[n++] = "-R";
132                        args[n++] = fn;
133                }
134                for( i = 1; argv[i] && i < argc; i ++ )
135                {
136                        if( strcmp( argv[i], "-R" ) == 0 )
137                                i += 2;
138                       
139                        args[n++] = argv[i];
140                }
141               
142                close( global.listen_socket );
143               
144                execve( args[0], args, envp );
145        }
146       
147        return( 0 );
148}
149
150static void sighandler( int signal )
151{
152        /* FIXME: Calling log_message() here is not a very good idea! */
153       
154        if( signal == SIGTERM )
155        {
156                static int first = 1;
157               
158                if( first )
159                {
160                        /* We don't know what we were doing when this signal came in. It's not safe to touch
161                           the user data now (not to mention writing them to disk), so add a timer. */
162                       
163                        log_message( LOGLVL_ERROR, "SIGTERM received, cleaning up process." );
164                        b_timeout_add( 1, (b_event_handler) bitlbee_shutdown, NULL );
165                       
166                        first = 0;
167                }
168                else
169                {
170                        /* Well, actually, for now we'll never need this part because this signal handler
171                           will never be called more than once in a session for a non-SIGPIPE signal...
172                           But just in case we decide to change that: */
173                       
174                        log_message( LOGLVL_ERROR, "SIGTERM received twice, so long for a clean shutdown." );
175                        raise( signal );
176                }
177        }
178        else if( signal == SIGCHLD )
179        {
180                pid_t pid;
181                int st;
182               
183                while( ( pid = waitpid( 0, &st, WNOHANG ) ) > 0 )
184                {
185                        if( WIFSIGNALED( st ) )
186                                log_message( LOGLVL_INFO, "Client %d terminated normally. (status = %d)", pid, WEXITSTATUS( st ) );
187                        else if( WIFEXITED( st ) )
188                                log_message( LOGLVL_INFO, "Client %d killed by signal %d.", pid, WTERMSIG( st ) );
189                }
190        }
191        else if( signal != SIGPIPE )
192        {
193                log_message( LOGLVL_ERROR, "Fatal signal received: %d. That's probably a bug.", signal );
194                raise( signal );
195        }
196}
197
198double gettime()
199{
200        struct timeval time[1];
201
202        gettimeofday( time, 0 );
203        return( (double) time->tv_sec + (double) time->tv_usec / 1000000 );
204}
Note: See TracBrowser for help on using the repository browser.