source: unix.c @ 5674207

Last change on this file since 5674207 was 5674207, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-11-28T19:51:39Z

Reshuffled initialization sequence a little bit. Most important change:
nogaim_init() should be done after fork() to make ForkDaemon mode work
again. Also, doing help_init() earlie makes "help purple" work.

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