1 | /* |
---|
2 | * This file Copyright (C) 2008-2010 Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: daemon.c 10594 2010-05-01 13:45:03Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <errno.h> |
---|
14 | #include <stdio.h> /* printf */ |
---|
15 | #include <stdlib.h> /* exit, atoi */ |
---|
16 | #include <string.h> /* strcmp */ |
---|
17 | |
---|
18 | #include <sys/types.h> /* umask*/ |
---|
19 | #include <sys/stat.h> /* umask*/ |
---|
20 | |
---|
21 | #include <fcntl.h> /* open */ |
---|
22 | #include <signal.h> |
---|
23 | #ifdef HAVE_SYSLOG |
---|
24 | #include <syslog.h> |
---|
25 | #endif |
---|
26 | #include <unistd.h> /* daemon */ |
---|
27 | |
---|
28 | #include <event.h> |
---|
29 | |
---|
30 | #include <libtransmission/transmission.h> |
---|
31 | #include <libtransmission/bencode.h> |
---|
32 | #include <libtransmission/tr-getopt.h> |
---|
33 | #include <libtransmission/utils.h> |
---|
34 | #include <libtransmission/version.h> |
---|
35 | |
---|
36 | #include "watch.h" |
---|
37 | |
---|
38 | #define MY_NAME "transmission-daemon" |
---|
39 | |
---|
40 | #define PREF_KEY_DIR_WATCH "watch-dir" |
---|
41 | #define PREF_KEY_DIR_WATCH_ENABLED "watch-dir-enabled" |
---|
42 | |
---|
43 | static tr_bool paused = FALSE; |
---|
44 | static tr_bool closing = FALSE; |
---|
45 | static tr_session * mySession = NULL; |
---|
46 | |
---|
47 | /*** |
---|
48 | **** Config File |
---|
49 | ***/ |
---|
50 | |
---|
51 | static const char * |
---|
52 | getUsage( void ) |
---|
53 | { |
---|
54 | return "Transmission " LONG_VERSION_STRING |
---|
55 | " http://www.transmissionbt.com/\n" |
---|
56 | "A fast and easy BitTorrent client\n" |
---|
57 | "\n" |
---|
58 | MY_NAME " is a headless Transmission session\n" |
---|
59 | "that can be controlled via transmission-remote\n" |
---|
60 | "or the web interface.\n" |
---|
61 | "\n" |
---|
62 | "Usage: " MY_NAME " [options]"; |
---|
63 | } |
---|
64 | |
---|
65 | static const struct tr_option options[] = |
---|
66 | { |
---|
67 | { 'a', "allowed", "Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a", 1, "<list>" }, |
---|
68 | { 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL }, |
---|
69 | { 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL }, |
---|
70 | { 'c', "watch-dir", "Directory to watch for new .torrent files", "c", 1, "<directory>" }, |
---|
71 | { 'C', "no-watch-dir", "Disable the watch-dir", "C", 0, NULL }, |
---|
72 | { 941, "incomplete-dir", "Where to store new torrents until they're complete", NULL, 1, "<directory>" }, |
---|
73 | { 942, "no-incomplete-dir", "Don't store incomplete torrents in a different location", NULL, 0, NULL }, |
---|
74 | { 'd', "dump-settings", "Dump the settings and exit", "d", 0, NULL }, |
---|
75 | { 'e', "logfile", "Dump the log messages to this filename", "e", 1, "<filename>" }, |
---|
76 | { 'f', "foreground", "Run in the foreground instead of daemonizing", "f", 0, NULL }, |
---|
77 | { 'g', "config-dir", "Where to look for configuration files", "g", 1, "<path>" }, |
---|
78 | { 'p', "port", "RPC port (Default: " TR_DEFAULT_RPC_PORT_STR ")", "p", 1, "<port>" }, |
---|
79 | { 't', "auth", "Require authentication", "t", 0, NULL }, |
---|
80 | { 'T', "no-auth", "Don't require authentication", "T", 0, NULL }, |
---|
81 | { 'u', "username", "Set username for authentication", "u", 1, "<username>" }, |
---|
82 | { 'v', "password", "Set password for authentication", "v", 1, "<password>" }, |
---|
83 | { 'V', "version", "Show version number and exit", "V", 0, NULL }, |
---|
84 | { 'w', "download-dir", "Where to save downloaded data", "w", 1, "<path>" }, |
---|
85 | { 800, "paused", "Pause all torrents on startup", NULL, 0, NULL }, |
---|
86 | { 'o', "dht", "Enable distributed hash tables (DHT)", "o", 0, NULL }, |
---|
87 | { 'O', "no-dht", "Disable distributed hash tables (DHT)", "O", 0, NULL }, |
---|
88 | { 'P', "peerport", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "P", 1, "<port>" }, |
---|
89 | { 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL }, |
---|
90 | { 'M', "no-portmap", "Disable portmapping", "M", 0, NULL }, |
---|
91 | { 'L', "peerlimit-global", "Maximum overall number of peers (Default: " TR_DEFAULT_PEER_LIMIT_GLOBAL_STR ")", "L", 1, "<limit>" }, |
---|
92 | { 'l', "peerlimit-torrent", "Maximum number of peers per torrent (Default: " TR_DEFAULT_PEER_LIMIT_TORRENT_STR ")", "l", 1, "<limit>" }, |
---|
93 | { 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL }, |
---|
94 | { 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL }, |
---|
95 | { 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL }, |
---|
96 | { 'i', "bind-address-ipv4", "Where to listen for peer connections", "i", 1, "<ipv4 address>" }, |
---|
97 | { 'I', "bind-address-ipv6", "Where to listen for peer connections", "I", 1, "<ipv6 address>" }, |
---|
98 | { 'r', "rpc-bind-address", "Where to listen for RPC connections", "r", 1, "<ipv4 address>" }, |
---|
99 | { 953, "global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed until a specific ratio", "gsr", 1, "ratio" }, |
---|
100 | { 954, "no-global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed regardless of ratio", "GSR", 0, NULL }, |
---|
101 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
102 | }; |
---|
103 | |
---|
104 | static void |
---|
105 | showUsage( void ) |
---|
106 | { |
---|
107 | tr_getopt_usage( MY_NAME, getUsage( ), options ); |
---|
108 | exit( 0 ); |
---|
109 | } |
---|
110 | |
---|
111 | static void |
---|
112 | gotsig( int sig ) |
---|
113 | { |
---|
114 | switch( sig ) |
---|
115 | { |
---|
116 | case SIGHUP: |
---|
117 | { |
---|
118 | tr_benc settings; |
---|
119 | const char * configDir = tr_sessionGetConfigDir( mySession ); |
---|
120 | tr_inf( "Reloading settings from \"%s\"", configDir ); |
---|
121 | tr_bencInitDict( &settings, 0 ); |
---|
122 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, TRUE ); |
---|
123 | tr_sessionLoadSettings( &settings, configDir, MY_NAME ); |
---|
124 | tr_sessionSet( mySession, &settings ); |
---|
125 | tr_bencFree( &settings ); |
---|
126 | tr_sessionReloadBlocklists( mySession ); |
---|
127 | break; |
---|
128 | } |
---|
129 | |
---|
130 | default: |
---|
131 | closing = TRUE; |
---|
132 | break; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | #if defined(WIN32) |
---|
137 | #define USE_NO_DAEMON |
---|
138 | #elif !defined(HAVE_DAEMON) || defined(__UCLIBC__) |
---|
139 | #define USE_TR_DAEMON |
---|
140 | #else |
---|
141 | #define USE_OS_DAEMON |
---|
142 | #endif |
---|
143 | |
---|
144 | static int |
---|
145 | tr_daemon( int nochdir, int noclose ) |
---|
146 | { |
---|
147 | #if defined(USE_OS_DAEMON) |
---|
148 | return daemon( nochdir, noclose ); |
---|
149 | #elif defined(USE_TR_DAEMON) |
---|
150 | pid_t pid = fork( ); |
---|
151 | if( pid < 0 ) |
---|
152 | return -1; |
---|
153 | else if( pid > 0 ) |
---|
154 | _exit( 0 ); |
---|
155 | else { |
---|
156 | pid = setsid( ); |
---|
157 | if( pid < 0 ) |
---|
158 | return -1; |
---|
159 | |
---|
160 | pid = fork( ); |
---|
161 | if( pid < 0 ) |
---|
162 | return -1; |
---|
163 | else if( pid > 0 ) |
---|
164 | _exit( 0 ); |
---|
165 | else { |
---|
166 | |
---|
167 | if( !nochdir ) |
---|
168 | if( chdir( "/" ) < 0 ) |
---|
169 | return -1; |
---|
170 | |
---|
171 | umask( (mode_t)0 ); |
---|
172 | |
---|
173 | if( !noclose ) { |
---|
174 | /* send stdin, stdout, and stderr to /dev/null */ |
---|
175 | int i; |
---|
176 | int fd = open( "/dev/null", O_RDWR, 0 ); |
---|
177 | if( fd < 0 ) |
---|
178 | fprintf( stderr, "unable to open /dev/null: %s\n", tr_strerror(errno) ); |
---|
179 | for( i=0; i<3; ++i ) { |
---|
180 | if( close( i ) ) |
---|
181 | return -1; |
---|
182 | dup2( fd, i ); |
---|
183 | } |
---|
184 | close( fd ); |
---|
185 | } |
---|
186 | |
---|
187 | return 0; |
---|
188 | } |
---|
189 | } |
---|
190 | #else /* USE_NO_DAEMON */ |
---|
191 | return 0; |
---|
192 | #endif |
---|
193 | } |
---|
194 | |
---|
195 | static const char* |
---|
196 | getConfigDir( int argc, const char ** argv ) |
---|
197 | { |
---|
198 | int c; |
---|
199 | const char * configDir = NULL; |
---|
200 | const char * optarg; |
---|
201 | const int ind = tr_optind; |
---|
202 | |
---|
203 | while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) { |
---|
204 | if( c == 'g' ) { |
---|
205 | configDir = optarg; |
---|
206 | break; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | tr_optind = ind; |
---|
211 | |
---|
212 | if( configDir == NULL ) |
---|
213 | configDir = tr_getDefaultConfigDir( MY_NAME ); |
---|
214 | |
---|
215 | return configDir; |
---|
216 | } |
---|
217 | |
---|
218 | static void |
---|
219 | onFileAdded( tr_session * session, const char * dir, const char * file ) |
---|
220 | { |
---|
221 | if( strstr( file, ".torrent" ) != NULL ) |
---|
222 | { |
---|
223 | char * filename = tr_buildPath( dir, file, NULL ); |
---|
224 | tr_ctor * ctor = tr_ctorNew( session ); |
---|
225 | |
---|
226 | int err = tr_ctorSetMetainfoFromFile( ctor, filename ); |
---|
227 | if( !err ) |
---|
228 | tr_torrentNew( ctor, &err ); |
---|
229 | |
---|
230 | tr_ctorFree( ctor ); |
---|
231 | tr_free( filename ); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | static void |
---|
236 | printMessage( FILE * logfile, int level, const char * name, const char * message, const char * file, int line ) |
---|
237 | { |
---|
238 | if( logfile != NULL ) |
---|
239 | { |
---|
240 | char timestr[64]; |
---|
241 | tr_getLogTimeStr( timestr, sizeof( timestr ) ); |
---|
242 | if( name ) |
---|
243 | fprintf( logfile, "[%s] %s %s (%s:%d)\n", timestr, name, message, file, line ); |
---|
244 | else |
---|
245 | fprintf( logfile, "[%s] %s (%s:%d)\n", timestr, message, file, line ); |
---|
246 | } |
---|
247 | #ifdef HAVE_SYSLOG |
---|
248 | else /* daemon... write to syslog */ |
---|
249 | { |
---|
250 | int priority; |
---|
251 | |
---|
252 | /* figure out the syslog priority */ |
---|
253 | switch( level ) { |
---|
254 | case TR_MSG_ERR: priority = LOG_ERR; break; |
---|
255 | case TR_MSG_DBG: priority = LOG_DEBUG; break; |
---|
256 | default: priority = LOG_INFO; break; |
---|
257 | } |
---|
258 | |
---|
259 | if( name ) |
---|
260 | syslog( priority, "%s %s (%s:%d)", name, message, file, line ); |
---|
261 | else |
---|
262 | syslog( priority, "%s (%s:%d)", message, file, line ); |
---|
263 | } |
---|
264 | #endif |
---|
265 | } |
---|
266 | |
---|
267 | static void |
---|
268 | pumpLogMessages( FILE * logfile ) |
---|
269 | { |
---|
270 | const tr_msg_list * l; |
---|
271 | tr_msg_list * list = tr_getQueuedMessages( ); |
---|
272 | |
---|
273 | for( l=list; l!=NULL; l=l->next ) |
---|
274 | printMessage( logfile, l->level, l->name, l->message, l->file, l->line ); |
---|
275 | |
---|
276 | tr_freeMessageList( list ); |
---|
277 | } |
---|
278 | |
---|
279 | int |
---|
280 | main( int argc, char ** argv ) |
---|
281 | { |
---|
282 | int c; |
---|
283 | const char * optarg; |
---|
284 | tr_benc settings; |
---|
285 | tr_bool boolVal; |
---|
286 | tr_bool loaded; |
---|
287 | tr_bool foreground = FALSE; |
---|
288 | tr_bool dumpSettings = FALSE; |
---|
289 | const char * configDir = NULL; |
---|
290 | dtr_watchdir * watchdir = NULL; |
---|
291 | FILE * logfile = NULL; |
---|
292 | |
---|
293 | signal( SIGINT, gotsig ); |
---|
294 | signal( SIGTERM, gotsig ); |
---|
295 | #ifndef WIN32 |
---|
296 | signal( SIGHUP, gotsig ); |
---|
297 | #endif |
---|
298 | |
---|
299 | /* load settings from defaults + config file */ |
---|
300 | tr_bencInitDict( &settings, 0 ); |
---|
301 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, TRUE ); |
---|
302 | configDir = getConfigDir( argc, (const char**)argv ); |
---|
303 | loaded = tr_sessionLoadSettings( &settings, configDir, MY_NAME ); |
---|
304 | |
---|
305 | /* overwrite settings from the comamndline */ |
---|
306 | tr_optind = 1; |
---|
307 | while(( c = tr_getopt( getUsage(), argc, (const char**)argv, options, &optarg ))) { |
---|
308 | switch( c ) { |
---|
309 | case 'a': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_WHITELIST, optarg ); |
---|
310 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, TRUE ); |
---|
311 | break; |
---|
312 | case 'b': tr_bencDictAddBool( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, TRUE ); |
---|
313 | break; |
---|
314 | case 'B': tr_bencDictAddBool( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, FALSE ); |
---|
315 | break; |
---|
316 | case 'c': tr_bencDictAddStr( &settings, PREF_KEY_DIR_WATCH, optarg ); |
---|
317 | tr_bencDictAddBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, TRUE ); |
---|
318 | break; |
---|
319 | case 'C': tr_bencDictAddBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, FALSE ); |
---|
320 | break; |
---|
321 | case 941: |
---|
322 | tr_bencDictAddStr( &settings, TR_PREFS_KEY_INCOMPLETE_DIR, optarg ); |
---|
323 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, TRUE ); |
---|
324 | break; |
---|
325 | case 942: |
---|
326 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, FALSE ); |
---|
327 | break; |
---|
328 | case 'd': dumpSettings = TRUE; |
---|
329 | break; |
---|
330 | case 'e': logfile = fopen( optarg, "a+" ); |
---|
331 | if( logfile == NULL ) |
---|
332 | fprintf( stderr, "Couldn't open \"%s\": %s\n", optarg, tr_strerror( errno ) ); |
---|
333 | break; |
---|
334 | case 'f': foreground = TRUE; |
---|
335 | break; |
---|
336 | case 'g': /* handled above */ |
---|
337 | break; |
---|
338 | case 'V': /* version */ |
---|
339 | fprintf(stderr, "Transmission %s\n", LONG_VERSION_STRING); |
---|
340 | exit( 0 ); |
---|
341 | case 'o': |
---|
342 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_DHT_ENABLED, TRUE ); |
---|
343 | break; |
---|
344 | case 'O': |
---|
345 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_DHT_ENABLED, FALSE ); |
---|
346 | break; |
---|
347 | case 'p': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_PORT, atoi( optarg ) ); |
---|
348 | break; |
---|
349 | case 't': tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, TRUE ); |
---|
350 | break; |
---|
351 | case 'T': tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, FALSE ); |
---|
352 | break; |
---|
353 | case 'u': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_USERNAME, optarg ); |
---|
354 | break; |
---|
355 | case 'v': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_PASSWORD, optarg ); |
---|
356 | break; |
---|
357 | case 'w': tr_bencDictAddStr( &settings, TR_PREFS_KEY_DOWNLOAD_DIR, optarg ); |
---|
358 | break; |
---|
359 | case 'P': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_PORT, atoi( optarg ) ); |
---|
360 | break; |
---|
361 | case 'm': tr_bencDictAddBool( &settings, TR_PREFS_KEY_PORT_FORWARDING, TRUE ); |
---|
362 | break; |
---|
363 | case 'M': tr_bencDictAddBool( &settings, TR_PREFS_KEY_PORT_FORWARDING, FALSE ); |
---|
364 | break; |
---|
365 | case 'L': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, atoi( optarg ) ); |
---|
366 | break; |
---|
367 | case 'l': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, atoi( optarg ) ); |
---|
368 | break; |
---|
369 | case 800: paused = TRUE; |
---|
370 | break; |
---|
371 | case 910: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_REQUIRED ); |
---|
372 | break; |
---|
373 | case 911: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED ); |
---|
374 | break; |
---|
375 | case 912: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED ); |
---|
376 | break; |
---|
377 | case 'i': |
---|
378 | tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4, optarg ); |
---|
379 | break; |
---|
380 | case 'I': |
---|
381 | tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6, optarg ); |
---|
382 | break; |
---|
383 | case 'r': |
---|
384 | tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_BIND_ADDRESS, optarg ); |
---|
385 | break; |
---|
386 | case 953: |
---|
387 | tr_bencDictAddReal( &settings, TR_PREFS_KEY_RATIO, atof(optarg) ); |
---|
388 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_RATIO_ENABLED, TRUE ); |
---|
389 | break; |
---|
390 | case 954: |
---|
391 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_RATIO_ENABLED, FALSE ); |
---|
392 | break; |
---|
393 | default: showUsage( ); |
---|
394 | break; |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | if( foreground && !logfile ) |
---|
399 | logfile = stderr; |
---|
400 | |
---|
401 | if( !loaded ) |
---|
402 | { |
---|
403 | printMessage( logfile, TR_MSG_ERR, MY_NAME, "Error loading config file -- exiting.", __FILE__, __LINE__ ); |
---|
404 | return -1; |
---|
405 | } |
---|
406 | |
---|
407 | if( dumpSettings ) |
---|
408 | { |
---|
409 | char * str = tr_bencToStr( &settings, TR_FMT_JSON, NULL ); |
---|
410 | fprintf( stderr, "%s", str ); |
---|
411 | tr_free( str ); |
---|
412 | return 0; |
---|
413 | } |
---|
414 | |
---|
415 | if( !foreground && tr_daemon( TRUE, FALSE ) < 0 ) |
---|
416 | { |
---|
417 | char buf[256]; |
---|
418 | tr_snprintf( buf, sizeof( buf ), "Failed to daemonize: %s", tr_strerror( errno ) ); |
---|
419 | printMessage( logfile, TR_MSG_ERR, MY_NAME, buf, __FILE__, __LINE__ ); |
---|
420 | exit( 1 ); |
---|
421 | } |
---|
422 | |
---|
423 | /* start the session */ |
---|
424 | mySession = tr_sessionInit( "daemon", configDir, TRUE, &settings ); |
---|
425 | tr_ninf( NULL, "Using settings from \"%s\"", configDir ); |
---|
426 | tr_sessionSaveSettings( mySession, configDir, &settings ); |
---|
427 | |
---|
428 | if( tr_bencDictFindBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &boolVal ) && boolVal ) |
---|
429 | tr_ninf( MY_NAME, "requiring authentication" ); |
---|
430 | |
---|
431 | /* maybe add a watchdir */ |
---|
432 | { |
---|
433 | const char * dir; |
---|
434 | |
---|
435 | if( tr_bencDictFindBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, &boolVal ) |
---|
436 | && boolVal |
---|
437 | && tr_bencDictFindStr( &settings, PREF_KEY_DIR_WATCH, &dir ) |
---|
438 | && dir |
---|
439 | && *dir ) |
---|
440 | { |
---|
441 | tr_inf( "Watching \"%s\" for new .torrent files", dir ); |
---|
442 | watchdir = dtr_watchdir_new( mySession, dir, onFileAdded ); |
---|
443 | } |
---|
444 | } |
---|
445 | |
---|
446 | /* load the torrents */ |
---|
447 | { |
---|
448 | tr_torrent ** torrents; |
---|
449 | tr_ctor * ctor = tr_ctorNew( mySession ); |
---|
450 | if( paused ) |
---|
451 | tr_ctorSetPaused( ctor, TR_FORCE, TRUE ); |
---|
452 | torrents = tr_sessionLoadTorrents( mySession, ctor, NULL ); |
---|
453 | tr_free( torrents ); |
---|
454 | tr_ctorFree( ctor ); |
---|
455 | } |
---|
456 | |
---|
457 | #ifdef HAVE_SYSLOG |
---|
458 | if( !foreground ) |
---|
459 | openlog( MY_NAME, LOG_CONS, LOG_DAEMON ); |
---|
460 | #endif |
---|
461 | |
---|
462 | while( !closing ) { |
---|
463 | tr_wait_msec( 1000 ); /* sleep one second */ |
---|
464 | dtr_watchdir_update( watchdir ); |
---|
465 | pumpLogMessages( logfile ); |
---|
466 | } |
---|
467 | |
---|
468 | /* shutdown */ |
---|
469 | #if HAVE_SYSLOG |
---|
470 | if( !foreground ) |
---|
471 | { |
---|
472 | syslog( LOG_INFO, "%s", "Closing session" ); |
---|
473 | closelog( ); |
---|
474 | } |
---|
475 | #endif |
---|
476 | |
---|
477 | printf( "Closing transmission session..." ); |
---|
478 | tr_sessionSaveSettings( mySession, configDir, &settings ); |
---|
479 | dtr_watchdir_free( watchdir ); |
---|
480 | tr_sessionClose( mySession ); |
---|
481 | printf( " done.\n" ); |
---|
482 | |
---|
483 | /* cleanup */ |
---|
484 | tr_bencFree( &settings ); |
---|
485 | return 0; |
---|
486 | } |
---|