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 10031 2010-01-28 13:33:40Z 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 or Clutch.\n" |
---|
60 | "\n" |
---|
61 | "Usage: " MY_NAME " [options]"; |
---|
62 | } |
---|
63 | |
---|
64 | static const struct tr_option options[] = |
---|
65 | { |
---|
66 | { 'a', "allowed", "Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a", 1, "<list>" }, |
---|
67 | { 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL }, |
---|
68 | { 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL }, |
---|
69 | { 'c', "watch-dir", "Directory to watch for new .torrent files", "c", 1, "<directory>" }, |
---|
70 | { 'C', "no-watch-dir", "Disable the watch-dir", "C", 0, NULL }, |
---|
71 | { 'd', "dump-settings", "Dump the settings and exit", "d", 0, NULL }, |
---|
72 | { 'f', "foreground", "Run in the foreground instead of daemonizing", "f", 0, NULL }, |
---|
73 | { 'g', "config-dir", "Where to look for configuration files", "g", 1, "<path>" }, |
---|
74 | { 'p', "port", "RPC port (Default: " TR_DEFAULT_RPC_PORT_STR ")", "p", 1, "<port>" }, |
---|
75 | { 't', "auth", "Require authentication", "t", 0, NULL }, |
---|
76 | { 'T', "no-auth", "Don't require authentication", "T", 0, NULL }, |
---|
77 | { 'u', "username", "Set username for authentication", "u", 1, "<username>" }, |
---|
78 | { 'v', "password", "Set password for authentication", "v", 1, "<password>" }, |
---|
79 | { 'V', "version", "Show version number and exit", "V", 0, NULL }, |
---|
80 | { 'w', "download-dir", "Where to save downloaded data", "w", 1, "<path>" }, |
---|
81 | { 800, "paused", "Pause all torrents on startup", NULL, 0, NULL }, |
---|
82 | { 'P', "peerport", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "P", 1, "<port>" }, |
---|
83 | { 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL }, |
---|
84 | { 'M', "no-portmap", "Disable portmapping", "M", 0, NULL }, |
---|
85 | { 'L', "peerlimit-global", "Maximum overall number of peers (Default: " TR_DEFAULT_PEER_LIMIT_GLOBAL_STR ")", "L", 1, "<limit>" }, |
---|
86 | { 'l', "peerlimit-torrent", "Maximum number of peers per torrent (Default: " TR_DEFAULT_PEER_LIMIT_TORRENT_STR ")", "l", 1, "<limit>" }, |
---|
87 | { 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL }, |
---|
88 | { 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL }, |
---|
89 | { 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL }, |
---|
90 | { 'i', "bind-address-ipv4", "Where to listen for peer connections", "i", 1, "<ipv4 address>" }, |
---|
91 | { 'I', "bind-address-ipv6", "Where to listen for peer connections", "I", 1, "<ipv6 address>" }, |
---|
92 | { 'r', "rpc-bind-address", "Where to listen for RPC connections", "r", 1, "<ipv4 address>" }, |
---|
93 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
94 | }; |
---|
95 | |
---|
96 | static void |
---|
97 | showUsage( void ) |
---|
98 | { |
---|
99 | tr_getopt_usage( MY_NAME, getUsage( ), options ); |
---|
100 | exit( 0 ); |
---|
101 | } |
---|
102 | |
---|
103 | static void |
---|
104 | gotsig( int sig ) |
---|
105 | { |
---|
106 | switch( sig ) |
---|
107 | { |
---|
108 | case SIGHUP: |
---|
109 | { |
---|
110 | tr_benc settings; |
---|
111 | const char * configDir = tr_sessionGetConfigDir( mySession ); |
---|
112 | tr_inf( "Reloading settings from \"%s\"", configDir ); |
---|
113 | tr_bencInitDict( &settings, 0 ); |
---|
114 | tr_sessionLoadSettings( &settings, configDir, MY_NAME ); |
---|
115 | tr_sessionSet( mySession, &settings ); |
---|
116 | tr_bencFree( &settings ); |
---|
117 | tr_sessionReloadBlocklists( mySession ); |
---|
118 | break; |
---|
119 | } |
---|
120 | |
---|
121 | default: |
---|
122 | closing = TRUE; |
---|
123 | break; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | #if defined(WIN32) |
---|
128 | #define USE_NO_DAEMON |
---|
129 | #elif !defined(HAVE_DAEMON) || defined(__UCLIBC__) |
---|
130 | #define USE_TR_DAEMON |
---|
131 | #else |
---|
132 | #define USE_OS_DAEMON |
---|
133 | #endif |
---|
134 | |
---|
135 | static int |
---|
136 | tr_daemon( int nochdir, int noclose ) |
---|
137 | { |
---|
138 | #if defined(USE_OS_DAEMON) |
---|
139 | return daemon( nochdir, noclose ); |
---|
140 | #elif defined(USE_TR_DAEMON) |
---|
141 | pid_t pid = fork( ); |
---|
142 | if( pid < 0 ) |
---|
143 | return -1; |
---|
144 | else if( pid > 0 ) |
---|
145 | _exit( 0 ); |
---|
146 | else { |
---|
147 | pid = setsid( ); |
---|
148 | if( pid < 0 ) |
---|
149 | return -1; |
---|
150 | |
---|
151 | pid = fork( ); |
---|
152 | if( pid < 0 ) |
---|
153 | return -1; |
---|
154 | else if( pid > 0 ) |
---|
155 | _exit( 0 ); |
---|
156 | else { |
---|
157 | |
---|
158 | if( !nochdir ) |
---|
159 | if( chdir( "/" ) < 0 ) |
---|
160 | return -1; |
---|
161 | |
---|
162 | umask( (mode_t)0 ); |
---|
163 | |
---|
164 | if( !noclose ) { |
---|
165 | /* send stdin, stdout, and stderr to /dev/null */ |
---|
166 | int i; |
---|
167 | int fd = open( "/dev/null", O_RDWR, 0 ); |
---|
168 | for( i=0; i<3; ++i ) { |
---|
169 | if( close( i ) ) |
---|
170 | return -1; |
---|
171 | dup2( fd, i ); |
---|
172 | } |
---|
173 | close( fd ); |
---|
174 | } |
---|
175 | |
---|
176 | return 0; |
---|
177 | } |
---|
178 | } |
---|
179 | #else /* USE_NO_DAEMON */ |
---|
180 | return 0; |
---|
181 | #endif |
---|
182 | } |
---|
183 | |
---|
184 | static const char* |
---|
185 | getConfigDir( int argc, const char ** argv ) |
---|
186 | { |
---|
187 | int c; |
---|
188 | const char * configDir = NULL; |
---|
189 | const char * optarg; |
---|
190 | const int ind = tr_optind; |
---|
191 | |
---|
192 | while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) { |
---|
193 | if( c == 'g' ) { |
---|
194 | configDir = optarg; |
---|
195 | break; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | tr_optind = ind; |
---|
200 | |
---|
201 | if( configDir == NULL ) |
---|
202 | configDir = tr_getDefaultConfigDir( MY_NAME ); |
---|
203 | |
---|
204 | return configDir; |
---|
205 | } |
---|
206 | |
---|
207 | static void |
---|
208 | onFileAdded( tr_session * session, const char * dir, const char * file ) |
---|
209 | { |
---|
210 | if( strstr( file, ".torrent" ) != NULL ) |
---|
211 | { |
---|
212 | char * filename = tr_buildPath( dir, file, NULL ); |
---|
213 | tr_ctor * ctor = tr_ctorNew( session ); |
---|
214 | |
---|
215 | int err = tr_ctorSetMetainfoFromFile( ctor, filename ); |
---|
216 | if( !err ) |
---|
217 | tr_torrentNew( ctor, &err ); |
---|
218 | |
---|
219 | tr_ctorFree( ctor ); |
---|
220 | tr_free( filename ); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | static void |
---|
225 | pumpLogMessages( tr_bool foreground ) |
---|
226 | { |
---|
227 | const tr_msg_list * l; |
---|
228 | tr_msg_list * list = tr_getQueuedMessages( ); |
---|
229 | |
---|
230 | for( l=list; l!=NULL; l=l->next ) |
---|
231 | { |
---|
232 | #ifdef HAVE_SYSLOG |
---|
233 | if( foreground ) |
---|
234 | { |
---|
235 | char timestr[64]; |
---|
236 | tr_getLogTimeStr( timestr, sizeof( timestr ) ); |
---|
237 | if( l->name ) |
---|
238 | fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, l->name, l->message, l->file, l->line ); |
---|
239 | else |
---|
240 | fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, l->message, l->file, l->line ); |
---|
241 | } |
---|
242 | else /* daemon... write to syslog */ |
---|
243 | { |
---|
244 | int priority; |
---|
245 | |
---|
246 | /* figure out the syslog priority */ |
---|
247 | switch( l->level ) { |
---|
248 | case TR_MSG_ERR: priority = LOG_ERR; break; |
---|
249 | case TR_MSG_DBG: priority = LOG_DEBUG; break; |
---|
250 | default: priority = LOG_INFO; break; |
---|
251 | } |
---|
252 | |
---|
253 | if( l->name ) |
---|
254 | syslog( priority, "%s %s (%s:%d)", l->name, l->message, l->file, l->line ); |
---|
255 | else |
---|
256 | syslog( priority, "%s (%s:%d)", l->message, l->file, l->line ); |
---|
257 | } |
---|
258 | #else |
---|
259 | { |
---|
260 | char timestr[64]; |
---|
261 | tr_getLogTimeStr( timestr, sizeof( timestr ) ); |
---|
262 | if( l->name ) |
---|
263 | fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, l->name, l->message, l->file, l->line ); |
---|
264 | else |
---|
265 | fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, l->message, l->file, l->line ); |
---|
266 | } |
---|
267 | #endif |
---|
268 | } |
---|
269 | |
---|
270 | tr_freeMessageList( list ); |
---|
271 | } |
---|
272 | |
---|
273 | int |
---|
274 | main( int argc, char ** argv ) |
---|
275 | { |
---|
276 | int c; |
---|
277 | const char * optarg; |
---|
278 | tr_benc settings; |
---|
279 | tr_bool boolVal; |
---|
280 | tr_bool loaded; |
---|
281 | tr_bool foreground = FALSE; |
---|
282 | tr_bool dumpSettings = FALSE; |
---|
283 | const char * configDir = NULL; |
---|
284 | dtr_watchdir * watchdir = NULL; |
---|
285 | |
---|
286 | signal( SIGINT, gotsig ); |
---|
287 | signal( SIGTERM, gotsig ); |
---|
288 | #ifndef WIN32 |
---|
289 | signal( SIGHUP, gotsig ); |
---|
290 | #endif |
---|
291 | |
---|
292 | /* load settings from defaults + config file */ |
---|
293 | tr_bencInitDict( &settings, 0 ); |
---|
294 | configDir = getConfigDir( argc, (const char**)argv ); |
---|
295 | loaded = tr_sessionLoadSettings( &settings, configDir, MY_NAME ); |
---|
296 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, TRUE ); |
---|
297 | |
---|
298 | /* overwrite settings from the comamndline */ |
---|
299 | tr_optind = 1; |
---|
300 | while(( c = tr_getopt( getUsage(), argc, (const char**)argv, options, &optarg ))) { |
---|
301 | switch( c ) { |
---|
302 | case 'a': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_WHITELIST, optarg ); |
---|
303 | tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, TRUE ); |
---|
304 | break; |
---|
305 | case 'b': tr_bencDictAddBool( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, TRUE ); |
---|
306 | break; |
---|
307 | case 'B': tr_bencDictAddBool( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, FALSE ); |
---|
308 | break; |
---|
309 | case 'c': tr_bencDictAddStr( &settings, PREF_KEY_DIR_WATCH, optarg ); |
---|
310 | tr_bencDictAddBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, TRUE ); |
---|
311 | break; |
---|
312 | case 'C': tr_bencDictAddBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, FALSE ); |
---|
313 | break; |
---|
314 | case 'd': dumpSettings = TRUE; |
---|
315 | break; |
---|
316 | case 'f': foreground = TRUE; |
---|
317 | break; |
---|
318 | case 'g': /* handled above */ |
---|
319 | break; |
---|
320 | case 'V': /* version */ |
---|
321 | fprintf(stderr, "Transmission %s\n", LONG_VERSION_STRING); |
---|
322 | exit( 0 ); |
---|
323 | case 'p': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_PORT, atoi( optarg ) ); |
---|
324 | break; |
---|
325 | case 't': tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, TRUE ); |
---|
326 | break; |
---|
327 | case 'T': tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, FALSE ); |
---|
328 | break; |
---|
329 | case 'u': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_USERNAME, optarg ); |
---|
330 | break; |
---|
331 | case 'v': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_PASSWORD, optarg ); |
---|
332 | break; |
---|
333 | case 'w': tr_bencDictAddStr( &settings, TR_PREFS_KEY_DOWNLOAD_DIR, optarg ); |
---|
334 | break; |
---|
335 | case 'P': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_PORT, atoi( optarg ) ); |
---|
336 | break; |
---|
337 | case 'm': tr_bencDictAddBool( &settings, TR_PREFS_KEY_PORT_FORWARDING, TRUE ); |
---|
338 | break; |
---|
339 | case 'M': tr_bencDictAddBool( &settings, TR_PREFS_KEY_PORT_FORWARDING, FALSE ); |
---|
340 | break; |
---|
341 | case 'L': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, atoi( optarg ) ); |
---|
342 | break; |
---|
343 | case 'l': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, atoi( optarg ) ); |
---|
344 | break; |
---|
345 | case 800: paused = TRUE; |
---|
346 | break; |
---|
347 | case 910: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_REQUIRED ); |
---|
348 | break; |
---|
349 | case 911: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED ); |
---|
350 | break; |
---|
351 | case 912: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED ); |
---|
352 | break; |
---|
353 | case 'i': |
---|
354 | tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4, optarg ); |
---|
355 | break; |
---|
356 | case 'I': |
---|
357 | tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6, optarg ); |
---|
358 | break; |
---|
359 | case 'r': |
---|
360 | tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_BIND_ADDRESS, optarg ); |
---|
361 | break; |
---|
362 | default: showUsage( ); |
---|
363 | break; |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | if( dumpSettings ) |
---|
368 | { |
---|
369 | char * str = tr_bencToStr( &settings, TR_FMT_JSON, NULL ); |
---|
370 | fprintf( stderr, "%s", str ); |
---|
371 | tr_free( str ); |
---|
372 | return 0; |
---|
373 | } |
---|
374 | |
---|
375 | if( !foreground && tr_daemon( TRUE, FALSE ) < 0 ) |
---|
376 | { |
---|
377 | fprintf( stderr, "failed to daemonize: %s\n", strerror( errno ) ); |
---|
378 | exit( 1 ); |
---|
379 | } |
---|
380 | |
---|
381 | /* start the session */ |
---|
382 | mySession = tr_sessionInit( "daemon", configDir, TRUE, &settings ); |
---|
383 | |
---|
384 | if( loaded ) |
---|
385 | tr_ninf( NULL, "Using settings from \"%s\"", configDir ); |
---|
386 | else |
---|
387 | tr_nerr( NULL, "Couldn't find settings in \"%s\"; using defaults", configDir ); |
---|
388 | |
---|
389 | tr_sessionSaveSettings( mySession, configDir, &settings ); |
---|
390 | |
---|
391 | if( tr_bencDictFindBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &boolVal ) && boolVal ) |
---|
392 | tr_ninf( MY_NAME, "requiring authentication" ); |
---|
393 | |
---|
394 | /* maybe add a watchdir */ |
---|
395 | { |
---|
396 | const char * dir; |
---|
397 | |
---|
398 | if( tr_bencDictFindBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, &boolVal ) |
---|
399 | && boolVal |
---|
400 | && tr_bencDictFindStr( &settings, PREF_KEY_DIR_WATCH, &dir ) |
---|
401 | && dir |
---|
402 | && *dir ) |
---|
403 | { |
---|
404 | tr_inf( "Watching \"%s\" for new .torrent files", dir ); |
---|
405 | watchdir = dtr_watchdir_new( mySession, dir, onFileAdded ); |
---|
406 | } |
---|
407 | } |
---|
408 | |
---|
409 | /* load the torrents */ |
---|
410 | { |
---|
411 | tr_torrent ** torrents; |
---|
412 | tr_ctor * ctor = tr_ctorNew( mySession ); |
---|
413 | if( paused ) |
---|
414 | tr_ctorSetPaused( ctor, TR_FORCE, TRUE ); |
---|
415 | torrents = tr_sessionLoadTorrents( mySession, ctor, NULL ); |
---|
416 | tr_free( torrents ); |
---|
417 | tr_ctorFree( ctor ); |
---|
418 | } |
---|
419 | |
---|
420 | #ifdef HAVE_SYSLOG |
---|
421 | if( !foreground ) |
---|
422 | openlog( MY_NAME, LOG_CONS, LOG_DAEMON ); |
---|
423 | #endif |
---|
424 | |
---|
425 | while( !closing ) { |
---|
426 | tr_wait_msec( 1000 ); /* sleep one second */ |
---|
427 | dtr_watchdir_update( watchdir ); |
---|
428 | pumpLogMessages( foreground ); |
---|
429 | } |
---|
430 | |
---|
431 | closelog( ); |
---|
432 | |
---|
433 | /* shutdown */ |
---|
434 | #if HAVE_SYSLOG |
---|
435 | if( !foreground ) |
---|
436 | syslog( LOG_INFO, "%s", "Closing session" ); |
---|
437 | #endif |
---|
438 | printf( "Closing transmission session..." ); |
---|
439 | tr_sessionSaveSettings( mySession, configDir, &settings ); |
---|
440 | dtr_watchdir_free( watchdir ); |
---|
441 | tr_sessionClose( mySession ); |
---|
442 | printf( " done.\n" ); |
---|
443 | |
---|
444 | /* cleanup */ |
---|
445 | tr_bencFree( &settings ); |
---|
446 | return 0; |
---|
447 | } |
---|