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