summaryrefslogtreecommitdiff
path: root/src/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/opentracker.c')
-rw-r--r--src/opentracker.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/opentracker.c b/src/opentracker.c
index e467ce6..759a6d0 100644
--- a/src/opentracker.c
+++ b/src/opentracker.c
@@ -7,6 +7,7 @@
7#include <errno.h> 7#include <errno.h>
8#include <limits.h> 8#include <limits.h>
9#include <pthread.h> 9#include <pthread.h>
10#include <grp.h>
10#include <pwd.h> 11#include <pwd.h>
11#include <signal.h> 12#include <signal.h>
12#include <stdint.h> 13#include <stdint.h>
@@ -300,9 +301,13 @@ void load_state(const char *const state_filename) {
300 FILE *state_filehandle; 301 FILE *state_filehandle;
301 char inbuf[512]; 302 char inbuf[512];
302 ot_hash infohash; 303 ot_hash infohash;
303 uint64_t age, downcount; 304 uint64_t touched, downcount;
304 int consumed; 305 int consumed;
305 306
307 /* The file records the minute a torrent was last touched, by the clock that
308 survives a restart */
309 uint64_t now = (uint64_t)time_wall_minutes();
310
306 state_filehandle = fopen(state_filename, "r"); 311 state_filehandle = fopen(state_filename, "r");
307 312
308 if (state_filehandle == NULL) { 313 if (state_filehandle == NULL) {
@@ -318,12 +323,12 @@ void load_state(const char *const state_filename) {
318 continue; 323 continue;
319 i = 2 * sizeof(ot_hash); 324 i = 2 * sizeof(ot_hash);
320 325
321 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &age, OT_TORRENT_TIMEOUT))) 326 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &touched, now)))
322 continue; 327 continue;
323 i += consumed; 328 i += consumed;
324 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &downcount, SIZE_MAX))) 329 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &downcount, SIZE_MAX)))
325 continue; 330 continue;
326 add_torrent_from_saved_state(infohash, (ot_time)age, (size_t)downcount); 331 add_torrent_from_saved_state(infohash, (ot_time)(now - touched), (size_t)downcount);
327 } 332 }
328 333
329 fclose(state_filehandle); 334 fclose(state_filehandle);
@@ -356,10 +361,10 @@ int drop_privileges(const char *const serveruser, const char *const serverdir) {
356 /* If we can't find server user, revert to nobody's default uid */ 361 /* If we can't find server user, revert to nobody's default uid */
357 if (!pws) { 362 if (!pws) {
358 fprintf(stderr, "Warning: Could not get password entry for %s. Reverting to uid -2.\n", serveruser); 363 fprintf(stderr, "Warning: Could not get password entry for %s. Reverting to uid -2.\n", serveruser);
359 if (setegid((gid_t)-2) || setgid((gid_t)-2) || setuid((uid_t)-2) || seteuid((uid_t)-2)) 364 if (setgroups(0, NULL) || setegid((gid_t)-2) || setgid((gid_t)-2) || setuid((uid_t)-2) || seteuid((uid_t)-2))
360 panic("Could not set uid to value -2"); 365 panic("Could not set uid to value -2");
361 } else { 366 } else {
362 if (setegid(pws->pw_gid) || setgid(pws->pw_gid) || setuid(pws->pw_uid) || seteuid(pws->pw_uid)) 367 if (setgroups(0, NULL) || setegid(pws->pw_gid) || setgid(pws->pw_gid) || setuid(pws->pw_uid) || seteuid(pws->pw_uid))
363 panic("Could not set uid to specified value"); 368 panic("Could not set uid to specified value");
364 } 369 }
365 370