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 7503 2008-12-25 22:06:48Z 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 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
69 | }; |
---|
70 | |
---|
71 | static void |
---|
72 | showUsage( void ) |
---|
73 | { |
---|
74 | tr_getopt_usage( MY_NAME, getUsage( ), options ); |
---|
75 | exit( 0 ); |
---|
76 | } |
---|
77 | |
---|
78 | static void |
---|
79 | gotsig( int sig UNUSED ) |
---|
80 | { |
---|
81 | closing = TRUE; |
---|
82 | } |
---|
83 | |
---|
84 | static int |
---|
85 | tr_daemon( int nochdir, int noclose ) |
---|
86 | { |
---|
87 | #if defined(HAVE_DAEMON) && !defined(WIN32) |
---|
88 | return daemon( nochdir, noclose ); |
---|
89 | #else |
---|
90 | pid_t pid = fork( ); |
---|
91 | if( pid < 0 ) |
---|
92 | return -1; |
---|
93 | else if( pid > 0 ) |
---|
94 | _exit( 0 ); |
---|
95 | else { |
---|
96 | pid = setsid( ); |
---|
97 | if( pid < 0 ) |
---|
98 | return -1; |
---|
99 | |
---|
100 | pid = fork( ); |
---|
101 | if( pid < 0 ) |
---|
102 | return -1; |
---|
103 | else if( pid > 0 ) |
---|
104 | _exit( 0 ); |
---|
105 | else { |
---|
106 | |
---|
107 | if( !nochdir ) |
---|
108 | if( chdir( "/" ) < 0 ) |
---|
109 | return -1; |
---|
110 | |
---|
111 | umask( (mode_t)0 ); |
---|
112 | |
---|
113 | if( !noclose ) { |
---|
114 | /* send stdin, stdout, and stderr to /dev/null */ |
---|
115 | int i; |
---|
116 | int fd = open( "/dev/null", O_RDWR, 0 ); |
---|
117 | for( i=0; i<3; ++i ) { |
---|
118 | if( close( i ) ) |
---|
119 | return -1; |
---|
120 | dup2( fd, i ); |
---|
121 | } |
---|
122 | close( fd ); |
---|
123 | } |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | } |
---|
128 | #endif |
---|
129 | } |
---|
130 | |
---|
131 | static const char* |
---|
132 | getConfigDir( int argc, const char ** argv ) |
---|
133 | { |
---|
134 | int c; |
---|
135 | const char * configDir = NULL; |
---|
136 | const char * optarg; |
---|
137 | const int ind = tr_optind; |
---|
138 | |
---|
139 | while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) |
---|
140 | if( c == 'g' ) |
---|
141 | configDir = optarg; |
---|
142 | |
---|
143 | tr_optind = ind; |
---|
144 | |
---|
145 | if( configDir == NULL ) |
---|
146 | configDir = tr_getDefaultConfigDir( MY_NAME ); |
---|
147 | |
---|
148 | return configDir; |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | int |
---|
153 | main( int argc, |
---|
154 | char ** argv ) |
---|
155 | { |
---|
156 | int c; |
---|
157 | int64_t i; |
---|
158 | const char * optarg; |
---|
159 | tr_benc settings; |
---|
160 | tr_bool foreground = FALSE; |
---|
161 | tr_bool dumpSettings = FALSE; |
---|
162 | const char * configDir = NULL; |
---|
163 | |
---|
164 | signal( SIGINT, gotsig ); |
---|
165 | signal( SIGTERM, gotsig ); |
---|
166 | #ifndef WIN32 |
---|
167 | signal( SIGQUIT, gotsig ); |
---|
168 | signal( SIGPIPE, SIG_IGN ); |
---|
169 | signal( SIGHUP, SIG_IGN ); |
---|
170 | #endif |
---|
171 | |
---|
172 | /* load settings from defaults + config file */ |
---|
173 | tr_bencInitDict( &settings, 0 ); |
---|
174 | configDir = getConfigDir( argc, (const char**)argv ); |
---|
175 | tr_sessionLoadSettings( &settings, configDir, MY_NAME ); |
---|
176 | tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_ENABLED, 1 ); |
---|
177 | |
---|
178 | /* overwrite settings from the comamndline */ |
---|
179 | tr_optind = 1; |
---|
180 | while(( c = tr_getopt( getUsage(), argc, (const char**)argv, options, &optarg ))) { |
---|
181 | switch( c ) { |
---|
182 | case 'a': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_WHITELIST, optarg ); |
---|
183 | tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, 1 ); |
---|
184 | break; |
---|
185 | case 'b': tr_bencDictAddInt( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, 1 ); |
---|
186 | break; |
---|
187 | case 'B': tr_bencDictAddInt( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, 0 ); |
---|
188 | break; |
---|
189 | case 'd': dumpSettings = TRUE; |
---|
190 | break; |
---|
191 | case 'f': foreground = TRUE; |
---|
192 | break; |
---|
193 | case 'g': /* handled above */ |
---|
194 | break; |
---|
195 | case 'p': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_PORT, atoi( optarg ) ); |
---|
196 | break; |
---|
197 | case 't': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, 0 ); |
---|
198 | break; |
---|
199 | case 'T': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, 1 ); |
---|
200 | break; |
---|
201 | case 'u': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_USERNAME, optarg ); |
---|
202 | break; |
---|
203 | case 'v': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_PASSWORD, optarg ); |
---|
204 | break; |
---|
205 | case 'w': tr_bencDictAddStr( &settings, TR_PREFS_KEY_DOWNLOAD_DIR, optarg ); |
---|
206 | break; |
---|
207 | default: showUsage( ); |
---|
208 | break; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | if( dumpSettings ) |
---|
213 | { |
---|
214 | char * str = tr_bencSaveAsJSON( &settings, NULL ); |
---|
215 | fprintf( stderr, "%s", str ); |
---|
216 | tr_free( str ); |
---|
217 | return 0; |
---|
218 | } |
---|
219 | |
---|
220 | #ifndef WIN32 |
---|
221 | if( !foreground ) |
---|
222 | { |
---|
223 | if( 0 > tr_daemon( TRUE, FALSE ) ) |
---|
224 | { |
---|
225 | fprintf( stderr, "failed to daemonize: %s\n", strerror( errno ) ); |
---|
226 | exit( 1 ); |
---|
227 | } |
---|
228 | } |
---|
229 | #endif |
---|
230 | |
---|
231 | /* start the session */ |
---|
232 | mySession = tr_sessionInit( "daemon", configDir, FALSE, &settings ); |
---|
233 | |
---|
234 | if( tr_bencDictFindInt( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &i ) && i!=0 ) |
---|
235 | tr_ninf( MY_NAME, "requiring authentication" ); |
---|
236 | |
---|
237 | /* load the torrents */ |
---|
238 | { |
---|
239 | tr_ctor * ctor = tr_ctorNew( mySession ); |
---|
240 | tr_torrent ** torrents = tr_sessionLoadTorrents( mySession, ctor, NULL ); |
---|
241 | tr_free( torrents ); |
---|
242 | tr_ctorFree( ctor ); |
---|
243 | } |
---|
244 | |
---|
245 | while( !closing ) |
---|
246 | tr_wait( 1000 ); /* sleep one second */ |
---|
247 | |
---|
248 | /* shutdown */ |
---|
249 | printf( "Closing transmission session..." ); |
---|
250 | tr_sessionSaveSettings( mySession, configDir, &settings ); |
---|
251 | tr_sessionClose( mySession ); |
---|
252 | printf( " done.\n" ); |
---|
253 | |
---|
254 | /* cleanup */ |
---|
255 | tr_bencFree( &settings ); |
---|
256 | return 0; |
---|
257 | } |
---|