1 | /* |
---|
2 | * This file Copyright (C) 2008 Charles Kerr <charles@transmissionbt.com> |
---|
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 7547 2008-12-30 18:18:34Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <errno.h> |
---|
15 | #include <stdio.h> /* printf */ |
---|
16 | #include <stdlib.h> /* exit, atoi */ |
---|
17 | #include <string.h> /* strcmp */ |
---|
18 | |
---|
19 | #include <sys/types.h> /* umask*/ |
---|
20 | #include <sys/stat.h> /* umask*/ |
---|
21 | |
---|
22 | #include <fcntl.h> /* open */ |
---|
23 | #include <signal.h> |
---|
24 | #include <unistd.h> /* daemon */ |
---|
25 | |
---|
26 | #include <libtransmission/transmission.h> |
---|
27 | #include <libtransmission/bencode.h> |
---|
28 | #include <libtransmission/tr-getopt.h> |
---|
29 | #include <libtransmission/utils.h> |
---|
30 | #include <libtransmission/version.h> |
---|
31 | |
---|
32 | #define MY_NAME "transmission-daemon" |
---|
33 | |
---|
34 | static int closing = FALSE; |
---|
35 | static tr_session * mySession = NULL; |
---|
36 | |
---|
37 | /*** |
---|
38 | **** Config File |
---|
39 | ***/ |
---|
40 | |
---|
41 | static const char * |
---|
42 | getUsage( void ) |
---|
43 | { |
---|
44 | return "Transmission " LONG_VERSION_STRING |
---|
45 | " http://www.transmissionbt.com/\n" |
---|
46 | "A fast and easy BitTorrent client\n" |
---|
47 | "\n" |
---|
48 | MY_NAME " is a headless Transmission session\n" |
---|
49 | "that can be controlled via transmission-remote or Clutch.\n" |
---|
50 | "\n" |
---|
51 | "Usage: " MY_NAME " [options]"; |
---|
52 | } |
---|
53 | |
---|
54 | static const struct tr_option options[] = |
---|
55 | { |
---|
56 | { 'a', "allowed", "Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a", 1, "<list>" }, |
---|
57 | { 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL }, |
---|
58 | { 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL }, |
---|
59 | { 'd', "dump-settings", "Dump the settings and exit", "d", 0, NULL }, |
---|
60 | { 'f', "foreground", "Run in the foreground instead of daemonizing", "f", 0, NULL }, |
---|
61 | { 'g', "config-dir", "Where to look for configuration files", "g", 1, "<path>" }, |
---|
62 | { 'p', "port", "RPC port (Default: " TR_DEFAULT_RPC_PORT_STR ")", "p", 1, "<port>" }, |
---|
63 | { 't', "auth", "Require authentication", "t", 0, NULL }, |
---|
64 | { 'T', "no-auth", "Don't require authentication", "T", 0, NULL }, |
---|
65 | { 'u', "username", "Set username for authentication", "u", 1, "<username>" }, |
---|
66 | { 'v', "password", "Set password for authentication", "v", 1, "<password>" }, |
---|
67 | { 'w', "download-dir", "Where to save downloaded data", "w", 1, "<path>" }, |
---|
68 | { 'P', "peerport", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "P", 1, "<port>" }, |
---|
69 | { 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL }, |
---|
70 | { 'M', "no-portmap", "Disable portmapping", "M", 0, NULL }, |
---|
71 | { 'L', "peerlimit-global", "Maximum overall number of peers (Default: " TR_DEFAULT_PEER_LIMIT_GLOBAL_STR ")", "L", 1, "<limit>" }, |
---|
72 | { 'l', "peerlimit-torrent", "Maximum number of peers per torrent (Default: " TR_DEFAULT_PEER_LIMIT_TORRENT_STR ")", "l", 1, "<limit>" }, |
---|
73 | { 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL }, |
---|
74 | { 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL }, |
---|
75 | { 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL }, |
---|
76 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
77 | }; |
---|
78 | |
---|
79 | static void |
---|
80 | showUsage( void ) |
---|
81 | { |
---|
82 | tr_getopt_usage( MY_NAME, getUsage( ), options ); |
---|
83 | exit( 0 ); |
---|
84 | } |
---|
85 | |
---|
86 | static void |
---|
87 | gotsig( int sig UNUSED ) |
---|
88 | { |
---|
89 | closing = TRUE; |
---|
90 | } |
---|
91 | |
---|
92 | #if defined(WIN32) |
---|
93 | #define USE_NO_DAEMON |
---|
94 | #elif !defined(HAVE_DAEMON) || defined(__MIPSEL__) |
---|
95 | #define USE_TR_DAEMON |
---|
96 | #else |
---|
97 | #define USE_OS_DAEMON |
---|
98 | #endif |
---|
99 | |
---|
100 | static int |
---|
101 | tr_daemon( int nochdir, int noclose ) |
---|
102 | { |
---|
103 | #if defined(USE_OS_DAEMON) |
---|
104 | return daemon( nochdir, noclose ); |
---|
105 | #elif defined(USE_TR_DAEMON) |
---|
106 | pid_t pid = fork( ); |
---|
107 | if( pid < 0 ) |
---|
108 | return -1; |
---|
109 | else if( pid > 0 ) |
---|
110 | _exit( 0 ); |
---|
111 | else { |
---|
112 | pid = setsid( ); |
---|
113 | if( pid < 0 ) |
---|
114 | return -1; |
---|
115 | |
---|
116 | pid = fork( ); |
---|
117 | if( pid < 0 ) |
---|
118 | return -1; |
---|
119 | else if( pid > 0 ) |
---|
120 | _exit( 0 ); |
---|
121 | else { |
---|
122 | |
---|
123 | if( !nochdir ) |
---|
124 | if( chdir( "/" ) < 0 ) |
---|
125 | return -1; |
---|
126 | |
---|
127 | umask( (mode_t)0 ); |
---|
128 | |
---|
129 | if( !noclose ) { |
---|
130 | /* send stdin, stdout, and stderr to /dev/null */ |
---|
131 | int i; |
---|
132 | int fd = open( "/dev/null", O_RDWR, 0 ); |
---|
133 | for( i=0; i<3; ++i ) { |
---|
134 | if( close( i ) ) |
---|
135 | return -1; |
---|
136 | dup2( fd, i ); |
---|
137 | } |
---|
138 | close( fd ); |
---|
139 | } |
---|
140 | |
---|
141 | return 0; |
---|
142 | } |
---|
143 | } |
---|
144 | #else /* USE_NO_DAEMON */ |
---|
145 | return 0; |
---|
146 | #endif |
---|
147 | } |
---|
148 | |
---|
149 | static const char* |
---|
150 | getConfigDir( int argc, const char ** argv ) |
---|
151 | { |
---|
152 | int c; |
---|
153 | const char * configDir = NULL; |
---|
154 | const char * optarg; |
---|
155 | const int ind = tr_optind; |
---|
156 | |
---|
157 | while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) |
---|
158 | if( c == 'g' ) |
---|
159 | configDir = optarg; |
---|
160 | |
---|
161 | tr_optind = ind; |
---|
162 | |
---|
163 | if( configDir == NULL ) |
---|
164 | configDir = tr_getDefaultConfigDir( MY_NAME ); |
---|
165 | |
---|
166 | return configDir; |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | int |
---|
171 | main( int argc, |
---|
172 | char ** argv ) |
---|
173 | { |
---|
174 | int c; |
---|
175 | int64_t i; |
---|
176 | const char * optarg; |
---|
177 | tr_benc settings; |
---|
178 | tr_bool foreground = FALSE; |
---|
179 | tr_bool dumpSettings = FALSE; |
---|
180 | const char * configDir = NULL; |
---|
181 | |
---|
182 | signal( SIGINT, gotsig ); |
---|
183 | signal( SIGTERM, gotsig ); |
---|
184 | #ifndef WIN32 |
---|
185 | signal( SIGQUIT, gotsig ); |
---|
186 | signal( SIGPIPE, SIG_IGN ); |
---|
187 | signal( SIGHUP, SIG_IGN ); |
---|
188 | #endif |
---|
189 | |
---|
190 | /* load settings from defaults + config file */ |
---|
191 | tr_bencInitDict( &settings, 0 ); |
---|
192 | configDir = getConfigDir( argc, (const char**)argv ); |
---|
193 | tr_sessionLoadSettings( &settings, configDir, MY_NAME ); |
---|
194 | tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_ENABLED, 1 ); |
---|
195 | |
---|
196 | /* overwrite settings from the comamndline */ |
---|
197 | tr_optind = 1; |
---|
198 | while(( c = tr_getopt( getUsage(), argc, (const char**)argv, options, &optarg ))) { |
---|
199 | switch( c ) { |
---|
200 | case 'a': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_WHITELIST, optarg ); |
---|
201 | tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, 1 ); |
---|
202 | break; |
---|
203 | case 'b': tr_bencDictAddInt( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, 1 ); |
---|
204 | break; |
---|
205 | case 'B': tr_bencDictAddInt( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, 0 ); |
---|
206 | break; |
---|
207 | case 'd': dumpSettings = TRUE; |
---|
208 | break; |
---|
209 | case 'f': foreground = TRUE; |
---|
210 | break; |
---|
211 | case 'g': /* handled above */ |
---|
212 | break; |
---|
213 | case 'p': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_PORT, atoi( optarg ) ); |
---|
214 | break; |
---|
215 | case 't': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, 1 ); |
---|
216 | break; |
---|
217 | case 'T': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, 0 ); |
---|
218 | break; |
---|
219 | case 'u': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_USERNAME, optarg ); |
---|
220 | break; |
---|
221 | case 'v': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_PASSWORD, optarg ); |
---|
222 | break; |
---|
223 | case 'w': tr_bencDictAddStr( &settings, TR_PREFS_KEY_DOWNLOAD_DIR, optarg ); |
---|
224 | break; |
---|
225 | case 'P': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_PORT, atoi( optarg ) ); |
---|
226 | break; |
---|
227 | case 'm': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PORT_FORWARDING, 1 ); |
---|
228 | break; |
---|
229 | case 'M': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PORT_FORWARDING, 0 ); |
---|
230 | break; |
---|
231 | case 'L': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, atoi( optarg ) ); |
---|
232 | break; |
---|
233 | case 'l': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, atoi( optarg ) ); |
---|
234 | break; |
---|
235 | case 910: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_REQUIRED ); |
---|
236 | break; |
---|
237 | case 911: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED ); |
---|
238 | break; |
---|
239 | case 912: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED ); |
---|
240 | break; |
---|
241 | default: showUsage( ); |
---|
242 | break; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | if( dumpSettings ) |
---|
247 | { |
---|
248 | char * str = tr_bencSaveAsJSON( &settings, NULL ); |
---|
249 | fprintf( stderr, "%s", str ); |
---|
250 | tr_free( str ); |
---|
251 | return 0; |
---|
252 | } |
---|
253 | |
---|
254 | if( !foreground && tr_daemon( TRUE, FALSE ) < 0 ) |
---|
255 | { |
---|
256 | fprintf( stderr, "failed to daemonize: %s\n", strerror( errno ) ); |
---|
257 | exit( 1 ); |
---|
258 | } |
---|
259 | |
---|
260 | /* start the session */ |
---|
261 | mySession = tr_sessionInit( "daemon", configDir, FALSE, &settings ); |
---|
262 | |
---|
263 | if( tr_bencDictFindInt( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &i ) && i!=0 ) |
---|
264 | tr_ninf( MY_NAME, "requiring authentication" ); |
---|
265 | |
---|
266 | /* load the torrents */ |
---|
267 | { |
---|
268 | tr_ctor * ctor = tr_ctorNew( mySession ); |
---|
269 | tr_torrent ** torrents = tr_sessionLoadTorrents( mySession, ctor, NULL ); |
---|
270 | tr_free( torrents ); |
---|
271 | tr_ctorFree( ctor ); |
---|
272 | } |
---|
273 | |
---|
274 | while( !closing ) |
---|
275 | tr_wait( 1000 ); /* sleep one second */ |
---|
276 | |
---|
277 | /* shutdown */ |
---|
278 | printf( "Closing transmission session..." ); |
---|
279 | tr_sessionSaveSettings( mySession, configDir, &settings ); |
---|
280 | tr_sessionClose( mySession ); |
---|
281 | printf( " done.\n" ); |
---|
282 | |
---|
283 | /* cleanup */ |
---|
284 | tr_bencFree( &settings ); |
---|
285 | return 0; |
---|
286 | } |
---|