Changeset 2909


Ignore:
Timestamp:
Aug 23, 2007, 5:08:22 AM (15 years ago)
Author:
joshe
Message:

Add command-line option to the daemon for using a pidfile.
Properly shut down the daemon upon receipt of a fatal signal.

Location:
branches/0.8x/daemon
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/0.8x/daemon/daemon.c

    r2415 r2909  
    5151
    5252static void usage       ( const char *, ... );
    53 static void readargs    ( int, char **, int *, int *, char ** );
     53static void readargs    ( int, char **, int *, int *, char **, char ** );
    5454static int  trylocksock ( const char * );
    5555static int  getlock     ( const char * );
     
    5858static void setupsigs   ( struct event_base * );
    5959static void gotsig      ( int, short, void * );
     60static int  savepid     ( const char * );
    6061
    6162static int  gl_lockfd               = -1;
     
    6364static int  gl_sockfd               = -1;
    6465static char gl_sockpath[MAXPATHLEN] = "";
     66static char gl_pidfile[MAXPATHLEN]  = "";
    6567
    6668int
     
    6971    struct event_base * evbase;
    7072    int                 nofork, debug, sockfd;
    71     char              * sockpath;
     73    char              * sockpath, * pidfile;
    7274
    7375    setmyname( argv[0] );
    74     readargs( argc, argv, &nofork, &debug, &sockpath );
     76    readargs( argc, argv, &nofork, &debug, &sockpath, &pidfile );
    7577
    7678    if( !nofork )
     
    8789    sockfd = trylocksock( sockpath );
    8890    if( 0 > sockfd )
     91    {
     92        exit( 1 );
     93    }
     94    if( NULL != pidfile && 0 > savepid( pidfile ) )
    8995    {
    9096        exit( 1 );
     
    119125
    120126    printf(
    121   "usage: %s [-dfh]\n"
     127  "usage: %s [-dfh] [-p file] [-s file]\n"
    122128  "\n"
    123129  "Transmission %s http://transmission.m0k.org/\n"
     
    127133  "  -f --foreground           Run in the foreground and log to stderr\n"
    128134  "  -h --help                 Display this message and exit\n"
     135  "  -p --pidfile <path>       Save the process id in a file at <path>\n"
    129136  "  -s --socket <path>        Place the socket file at <path>\n"
    130137  "\n"
     
    135142
    136143void
    137 readargs( int argc, char ** argv, int * nofork, int * debug, char ** sock )
    138 {
    139     char optstr[] = "dfhs:";
     144readargs( int argc, char ** argv, int * nofork, int * debug, char ** sock,
     145          char ** pidfile )
     146{
     147    char optstr[] = "dfhp:s:";
    140148    struct option longopts[] =
    141149    {
     
    143151        { "foreground",         no_argument,       NULL, 'f' },
    144152        { "help",               no_argument,       NULL, 'h' },
     153        { "pidfile",            required_argument, NULL, 'p' },
    145154        { "socket",             required_argument, NULL, 's' },
    146155        { NULL, 0, NULL, 0 }
     
    148157    int opt;
    149158
    150     *nofork = 0;
    151     *debug  = 0;
    152     *sock   = NULL;
     159    *nofork    = 0;
     160    *debug     = 0;
     161    *sock      = NULL;
     162    *pidfile   = NULL;
    153163
    154164    while( 0 <= ( opt = getopt_long( argc, argv, optstr, longopts, NULL ) ) )
     
    161171            case 'f':
    162172                *nofork = 1;
     173                break;
     174            case 'p':
     175                *pidfile = optarg;
    163176                break;
    164177            case 's':
     
    290303        close( gl_sockfd );
    291304    }
     305    if( 0 != gl_pidfile[0] )
     306    {
     307        unlink( gl_pidfile );
     308    }
    292309    if( 0 <= gl_lockfd )
    293310    {
     
    329346        exiting = 1;
    330347        errmsg( "received fatal signal %i, attempting to exit cleanly", sig );
    331         torrent_exit( 0 );
     348        server_quit();
    332349    }
    333350    else
     
    339356    }
    340357}
     358
     359int
     360savepid( const char * file )
     361{
     362    FILE * pid;
     363
     364    pid = fopen( file, "wb" );
     365    if( NULL == pid )
     366    {
     367        errnomsg( "failed to open pid file: %s", file );
     368        return -1;
     369    }
     370
     371    if( 0 > fprintf( pid, "%d\n", (int) getpid() ) )
     372    {
     373        errnomsg( "failed to write pid to file: %s", file );
     374        fclose( pid );
     375        unlink( file );
     376        return -1;
     377    }
     378
     379    fclose( pid );
     380    strlcpy( gl_pidfile, file, sizeof gl_pidfile );
     381
     382    return 0;
     383}
  • branches/0.8x/daemon/server.c

    r2415 r2909  
    187187
    188188void
     189server_quit( void )
     190{
     191    struct client * ii, * next;
     192
     193    torrent_exit( 0 );
     194    gl_exiting = 1;
     195
     196    for( ii = RB_MIN( allclients, &gl_clients ); NULL != ii; ii = next )
     197    {
     198        next = RB_NEXT( allclients, &gl_clients, ii );
     199        byebye( ii->ev, EVBUFFER_EOF, NULL );
     200    }
     201}
     202
     203void
    189204newclient( int fd, short event UNUSED, void * arg )
    190205{
     
    561576         void * arg UNUSED )
    562577{
    563     struct client * ii, * next;
    564 
    565     torrent_exit( 0 );
    566     gl_exiting = 1;
    567 
    568     for( ii = RB_MIN( allclients, &gl_clients ); NULL != ii; ii = next )
    569     {
    570         next = RB_NEXT( allclients, &gl_clients, ii );
    571         byebye( ii->ev, EVBUFFER_EOF, NULL );
    572     }
     578    server_quit();
    573579}
    574580
  • branches/0.8x/daemon/server.h

    r1806 r2909  
    3131void server_debug( int );
    3232int  server_listen( int );
     33void server_quit( void );
    3334
    3435#endif /* TR_DAEMON_SERVER_H */
  • branches/0.8x/daemon/transmission-daemon.1

    r1924 r2909  
    3333.Nm
    3434.Op Fl df
     35.Op Fl p Ar pid-file
    3536.Op Fl s Ar socket-file
    3637.Ek
     
    5051.It Fl h Fl -help
    5152Print command-line option descriptions.
     53.It Fl p Fl -pidfile Ar pid-file
     54Save the daemon's process ID in
     55.Ar pid-file .
     56.It Fl s Fl -socket Ar socket-file
     57Create the unix-domain socket file at
     58.Ar socket-file
     59instead of the platform-dependent default location.
    5260.El
    5361.Sh FILES
Note: See TracChangeset for help on using the changeset viewer.