Changeset 5167 for trunk/daemon/daemon.c


Ignore:
Timestamp:
Feb 28, 2008, 7:06:23 PM (14 years ago)
Author:
charles
Message:

add win32/posix wrapper function tr_lockfile() to libT and use it in daemon/gtk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/daemon.c

    r4390 r5167  
    3333#include <errno.h>
    3434#include <event.h>
    35 #include <fcntl.h>
    3635#include <getopt.h>
    3736#include <signal.h>
     
    4342
    4443#include <libtransmission/trcompat.h>
     44#include <libtransmission/platform.h>
    4545#include <libtransmission/version.h>
    4646
     
    5353static void readargs    ( int, char **, int *, int *, char **, char ** );
    5454static int  trylocksock ( const char * );
    55 static int  getlock     ( const char * );
    5655static int  getsock     ( const char * );
    5756static void exitcleanup ( void );
     
    6059static int  savepid     ( const char * );
    6160
    62 static int  gl_lockfd               = -1;
    6361static char gl_lockpath[MAXPATHLEN] = "";
    6462static int  gl_sockfd               = -1;
     
    184182}
    185183
     184static int
     185getlock( const char * filename )
     186{
     187    const int state = tr_lockfile( filename );
     188    const int success = state == TR_LOCKFILE_SUCCESS;
     189
     190    if( !success ) switch( state ) {
     191        case TR_LOCKFILE_EOPEN:
     192            errnomsg( "failed to open file: %s", filename );
     193            break;
     194        case TR_LOCKFILE_ELOCK:
     195            errmsg( "another copy of %s is already running", getmyname() );
     196            break;
     197        default:
     198            errmsg( "unhandled tr_lockfile error: %d", state );
     199            break;
     200    }
     201
     202    return success;
     203}
     204
     205
    186206int
    187207trylocksock( const char * sockpath )
     
    198218
    199219    confpath( path, sizeof path, CONF_FILE_LOCK, 0 );
    200     fd = getlock( path );
    201     if( 0 > fd )
    202     {
    203         return -1;
    204     }
    205     gl_lockfd = fd;
     220    if( !getlock( path ) )
     221        return -1;
    206222    strlcpy( gl_lockpath, path, sizeof gl_lockpath );
    207223
     
    218234    gl_sockfd = fd;
    219235    strlcpy( gl_sockpath, sockpath, sizeof gl_sockpath );
    220 
    221     return fd;
    222 }
    223 
    224 int
    225 getlock( const char * path )
    226 {
    227     struct flock lk;
    228     int          fd;
    229     char         pid[64];
    230 
    231     fd = open( path, O_RDWR | O_CREAT, 0666 );
    232     if( 0 > fd )
    233     {
    234         errnomsg( "failed to open file: %s", path );
    235         return -1;
    236     }
    237 
    238     memset( &lk, 0, sizeof lk );
    239     lk.l_start  = 0;
    240     lk.l_len    = 0;
    241     lk.l_type   = F_WRLCK;
    242     lk.l_whence = SEEK_SET;
    243     if( 0 > fcntl( fd, F_SETLK, &lk ) )
    244     {
    245         if( EAGAIN == errno )
    246         {
    247             errmsg( "another copy of %s is already running", getmyname() );
    248         }
    249         else
    250         {
    251             errnomsg( "failed to obtain lock on file: %s", path );
    252         }
    253         close( fd );
    254         return -1;
    255     }
    256 
    257     ftruncate( fd, 0 );
    258     snprintf( pid, sizeof pid, "%i\n", getpid() );
    259     write( fd, pid, strlen( pid ) );
    260236
    261237    return fd;
     
    306282        unlink( gl_pidfile );
    307283    }
    308     if( 0 <= gl_lockfd )
    309     {
     284
     285    if( *gl_lockpath )
    310286        unlink( gl_lockpath );
    311         close( gl_lockfd );
    312     }
    313287}
    314288
Note: See TracChangeset for help on using the changeset viewer.