1 | /* |
---|
2 | * This file Copyright (C) 2008-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: daemon.c 14382 2014-12-13 15:22:39Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <errno.h> |
---|
11 | #include <stdio.h> /* printf */ |
---|
12 | #include <stdlib.h> /* exit, atoi */ |
---|
13 | |
---|
14 | #include <signal.h> |
---|
15 | #ifdef HAVE_SYSLOG |
---|
16 | #include <syslog.h> |
---|
17 | #endif |
---|
18 | |
---|
19 | #ifdef _WIN32 |
---|
20 | #include <process.h> /* getpid */ |
---|
21 | #else |
---|
22 | #include <fcntl.h> /* open */ |
---|
23 | #include <unistd.h> /* daemon */ |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <event2/buffer.h> |
---|
27 | #include <event2/event.h> |
---|
28 | |
---|
29 | #include <libtransmission/transmission.h> |
---|
30 | #include <libtransmission/error.h> |
---|
31 | #include <libtransmission/file.h> |
---|
32 | #include <libtransmission/tr-getopt.h> |
---|
33 | #include <libtransmission/log.h> |
---|
34 | #include <libtransmission/utils.h> |
---|
35 | #include <libtransmission/variant.h> |
---|
36 | #include <libtransmission/version.h> |
---|
37 | |
---|
38 | #ifdef USE_SYSTEMD_DAEMON |
---|
39 | #include <systemd/sd-daemon.h> |
---|
40 | #else |
---|
41 | static void sd_notify (int status UNUSED, const char * str UNUSED) { } |
---|
42 | static void sd_notifyf (int status UNUSED, const char * fmt UNUSED, ...) { } |
---|
43 | #endif |
---|
44 | |
---|
45 | #include "watch.h" |
---|
46 | |
---|
47 | #define MY_NAME "transmission-daemon" |
---|
48 | |
---|
49 | #define MEM_K 1024 |
---|
50 | #define MEM_K_STR "KiB" |
---|
51 | #define MEM_M_STR "MiB" |
---|
52 | #define MEM_G_STR "GiB" |
---|
53 | #define MEM_T_STR "TiB" |
---|
54 | |
---|
55 | #define DISK_K 1000 |
---|
56 | #define DISK_B_STR "B" |
---|
57 | #define DISK_K_STR "kB" |
---|
58 | #define DISK_M_STR "MB" |
---|
59 | #define DISK_G_STR "GB" |
---|
60 | #define DISK_T_STR "TB" |
---|
61 | |
---|
62 | #define SPEED_K 1000 |
---|
63 | #define SPEED_B_STR "B/s" |
---|
64 | #define SPEED_K_STR "kB/s" |
---|
65 | #define SPEED_M_STR "MB/s" |
---|
66 | #define SPEED_G_STR "GB/s" |
---|
67 | #define SPEED_T_STR "TB/s" |
---|
68 | |
---|
69 | static bool paused = false; |
---|
70 | static bool seenHUP = false; |
---|
71 | static const char *logfileName = NULL; |
---|
72 | static tr_sys_file_t logfile = TR_BAD_SYS_FILE; |
---|
73 | static tr_session * mySession = NULL; |
---|
74 | static tr_quark key_pidfile = 0; |
---|
75 | static struct event_base *ev_base = NULL; |
---|
76 | |
---|
77 | /*** |
---|
78 | **** Config File |
---|
79 | ***/ |
---|
80 | |
---|
81 | static const char * |
---|
82 | getUsage (void) |
---|
83 | { |
---|
84 | return "Transmission " LONG_VERSION_STRING |
---|
85 | " http://www.transmissionbt.com/\n" |
---|
86 | "A fast and easy BitTorrent client\n" |
---|
87 | "\n" |
---|
88 | MY_NAME " is a headless Transmission session\n" |
---|
89 | "that can be controlled via transmission-remote\n" |
---|
90 | "or the web interface.\n" |
---|
91 | "\n" |
---|
92 | "Usage: " MY_NAME " [options]"; |
---|
93 | } |
---|
94 | |
---|
95 | static const struct tr_option options[] = |
---|
96 | { |
---|
97 | |
---|
98 | { 'a', "allowed", "Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a", 1, "<list>" }, |
---|
99 | { 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL }, |
---|
100 | { 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL }, |
---|
101 | { 'c', "watch-dir", "Where to watch for new .torrent files", "c", 1, "<directory>" }, |
---|
102 | { 'C', "no-watch-dir", "Disable the watch-dir", "C", 0, NULL }, |
---|
103 | { 941, "incomplete-dir", "Where to store new torrents until they're complete", NULL, 1, "<directory>" }, |
---|
104 | { 942, "no-incomplete-dir", "Don't store incomplete torrents in a different location", NULL, 0, NULL }, |
---|
105 | { 'd', "dump-settings", "Dump the settings and exit", "d", 0, NULL }, |
---|
106 | { 'e', "logfile", "Dump the log messages to this filename", "e", 1, "<filename>" }, |
---|
107 | { 'f', "foreground", "Run in the foreground instead of daemonizing", "f", 0, NULL }, |
---|
108 | { 'g', "config-dir", "Where to look for configuration files", "g", 1, "<path>" }, |
---|
109 | { 'p', "port", "RPC port (Default: " TR_DEFAULT_RPC_PORT_STR ")", "p", 1, "<port>" }, |
---|
110 | { 't', "auth", "Require authentication", "t", 0, NULL }, |
---|
111 | { 'T', "no-auth", "Don't require authentication", "T", 0, NULL }, |
---|
112 | { 'u', "username", "Set username for authentication", "u", 1, "<username>" }, |
---|
113 | { 'v', "password", "Set password for authentication", "v", 1, "<password>" }, |
---|
114 | { 'V', "version", "Show version number and exit", "V", 0, NULL }, |
---|
115 | { 810, "log-error", "Show error messages", NULL, 0, NULL }, |
---|
116 | { 811, "log-info", "Show error and info messages", NULL, 0, NULL }, |
---|
117 | { 812, "log-debug", "Show error, info, and debug messages", NULL, 0, NULL }, |
---|
118 | { 'w', "download-dir", "Where to save downloaded data", "w", 1, "<path>" }, |
---|
119 | { 800, "paused", "Pause all torrents on startup", NULL, 0, NULL }, |
---|
120 | { 'o', "dht", "Enable distributed hash tables (DHT)", "o", 0, NULL }, |
---|
121 | { 'O', "no-dht", "Disable distributed hash tables (DHT)", "O", 0, NULL }, |
---|
122 | { 'y', "lpd", "Enable local peer discovery (LPD)", "y", 0, NULL }, |
---|
123 | { 'Y', "no-lpd", "Disable local peer discovery (LPD)", "Y", 0, NULL }, |
---|
124 | { 830, "utp", "Enable uTP for peer connections", NULL, 0, NULL }, |
---|
125 | { 831, "no-utp", "Disable uTP for peer connections", NULL, 0, NULL }, |
---|
126 | { 'P', "peerport", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "P", 1, "<port>" }, |
---|
127 | { 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL }, |
---|
128 | { 'M', "no-portmap", "Disable portmapping", "M", 0, NULL }, |
---|
129 | { 'L', "peerlimit-global", "Maximum overall number of peers (Default: " TR_DEFAULT_PEER_LIMIT_GLOBAL_STR ")", "L", 1, "<limit>" }, |
---|
130 | { 'l', "peerlimit-torrent", "Maximum number of peers per torrent (Default: " TR_DEFAULT_PEER_LIMIT_TORRENT_STR ")", "l", 1, "<limit>" }, |
---|
131 | { 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL }, |
---|
132 | { 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL }, |
---|
133 | { 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL }, |
---|
134 | { 'i', "bind-address-ipv4", "Where to listen for peer connections", "i", 1, "<ipv4 addr>" }, |
---|
135 | { 'I', "bind-address-ipv6", "Where to listen for peer connections", "I", 1, "<ipv6 addr>" }, |
---|
136 | { 'r', "rpc-bind-address", "Where to listen for RPC connections", "r", 1, "<ipv4 addr>" }, |
---|
137 | { 953, "global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed until a specific ratio", "gsr", 1, "ratio" }, |
---|
138 | { 954, "no-global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed regardless of ratio", "GSR", 0, NULL }, |
---|
139 | { 'x', "pid-file", "Enable PID file", "x", 1, "<pid-file>" }, |
---|
140 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
141 | }; |
---|
142 | |
---|
143 | static void |
---|
144 | showUsage (void) |
---|
145 | { |
---|
146 | tr_getopt_usage (MY_NAME, getUsage (), options); |
---|
147 | exit (0); |
---|
148 | } |
---|
149 | |
---|
150 | static bool |
---|
151 | reopen_log_file (const char *filename) |
---|
152 | { |
---|
153 | tr_error * error = NULL; |
---|
154 | const tr_sys_file_t old_log_file = logfile; |
---|
155 | const tr_sys_file_t new_log_file = tr_sys_file_open (filename, |
---|
156 | TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_APPEND, |
---|
157 | 0666, &error); |
---|
158 | |
---|
159 | if (new_log_file == TR_BAD_SYS_FILE) |
---|
160 | { |
---|
161 | fprintf (stderr, "Couldn't (re)open log file \"%s\": %s\n", filename, error->message); |
---|
162 | tr_error_free (error); |
---|
163 | return false; |
---|
164 | } |
---|
165 | |
---|
166 | logfile = new_log_file; |
---|
167 | |
---|
168 | if (old_log_file != TR_BAD_SYS_FILE) |
---|
169 | tr_sys_file_close (old_log_file, NULL); |
---|
170 | |
---|
171 | return true; |
---|
172 | } |
---|
173 | |
---|
174 | static void |
---|
175 | gotsig (int sig) |
---|
176 | { |
---|
177 | switch (sig) |
---|
178 | { |
---|
179 | #ifdef SIGHUP |
---|
180 | case SIGHUP: |
---|
181 | { |
---|
182 | if (!mySession) |
---|
183 | { |
---|
184 | tr_logAddInfo ("Deferring reload until session is fully started."); |
---|
185 | seenHUP = true; |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | tr_variant settings; |
---|
190 | const char * configDir; |
---|
191 | |
---|
192 | /* reopen the logfile to allow for log rotation */ |
---|
193 | if (logfileName != NULL) |
---|
194 | { |
---|
195 | reopen_log_file (logfileName); |
---|
196 | } |
---|
197 | |
---|
198 | configDir = tr_sessionGetConfigDir (mySession); |
---|
199 | tr_logAddInfo ("Reloading settings from \"%s\"", configDir); |
---|
200 | tr_variantInitDict (&settings, 0); |
---|
201 | tr_variantDictAddBool (&settings, TR_KEY_rpc_enabled, true); |
---|
202 | tr_sessionLoadSettings (&settings, configDir, MY_NAME); |
---|
203 | tr_sessionSet (mySession, &settings); |
---|
204 | tr_variantFree (&settings); |
---|
205 | tr_sessionReloadBlocklists (mySession); |
---|
206 | } |
---|
207 | break; |
---|
208 | } |
---|
209 | #endif |
---|
210 | |
---|
211 | default: |
---|
212 | tr_logAddError ("Unexpected signal (%d) in daemon, closing.", sig); |
---|
213 | /* no break */ |
---|
214 | |
---|
215 | case SIGINT: |
---|
216 | case SIGTERM: |
---|
217 | event_base_loopexit(ev_base, NULL); |
---|
218 | break; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | #if defined (_WIN32) |
---|
223 | #define USE_NO_DAEMON |
---|
224 | #elif !defined (HAVE_DAEMON) || defined (__UCLIBC__) |
---|
225 | #define USE_TR_DAEMON |
---|
226 | #else |
---|
227 | #define USE_OS_DAEMON |
---|
228 | #endif |
---|
229 | |
---|
230 | static int |
---|
231 | tr_daemon (int nochdir, int noclose) |
---|
232 | { |
---|
233 | #if defined (USE_OS_DAEMON) |
---|
234 | |
---|
235 | return daemon (nochdir, noclose); |
---|
236 | |
---|
237 | #elif defined (USE_TR_DAEMON) |
---|
238 | |
---|
239 | /* this is loosely based off of glibc's daemon () implementation |
---|
240 | * http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=misc/daemon.c */ |
---|
241 | |
---|
242 | switch (fork ()) { |
---|
243 | case -1: return -1; |
---|
244 | case 0: break; |
---|
245 | default: _exit (0); |
---|
246 | } |
---|
247 | |
---|
248 | if (setsid () == -1) |
---|
249 | return -1; |
---|
250 | |
---|
251 | if (!nochdir) |
---|
252 | chdir ("/"); |
---|
253 | |
---|
254 | if (!noclose) { |
---|
255 | int fd = open ("/dev/null", O_RDWR, 0); |
---|
256 | dup2 (fd, STDIN_FILENO); |
---|
257 | dup2 (fd, STDOUT_FILENO); |
---|
258 | dup2 (fd, STDERR_FILENO); |
---|
259 | close (fd); |
---|
260 | } |
---|
261 | |
---|
262 | return 0; |
---|
263 | |
---|
264 | #else /* USE_NO_DAEMON */ |
---|
265 | return 0; |
---|
266 | #endif |
---|
267 | } |
---|
268 | |
---|
269 | static const char* |
---|
270 | getConfigDir (int argc, const char ** argv) |
---|
271 | { |
---|
272 | int c; |
---|
273 | const char * configDir = NULL; |
---|
274 | const char * optarg; |
---|
275 | const int ind = tr_optind; |
---|
276 | |
---|
277 | while ((c = tr_getopt (getUsage (), argc, argv, options, &optarg))) { |
---|
278 | if (c == 'g') { |
---|
279 | configDir = optarg; |
---|
280 | break; |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | tr_optind = ind; |
---|
285 | |
---|
286 | if (configDir == NULL) |
---|
287 | configDir = tr_getDefaultConfigDir (MY_NAME); |
---|
288 | |
---|
289 | return configDir; |
---|
290 | } |
---|
291 | |
---|
292 | static void |
---|
293 | onFileAdded (tr_session * session, const char * dir, const char * file) |
---|
294 | { |
---|
295 | char * filename = tr_buildPath (dir, file, NULL); |
---|
296 | tr_ctor * ctor = tr_ctorNew (session); |
---|
297 | int err = tr_ctorSetMetainfoFromFile (ctor, filename); |
---|
298 | |
---|
299 | if (!err) |
---|
300 | { |
---|
301 | tr_torrentNew (ctor, &err, NULL); |
---|
302 | |
---|
303 | if (err == TR_PARSE_ERR) |
---|
304 | tr_logAddError ("Error parsing .torrent file \"%s\"", file); |
---|
305 | else |
---|
306 | { |
---|
307 | bool trash = false; |
---|
308 | int test = tr_ctorGetDeleteSource (ctor, &trash); |
---|
309 | |
---|
310 | tr_logAddInfo ("Parsing .torrent file successful \"%s\"", file); |
---|
311 | |
---|
312 | if (!test && trash) |
---|
313 | { |
---|
314 | tr_error * error = NULL; |
---|
315 | |
---|
316 | tr_logAddInfo ("Deleting input .torrent file \"%s\"", file); |
---|
317 | if (!tr_sys_path_remove (filename, &error)) |
---|
318 | { |
---|
319 | tr_logAddError ("Error deleting .torrent file: %s", error->message); |
---|
320 | tr_error_free (error); |
---|
321 | } |
---|
322 | } |
---|
323 | else |
---|
324 | { |
---|
325 | char * new_filename = tr_strdup_printf ("%s.added", filename); |
---|
326 | tr_sys_path_rename (filename, new_filename, NULL); |
---|
327 | tr_free (new_filename); |
---|
328 | } |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | tr_ctorFree (ctor); |
---|
333 | tr_free (filename); |
---|
334 | } |
---|
335 | |
---|
336 | static void |
---|
337 | printMessage (tr_sys_file_t logfile, int level, const char * name, const char * message, const char * file, int line) |
---|
338 | { |
---|
339 | if (logfile != TR_BAD_SYS_FILE) |
---|
340 | { |
---|
341 | char timestr[64]; |
---|
342 | tr_logGetTimeStr (timestr, sizeof (timestr)); |
---|
343 | if (name) |
---|
344 | tr_sys_file_write_fmt (logfile, "[%s] %s %s (%s:%d)" TR_NATIVE_EOL_STR, |
---|
345 | NULL, timestr, name, message, file, line); |
---|
346 | else |
---|
347 | tr_sys_file_write_fmt (logfile, "[%s] %s (%s:%d)" TR_NATIVE_EOL_STR, |
---|
348 | NULL, timestr, message, file, line); |
---|
349 | } |
---|
350 | #ifdef HAVE_SYSLOG |
---|
351 | else /* daemon... write to syslog */ |
---|
352 | { |
---|
353 | int priority; |
---|
354 | |
---|
355 | /* figure out the syslog priority */ |
---|
356 | switch (level) { |
---|
357 | case TR_LOG_ERROR: priority = LOG_ERR; break; |
---|
358 | case TR_LOG_DEBUG: priority = LOG_DEBUG; break; |
---|
359 | default: priority = LOG_INFO; break; |
---|
360 | } |
---|
361 | |
---|
362 | if (name) |
---|
363 | syslog (priority, "%s %s (%s:%d)", name, message, file, line); |
---|
364 | else |
---|
365 | syslog (priority, "%s (%s:%d)", message, file, line); |
---|
366 | } |
---|
367 | #endif |
---|
368 | } |
---|
369 | |
---|
370 | static void |
---|
371 | pumpLogMessages (tr_sys_file_t logfile) |
---|
372 | { |
---|
373 | const tr_log_message * l; |
---|
374 | tr_log_message * list = tr_logGetQueue (); |
---|
375 | |
---|
376 | for (l=list; l!=NULL; l=l->next) |
---|
377 | printMessage (logfile, l->level, l->name, l->message, l->file, l->line); |
---|
378 | |
---|
379 | if (logfile != TR_BAD_SYS_FILE) |
---|
380 | tr_sys_file_flush (logfile, NULL); |
---|
381 | |
---|
382 | tr_logFreeQueue (list); |
---|
383 | } |
---|
384 | |
---|
385 | static void |
---|
386 | reportStatus (void) |
---|
387 | { |
---|
388 | const double up = tr_sessionGetRawSpeed_KBps (mySession, TR_UP); |
---|
389 | const double dn = tr_sessionGetRawSpeed_KBps (mySession, TR_DOWN); |
---|
390 | |
---|
391 | if (up>0 || dn>0) |
---|
392 | sd_notifyf (0, "STATUS=Uploading %.2f KBps, Downloading %.2f KBps.\n", up, dn); |
---|
393 | else |
---|
394 | sd_notify (0, "STATUS=Idle.\n"); |
---|
395 | } |
---|
396 | |
---|
397 | static void |
---|
398 | periodicUpdate (evutil_socket_t fd UNUSED, short what UNUSED, void *watchdir) |
---|
399 | { |
---|
400 | dtr_watchdir_update (watchdir); |
---|
401 | |
---|
402 | pumpLogMessages (logfile); |
---|
403 | |
---|
404 | reportStatus (); |
---|
405 | } |
---|
406 | |
---|
407 | static tr_rpc_callback_status |
---|
408 | on_rpc_callback (tr_session * session UNUSED, |
---|
409 | tr_rpc_callback_type type, |
---|
410 | struct tr_torrent * tor UNUSED, |
---|
411 | void * user_data UNUSED) |
---|
412 | { |
---|
413 | if (type == TR_RPC_SESSION_CLOSE) |
---|
414 | event_base_loopexit(ev_base, NULL); |
---|
415 | return TR_RPC_OK; |
---|
416 | } |
---|
417 | |
---|
418 | int |
---|
419 | main (int argc, char ** argv) |
---|
420 | { |
---|
421 | int c; |
---|
422 | const char * optarg; |
---|
423 | tr_variant settings; |
---|
424 | bool boolVal; |
---|
425 | bool loaded; |
---|
426 | bool foreground = false; |
---|
427 | bool dumpSettings = false; |
---|
428 | const char * configDir = NULL; |
---|
429 | const char * pid_filename; |
---|
430 | dtr_watchdir * watchdir = NULL; |
---|
431 | bool pidfile_created = false; |
---|
432 | tr_session * session = NULL; |
---|
433 | struct event *status_ev; |
---|
434 | |
---|
435 | #ifdef _WIN32 |
---|
436 | tr_win32_make_args_utf8 (&argc, &argv); |
---|
437 | #endif |
---|
438 | |
---|
439 | key_pidfile = tr_quark_new ("pidfile", 7); |
---|
440 | |
---|
441 | signal (SIGINT, gotsig); |
---|
442 | signal (SIGTERM, gotsig); |
---|
443 | #ifdef SIGHUP |
---|
444 | signal (SIGHUP, gotsig); |
---|
445 | #endif |
---|
446 | |
---|
447 | /* load settings from defaults + config file */ |
---|
448 | tr_variantInitDict (&settings, 0); |
---|
449 | tr_variantDictAddBool (&settings, TR_KEY_rpc_enabled, true); |
---|
450 | configDir = getConfigDir (argc, (const char**)argv); |
---|
451 | loaded = tr_sessionLoadSettings (&settings, configDir, MY_NAME); |
---|
452 | |
---|
453 | /* overwrite settings from the comamndline */ |
---|
454 | tr_optind = 1; |
---|
455 | while ((c = tr_getopt (getUsage (), argc, (const char**)argv, options, &optarg))) { |
---|
456 | switch (c) { |
---|
457 | case 'a': tr_variantDictAddStr (&settings, TR_KEY_rpc_whitelist, optarg); |
---|
458 | tr_variantDictAddBool (&settings, TR_KEY_rpc_whitelist_enabled, true); |
---|
459 | break; |
---|
460 | case 'b': tr_variantDictAddBool (&settings, TR_KEY_blocklist_enabled, true); |
---|
461 | break; |
---|
462 | case 'B': tr_variantDictAddBool (&settings, TR_KEY_blocklist_enabled, false); |
---|
463 | break; |
---|
464 | case 'c': tr_variantDictAddStr (&settings, TR_KEY_watch_dir, optarg); |
---|
465 | tr_variantDictAddBool (&settings, TR_KEY_watch_dir_enabled, true); |
---|
466 | break; |
---|
467 | case 'C': tr_variantDictAddBool (&settings, TR_KEY_watch_dir_enabled, false); |
---|
468 | break; |
---|
469 | case 941: tr_variantDictAddStr (&settings, TR_KEY_incomplete_dir, optarg); |
---|
470 | tr_variantDictAddBool (&settings, TR_KEY_incomplete_dir_enabled, true); |
---|
471 | break; |
---|
472 | case 942: tr_variantDictAddBool (&settings, TR_KEY_incomplete_dir_enabled, false); |
---|
473 | break; |
---|
474 | case 'd': dumpSettings = true; |
---|
475 | break; |
---|
476 | case 'e': if (reopen_log_file (optarg)) |
---|
477 | logfileName = optarg; |
---|
478 | break; |
---|
479 | case 'f': foreground = true; |
---|
480 | break; |
---|
481 | case 'g': /* handled above */ |
---|
482 | break; |
---|
483 | case 'V': /* version */ |
---|
484 | fprintf (stderr, "%s %s\n", MY_NAME, LONG_VERSION_STRING); |
---|
485 | exit (0); |
---|
486 | case 'o': tr_variantDictAddBool (&settings, TR_KEY_dht_enabled, true); |
---|
487 | break; |
---|
488 | case 'O': tr_variantDictAddBool (&settings, TR_KEY_dht_enabled, false); |
---|
489 | break; |
---|
490 | case 'p': tr_variantDictAddInt (&settings, TR_KEY_rpc_port, atoi (optarg)); |
---|
491 | break; |
---|
492 | case 't': tr_variantDictAddBool (&settings, TR_KEY_rpc_authentication_required, true); |
---|
493 | break; |
---|
494 | case 'T': tr_variantDictAddBool (&settings, TR_KEY_rpc_authentication_required, false); |
---|
495 | break; |
---|
496 | case 'u': tr_variantDictAddStr (&settings, TR_KEY_rpc_username, optarg); |
---|
497 | break; |
---|
498 | case 'v': tr_variantDictAddStr (&settings, TR_KEY_rpc_password, optarg); |
---|
499 | break; |
---|
500 | case 'w': tr_variantDictAddStr (&settings, TR_KEY_download_dir, optarg); |
---|
501 | break; |
---|
502 | case 'P': tr_variantDictAddInt (&settings, TR_KEY_peer_port, atoi (optarg)); |
---|
503 | break; |
---|
504 | case 'm': tr_variantDictAddBool (&settings, TR_KEY_port_forwarding_enabled, true); |
---|
505 | break; |
---|
506 | case 'M': tr_variantDictAddBool (&settings, TR_KEY_port_forwarding_enabled, false); |
---|
507 | break; |
---|
508 | case 'L': tr_variantDictAddInt (&settings, TR_KEY_peer_limit_global, atoi (optarg)); |
---|
509 | break; |
---|
510 | case 'l': tr_variantDictAddInt (&settings, TR_KEY_peer_limit_per_torrent, atoi (optarg)); |
---|
511 | break; |
---|
512 | case 800: paused = true; |
---|
513 | break; |
---|
514 | case 910: tr_variantDictAddInt (&settings, TR_KEY_encryption, TR_ENCRYPTION_REQUIRED); |
---|
515 | break; |
---|
516 | case 911: tr_variantDictAddInt (&settings, TR_KEY_encryption, TR_ENCRYPTION_PREFERRED); |
---|
517 | break; |
---|
518 | case 912: tr_variantDictAddInt (&settings, TR_KEY_encryption, TR_CLEAR_PREFERRED); |
---|
519 | break; |
---|
520 | case 'i': tr_variantDictAddStr (&settings, TR_KEY_bind_address_ipv4, optarg); |
---|
521 | break; |
---|
522 | case 'I': tr_variantDictAddStr (&settings, TR_KEY_bind_address_ipv6, optarg); |
---|
523 | break; |
---|
524 | case 'r': tr_variantDictAddStr (&settings, TR_KEY_rpc_bind_address, optarg); |
---|
525 | break; |
---|
526 | case 953: tr_variantDictAddReal (&settings, TR_KEY_ratio_limit, atof (optarg)); |
---|
527 | tr_variantDictAddBool (&settings, TR_KEY_ratio_limit_enabled, true); |
---|
528 | break; |
---|
529 | case 954: tr_variantDictAddBool (&settings, TR_KEY_ratio_limit_enabled, false); |
---|
530 | break; |
---|
531 | case 'x': tr_variantDictAddStr (&settings, key_pidfile, optarg); |
---|
532 | break; |
---|
533 | case 'y': tr_variantDictAddBool (&settings, TR_KEY_lpd_enabled, true); |
---|
534 | break; |
---|
535 | case 'Y': tr_variantDictAddBool (&settings, TR_KEY_lpd_enabled, false); |
---|
536 | break; |
---|
537 | case 810: tr_variantDictAddInt (&settings, TR_KEY_message_level, TR_LOG_ERROR); |
---|
538 | break; |
---|
539 | case 811: tr_variantDictAddInt (&settings, TR_KEY_message_level, TR_LOG_INFO); |
---|
540 | break; |
---|
541 | case 812: tr_variantDictAddInt (&settings, TR_KEY_message_level, TR_LOG_DEBUG); |
---|
542 | break; |
---|
543 | case 830: tr_variantDictAddBool (&settings, TR_KEY_utp_enabled, true); |
---|
544 | break; |
---|
545 | case 831: tr_variantDictAddBool (&settings, TR_KEY_utp_enabled, false); |
---|
546 | break; |
---|
547 | default: showUsage (); |
---|
548 | break; |
---|
549 | } |
---|
550 | } |
---|
551 | |
---|
552 | if (foreground && logfile == TR_BAD_SYS_FILE) |
---|
553 | logfile = tr_sys_file_get_std (TR_STD_SYS_FILE_ERR, NULL); |
---|
554 | |
---|
555 | if (!loaded) |
---|
556 | { |
---|
557 | printMessage (logfile, TR_LOG_ERROR, MY_NAME, "Error loading config file -- exiting.", __FILE__, __LINE__); |
---|
558 | return -1; |
---|
559 | } |
---|
560 | |
---|
561 | if (dumpSettings) |
---|
562 | { |
---|
563 | char * str = tr_variantToStr (&settings, TR_VARIANT_FMT_JSON, NULL); |
---|
564 | fprintf (stderr, "%s", str); |
---|
565 | tr_free (str); |
---|
566 | return 0; |
---|
567 | } |
---|
568 | |
---|
569 | if (!foreground && tr_daemon (true, false) < 0) |
---|
570 | { |
---|
571 | char buf[256]; |
---|
572 | tr_snprintf (buf, sizeof (buf), "Failed to daemonize: %s", tr_strerror (errno)); |
---|
573 | printMessage (logfile, TR_LOG_ERROR, MY_NAME, buf, __FILE__, __LINE__); |
---|
574 | exit (1); |
---|
575 | } |
---|
576 | |
---|
577 | sd_notifyf (0, "MAINPID=%d\n", (int)getpid()); |
---|
578 | |
---|
579 | /* setup event state */ |
---|
580 | ev_base = event_base_new(); |
---|
581 | if (ev_base == NULL) |
---|
582 | { |
---|
583 | char buf[256]; |
---|
584 | tr_snprintf(buf, sizeof(buf), "Failed to init daemon event state: %s", tr_strerror(errno)); |
---|
585 | printMessage (logfile, TR_LOG_ERROR, MY_NAME, buf, __FILE__, __LINE__); |
---|
586 | exit (1); |
---|
587 | } |
---|
588 | |
---|
589 | /* start the session */ |
---|
590 | tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR); |
---|
591 | tr_formatter_size_init (DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR); |
---|
592 | tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR); |
---|
593 | session = tr_sessionInit ("daemon", configDir, true, &settings); |
---|
594 | tr_sessionSetRPCCallback (session, on_rpc_callback, NULL); |
---|
595 | tr_logAddNamedInfo (NULL, "Using settings from \"%s\"", configDir); |
---|
596 | tr_sessionSaveSettings (session, configDir, &settings); |
---|
597 | |
---|
598 | pid_filename = NULL; |
---|
599 | tr_variantDictFindStr (&settings, key_pidfile, &pid_filename, NULL); |
---|
600 | if (pid_filename && *pid_filename) |
---|
601 | { |
---|
602 | tr_error * error = NULL; |
---|
603 | tr_sys_file_t fp = tr_sys_file_open (pid_filename, |
---|
604 | TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, |
---|
605 | 0666, &error); |
---|
606 | if (fp != TR_BAD_SYS_FILE) |
---|
607 | { |
---|
608 | tr_sys_file_write_fmt (fp, "%d", NULL, (int)getpid ()); |
---|
609 | tr_sys_file_close (fp, NULL); |
---|
610 | tr_logAddInfo ("Saved pidfile \"%s\"", pid_filename); |
---|
611 | pidfile_created = true; |
---|
612 | } |
---|
613 | else |
---|
614 | { |
---|
615 | tr_logAddError ("Unable to save pidfile \"%s\": %s", pid_filename, error->message); |
---|
616 | tr_error_free (error); |
---|
617 | } |
---|
618 | } |
---|
619 | |
---|
620 | if (tr_variantDictFindBool (&settings, TR_KEY_rpc_authentication_required, &boolVal) && boolVal) |
---|
621 | tr_logAddNamedInfo (MY_NAME, "requiring authentication"); |
---|
622 | |
---|
623 | mySession = session; |
---|
624 | |
---|
625 | #ifdef SIGHUP |
---|
626 | /* If we got a SIGHUP during startup, process that now. */ |
---|
627 | if (seenHUP) |
---|
628 | gotsig (SIGHUP); |
---|
629 | #endif |
---|
630 | |
---|
631 | /* maybe add a watchdir */ |
---|
632 | { |
---|
633 | const char * dir; |
---|
634 | |
---|
635 | if (tr_variantDictFindBool (&settings, TR_KEY_watch_dir_enabled, &boolVal) |
---|
636 | && boolVal |
---|
637 | && tr_variantDictFindStr (&settings, TR_KEY_watch_dir, &dir, NULL) |
---|
638 | && dir |
---|
639 | && *dir) |
---|
640 | { |
---|
641 | tr_logAddInfo ("Watching \"%s\" for new .torrent files", dir); |
---|
642 | watchdir = dtr_watchdir_new (mySession, dir, onFileAdded); |
---|
643 | } |
---|
644 | } |
---|
645 | |
---|
646 | /* load the torrents */ |
---|
647 | { |
---|
648 | tr_torrent ** torrents; |
---|
649 | tr_ctor * ctor = tr_ctorNew (mySession); |
---|
650 | if (paused) |
---|
651 | tr_ctorSetPaused (ctor, TR_FORCE, true); |
---|
652 | torrents = tr_sessionLoadTorrents (mySession, ctor, NULL); |
---|
653 | tr_free (torrents); |
---|
654 | tr_ctorFree (ctor); |
---|
655 | } |
---|
656 | |
---|
657 | #ifdef HAVE_SYSLOG |
---|
658 | if (!foreground) |
---|
659 | openlog (MY_NAME, LOG_CONS|LOG_PID, LOG_DAEMON); |
---|
660 | #endif |
---|
661 | |
---|
662 | /* Create new timer event to report daemon status */ |
---|
663 | { |
---|
664 | struct timeval one_sec = { 1, 0 }; |
---|
665 | status_ev = event_new(ev_base, -1, EV_PERSIST, &periodicUpdate, watchdir); |
---|
666 | if (status_ev == NULL) |
---|
667 | { |
---|
668 | tr_logAddError("Failed to create status event %s", tr_strerror(errno)); |
---|
669 | goto cleanup; |
---|
670 | } |
---|
671 | if (event_add(status_ev, &one_sec) == -1) |
---|
672 | { |
---|
673 | tr_logAddError("Failed to add status event %s", tr_strerror(errno)); |
---|
674 | goto cleanup; |
---|
675 | } |
---|
676 | } |
---|
677 | |
---|
678 | sd_notify( 0, "READY=1\n" ); |
---|
679 | |
---|
680 | /* Run daemon event loop */ |
---|
681 | if (event_base_dispatch(ev_base) == -1) |
---|
682 | { |
---|
683 | tr_logAddError("Failed to launch daemon event loop: %s", tr_strerror(errno)); |
---|
684 | goto cleanup; |
---|
685 | } |
---|
686 | |
---|
687 | cleanup: |
---|
688 | sd_notify( 0, "STATUS=Closing transmission session...\n" ); |
---|
689 | printf ("Closing transmission session..."); |
---|
690 | |
---|
691 | if (status_ev) |
---|
692 | { |
---|
693 | event_del(status_ev); |
---|
694 | event_free(status_ev); |
---|
695 | } |
---|
696 | event_base_free(ev_base); |
---|
697 | |
---|
698 | tr_sessionSaveSettings (mySession, configDir, &settings); |
---|
699 | dtr_watchdir_free (watchdir); |
---|
700 | tr_sessionClose (mySession); |
---|
701 | pumpLogMessages (logfile); |
---|
702 | printf (" done.\n"); |
---|
703 | |
---|
704 | /* shutdown */ |
---|
705 | #ifdef HAVE_SYSLOG |
---|
706 | if (!foreground) |
---|
707 | { |
---|
708 | syslog (LOG_INFO, "%s", "Closing session"); |
---|
709 | closelog (); |
---|
710 | } |
---|
711 | #endif |
---|
712 | |
---|
713 | /* cleanup */ |
---|
714 | if (pidfile_created) |
---|
715 | tr_sys_path_remove (pid_filename, NULL); |
---|
716 | tr_variantFree (&settings); |
---|
717 | sd_notify (0, "STATUS=\n"); |
---|
718 | return 0; |
---|
719 | } |
---|