Changeset 5167 for trunk/daemon/daemon.c
- Timestamp:
- Feb 28, 2008, 7:06:23 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/daemon/daemon.c
r4390 r5167 33 33 #include <errno.h> 34 34 #include <event.h> 35 #include <fcntl.h>36 35 #include <getopt.h> 37 36 #include <signal.h> … … 43 42 44 43 #include <libtransmission/trcompat.h> 44 #include <libtransmission/platform.h> 45 45 #include <libtransmission/version.h> 46 46 … … 53 53 static void readargs ( int, char **, int *, int *, char **, char ** ); 54 54 static int trylocksock ( const char * ); 55 static int getlock ( const char * );56 55 static int getsock ( const char * ); 57 56 static void exitcleanup ( void ); … … 60 59 static int savepid ( const char * ); 61 60 62 static int gl_lockfd = -1;63 61 static char gl_lockpath[MAXPATHLEN] = ""; 64 62 static int gl_sockfd = -1; … … 184 182 } 185 183 184 static int 185 getlock( 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 186 206 int 187 207 trylocksock( const char * sockpath ) … … 198 218 199 219 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; 206 222 strlcpy( gl_lockpath, path, sizeof gl_lockpath ); 207 223 … … 218 234 gl_sockfd = fd; 219 235 strlcpy( gl_sockpath, sockpath, sizeof gl_sockpath ); 220 221 return fd;222 }223 224 int225 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 else250 {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 ) );260 236 261 237 return fd; … … 306 282 unlink( gl_pidfile ); 307 283 } 308 if( 0 <= gl_lockfd ) 309 {284 285 if( *gl_lockpath ) 310 286 unlink( gl_lockpath ); 311 close( gl_lockfd );312 }313 287 } 314 288
Note: See TracChangeset
for help on using the changeset viewer.