source: unix.c @ 4eb4c0f

Last change on this file since 4eb4c0f was 4eb4c0f, checked in by Sven Moritz Hallberg <sm@…>, at 2008-02-16T17:15:31Z

merge in upstream changes

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