1 | /* |
---|
2 | * This file Copyright (C) 2010 Mnemosyne LLC |
---|
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: fdlimit.c 10928 2010-07-02 12:48:49Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef WIN32 |
---|
14 | #define HAVE_GETRLIMIT |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifdef HAVE_POSIX_FADVISE |
---|
18 | #ifdef _XOPEN_SOURCE |
---|
19 | #undef _XOPEN_SOURCE |
---|
20 | #endif |
---|
21 | #define _XOPEN_SOURCE 600 |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <assert.h> |
---|
25 | #include <errno.h> |
---|
26 | #include <inttypes.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <stdlib.h> |
---|
29 | #include <string.h> |
---|
30 | #ifdef SYS_DARWIN |
---|
31 | #include <fcntl.h> |
---|
32 | #endif |
---|
33 | |
---|
34 | #ifdef HAVE_FALLOCATE64 |
---|
35 | /* FIXME can't find the right #include voodoo to pick up the declaration.. */ |
---|
36 | extern int fallocate64( int fd, int mode, uint64_t offset, uint64_t len ); |
---|
37 | #endif |
---|
38 | |
---|
39 | #ifdef HAVE_XFS_XFS_H |
---|
40 | #include <xfs/xfs.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | #include <sys/types.h> |
---|
44 | #include <sys/stat.h> |
---|
45 | #ifdef HAVE_GETRLIMIT |
---|
46 | #include <sys/time.h> /* getrlimit */ |
---|
47 | #include <sys/resource.h> /* getrlimit */ |
---|
48 | #endif |
---|
49 | #include <fcntl.h> /* O_LARGEFILE posix_fadvise */ |
---|
50 | #include <unistd.h> |
---|
51 | |
---|
52 | #include "transmission.h" |
---|
53 | #include "fdlimit.h" |
---|
54 | #include "net.h" |
---|
55 | #include "platform.h" /* TR_PATH_MAX, TR_PATH_DELIMITER */ |
---|
56 | #include "session.h" |
---|
57 | #include "torrent.h" /* tr_isTorrent() */ |
---|
58 | #include "utils.h" |
---|
59 | |
---|
60 | #define dbgmsg( ... ) \ |
---|
61 | do { \ |
---|
62 | if( tr_deepLoggingIsActive( ) ) \ |
---|
63 | tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \ |
---|
64 | } while( 0 ) |
---|
65 | |
---|
66 | /** |
---|
67 | *** |
---|
68 | **/ |
---|
69 | |
---|
70 | struct tr_openfile |
---|
71 | { |
---|
72 | tr_bool isWritable; |
---|
73 | int torrentId; |
---|
74 | tr_file_index_t fileNum; |
---|
75 | char filename[TR_PATH_MAX]; |
---|
76 | int fd; |
---|
77 | uint64_t date; |
---|
78 | }; |
---|
79 | |
---|
80 | struct tr_fdInfo |
---|
81 | { |
---|
82 | int socketCount; |
---|
83 | int socketLimit; |
---|
84 | int publicSocketLimit; |
---|
85 | int openFileLimit; |
---|
86 | struct tr_openfile * openFiles; |
---|
87 | }; |
---|
88 | |
---|
89 | /*** |
---|
90 | **** |
---|
91 | **** Local Files |
---|
92 | **** |
---|
93 | ***/ |
---|
94 | |
---|
95 | #ifndef O_LARGEFILE |
---|
96 | #define O_LARGEFILE 0 |
---|
97 | #endif |
---|
98 | |
---|
99 | static tr_bool |
---|
100 | preallocateFileSparse( int fd, uint64_t length ) |
---|
101 | { |
---|
102 | const char zero = '\0'; |
---|
103 | tr_bool success = 0; |
---|
104 | |
---|
105 | if( !length ) |
---|
106 | success = TRUE; |
---|
107 | |
---|
108 | #ifdef HAVE_FALLOCATE64 |
---|
109 | if( !success ) /* fallocate64 is always preferred, so try it first */ |
---|
110 | success = !fallocate64( fd, 0, 0, length ); |
---|
111 | #endif |
---|
112 | |
---|
113 | if( !success ) /* fallback: the old-style seek-and-write */ |
---|
114 | success = ( lseek( fd, length-1, SEEK_SET ) != -1 ) |
---|
115 | && ( write( fd, &zero, 1 ) != -1 ) |
---|
116 | && ( ftruncate( fd, length ) != -1 ); |
---|
117 | |
---|
118 | return success; |
---|
119 | } |
---|
120 | |
---|
121 | static tr_bool |
---|
122 | preallocateFileFull( const char * filename, uint64_t length ) |
---|
123 | { |
---|
124 | tr_bool success = 0; |
---|
125 | |
---|
126 | #ifdef WIN32 |
---|
127 | |
---|
128 | HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 ); |
---|
129 | if( hFile != INVALID_HANDLE_VALUE ) |
---|
130 | { |
---|
131 | LARGE_INTEGER li; |
---|
132 | li.QuadPart = length; |
---|
133 | success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile ); |
---|
134 | CloseHandle( hFile ); |
---|
135 | } |
---|
136 | |
---|
137 | #else |
---|
138 | |
---|
139 | int flags = O_RDWR | O_CREAT | O_LARGEFILE; |
---|
140 | int fd = open( filename, flags, 0666 ); |
---|
141 | if( fd >= 0 ) |
---|
142 | { |
---|
143 | # ifdef HAVE_FALLOCATE64 |
---|
144 | if( !success ) |
---|
145 | { |
---|
146 | success = !fallocate64( fd, 0, 0, length ); |
---|
147 | } |
---|
148 | # endif |
---|
149 | # ifdef HAVE_XFS_XFS_H |
---|
150 | if( !success && platform_test_xfs_fd( fd ) ) |
---|
151 | { |
---|
152 | xfs_flock64_t fl; |
---|
153 | fl.l_whence = 0; |
---|
154 | fl.l_start = 0; |
---|
155 | fl.l_len = length; |
---|
156 | success = !xfsctl( NULL, fd, XFS_IOC_RESVSP64, &fl ); |
---|
157 | } |
---|
158 | # endif |
---|
159 | # ifdef SYS_DARWIN |
---|
160 | if( !success ) |
---|
161 | { |
---|
162 | fstore_t fst; |
---|
163 | fst.fst_flags = F_ALLOCATECONTIG; |
---|
164 | fst.fst_posmode = F_PEOFPOSMODE; |
---|
165 | fst.fst_offset = 0; |
---|
166 | fst.fst_length = length; |
---|
167 | fst.fst_bytesalloc = 0; |
---|
168 | success = !fcntl( fd, F_PREALLOCATE, &fst ); |
---|
169 | } |
---|
170 | # endif |
---|
171 | # ifdef HAVE_POSIX_FALLOCATE |
---|
172 | if( !success ) |
---|
173 | { |
---|
174 | success = !posix_fallocate( fd, 0, length ); |
---|
175 | } |
---|
176 | # endif |
---|
177 | |
---|
178 | if( !success ) /* if nothing else works, do it the old-fashioned way */ |
---|
179 | { |
---|
180 | uint8_t buf[ 4096 ]; |
---|
181 | memset( buf, 0, sizeof( buf ) ); |
---|
182 | success = TRUE; |
---|
183 | while ( success && ( length > 0 ) ) |
---|
184 | { |
---|
185 | const int thisPass = MIN( length, sizeof( buf ) ); |
---|
186 | success = write( fd, buf, thisPass ) == thisPass; |
---|
187 | length -= thisPass; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | close( fd ); |
---|
192 | } |
---|
193 | |
---|
194 | #endif |
---|
195 | |
---|
196 | return success; |
---|
197 | } |
---|
198 | |
---|
199 | /* Like pread and pwrite, except that the position is undefined afterwards. |
---|
200 | And of course they are not thread-safe. */ |
---|
201 | |
---|
202 | #ifdef SYS_DARWIN |
---|
203 | #define HAVE_PREAD |
---|
204 | #define HAVE_PWRITE |
---|
205 | #endif |
---|
206 | |
---|
207 | ssize_t |
---|
208 | tr_pread( int fd, void *buf, size_t count, off_t offset ) |
---|
209 | { |
---|
210 | #ifdef HAVE_PREAD |
---|
211 | return pread( fd, buf, count, offset ); |
---|
212 | #else |
---|
213 | const off_t lrc = lseek( fd, offset, SEEK_SET ); |
---|
214 | if( lrc < 0 ) |
---|
215 | return -1; |
---|
216 | return read( fd, buf, count ); |
---|
217 | #endif |
---|
218 | } |
---|
219 | |
---|
220 | ssize_t |
---|
221 | tr_pwrite( int fd, const void *buf, size_t count, off_t offset ) |
---|
222 | { |
---|
223 | #ifdef HAVE_PWRITE |
---|
224 | return pwrite( fd, buf, count, offset ); |
---|
225 | #else |
---|
226 | const off_t lrc = lseek( fd, offset, SEEK_SET ); |
---|
227 | if( lrc < 0 ) |
---|
228 | return -1; |
---|
229 | return write( fd, buf, count ); |
---|
230 | #endif |
---|
231 | } |
---|
232 | |
---|
233 | int |
---|
234 | tr_prefetch( int fd, off_t offset, size_t count ) |
---|
235 | { |
---|
236 | #ifdef HAVE_POSIX_FADVISE |
---|
237 | return posix_fadvise( fd, offset, count, POSIX_FADV_WILLNEED ); |
---|
238 | #elif defined(SYS_DARWIN) |
---|
239 | struct radvisory radv; |
---|
240 | radv.ra_offset = offset; |
---|
241 | radv.ra_count = count; |
---|
242 | return fcntl( fd, F_RDADVISE, &radv ); |
---|
243 | #else |
---|
244 | return 0; |
---|
245 | #endif |
---|
246 | } |
---|
247 | |
---|
248 | int |
---|
249 | tr_open_file_for_writing( const char * filename ) |
---|
250 | { |
---|
251 | int flags = O_WRONLY | O_CREAT; |
---|
252 | #ifdef O_BINARY |
---|
253 | flags |= O_BINARY; |
---|
254 | #endif |
---|
255 | #ifdef O_LARGEFILE |
---|
256 | flags |= O_LARGEFILE; |
---|
257 | #endif |
---|
258 | return open( filename, flags, 0666 ); |
---|
259 | } |
---|
260 | |
---|
261 | int |
---|
262 | tr_open_file_for_scanning( const char * filename ) |
---|
263 | { |
---|
264 | int fd; |
---|
265 | int flags; |
---|
266 | |
---|
267 | /* build the flags */ |
---|
268 | flags = O_RDONLY; |
---|
269 | #ifdef O_SEQUENTIAL |
---|
270 | flags |= O_SEQUENTIAL; |
---|
271 | #endif |
---|
272 | #ifdef O_BINARY |
---|
273 | flags |= O_BINARY; |
---|
274 | #endif |
---|
275 | #ifdef O_LARGEFILE |
---|
276 | flags |= O_LARGEFILE; |
---|
277 | #endif |
---|
278 | |
---|
279 | /* open the file */ |
---|
280 | fd = open( filename, flags, 0666 ); |
---|
281 | if( fd >= 0 ) |
---|
282 | { |
---|
283 | /* Set hints about the lookahead buffer and caching. It's okay |
---|
284 | for these to fail silently, so don't let them affect errno */ |
---|
285 | const int err = errno; |
---|
286 | #ifdef HAVE_POSIX_FADVISE |
---|
287 | posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL ); |
---|
288 | #endif |
---|
289 | #ifdef SYS_DARWIN |
---|
290 | fcntl( fd, F_NOCACHE, 1 ); |
---|
291 | fcntl( fd, F_RDAHEAD, 1 ); |
---|
292 | #endif |
---|
293 | errno = err; |
---|
294 | } |
---|
295 | |
---|
296 | return fd; |
---|
297 | } |
---|
298 | |
---|
299 | void |
---|
300 | tr_close_file( int fd ) |
---|
301 | { |
---|
302 | #if defined(HAVE_POSIX_FADVISE) |
---|
303 | /* Set hint about not caching this file. |
---|
304 | It's okay for this to fail silently, so don't let it affect errno */ |
---|
305 | const int err = errno; |
---|
306 | posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED ); |
---|
307 | errno = err; |
---|
308 | #endif |
---|
309 | close( fd ); |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | * returns 0 on success, or an errno value on failure. |
---|
314 | * errno values include ENOENT if the parent folder doesn't exist, |
---|
315 | * plus the errno values set by tr_mkdirp() and open(). |
---|
316 | */ |
---|
317 | static int |
---|
318 | TrOpenFile( tr_session * session, |
---|
319 | int i, |
---|
320 | const char * filename, |
---|
321 | tr_bool doWrite, |
---|
322 | tr_preallocation_mode preallocationMode, |
---|
323 | uint64_t desiredFileSize ) |
---|
324 | { |
---|
325 | int flags; |
---|
326 | struct stat sb; |
---|
327 | tr_bool alreadyExisted; |
---|
328 | struct tr_openfile * file; |
---|
329 | |
---|
330 | assert( tr_isSession( session ) ); |
---|
331 | assert( session->fdInfo != NULL ); |
---|
332 | |
---|
333 | file = &session->fdInfo->openFiles[i]; |
---|
334 | |
---|
335 | /* create subfolders, if any */ |
---|
336 | if( doWrite ) |
---|
337 | { |
---|
338 | char * dir = tr_dirname( filename ); |
---|
339 | const int err = tr_mkdirp( dir, 0777 ) ? errno : 0; |
---|
340 | if( err ) { |
---|
341 | tr_err( _( "Couldn't create \"%1$s\": %2$s" ), dir, tr_strerror( err ) ); |
---|
342 | tr_free( dir ); |
---|
343 | return err; |
---|
344 | } |
---|
345 | tr_free( dir ); |
---|
346 | } |
---|
347 | |
---|
348 | alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode ); |
---|
349 | |
---|
350 | if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_FULL ) ) |
---|
351 | if( preallocateFileFull( filename, desiredFileSize ) ) |
---|
352 | tr_dbg( _( "Preallocated file \"%s\"" ), filename ); |
---|
353 | |
---|
354 | /* open the file */ |
---|
355 | flags = doWrite ? ( O_RDWR | O_CREAT ) : O_RDONLY; |
---|
356 | #ifdef O_SEQUENTIAL |
---|
357 | flags |= O_SEQUENTIAL; |
---|
358 | #endif |
---|
359 | #ifdef O_LARGEFILE |
---|
360 | flags |= O_LARGEFILE; |
---|
361 | #endif |
---|
362 | #ifdef WIN32 |
---|
363 | flags |= O_BINARY; |
---|
364 | #endif |
---|
365 | file->fd = open( filename, flags, 0666 ); |
---|
366 | if( file->fd == -1 ) |
---|
367 | { |
---|
368 | const int err = errno; |
---|
369 | tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror( err ) ); |
---|
370 | return err; |
---|
371 | } |
---|
372 | |
---|
373 | /* If the file already exists and it's too large, truncate it. |
---|
374 | * This is a fringe case that happens if a torrent's been updated |
---|
375 | * and one of the updated torrent's files is smaller. |
---|
376 | * http://trac.transmissionbt.com/ticket/2228 |
---|
377 | * https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249 |
---|
378 | */ |
---|
379 | if( alreadyExisted && ( desiredFileSize < (uint64_t)sb.st_size ) ) |
---|
380 | ftruncate( file->fd, desiredFileSize ); |
---|
381 | |
---|
382 | if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_SPARSE ) ) |
---|
383 | preallocateFileSparse( file->fd, desiredFileSize ); |
---|
384 | |
---|
385 | #ifdef HAVE_POSIX_FADVISE |
---|
386 | /* this doubles the OS level readahead buffer, which in practice |
---|
387 | * turns out to be a good thing, because many (most?) clients request |
---|
388 | * chunks of blocks in order. |
---|
389 | * It's okay for this to fail silently, so don't let it affect errno */ |
---|
390 | { |
---|
391 | const int err = errno; |
---|
392 | posix_fadvise( file->fd, 0, 0, POSIX_FADV_SEQUENTIAL ); |
---|
393 | errno = err; |
---|
394 | } |
---|
395 | #endif |
---|
396 | |
---|
397 | #if defined( SYS_DARWIN ) |
---|
398 | /** |
---|
399 | * 1. Enable readahead for reasons described above w/POSIX_FADV_SEQUENTIAL. |
---|
400 | * 2. Disable OS-level caching due to user reports of adverse effects of |
---|
401 | * excessive inactive memory. |
---|
402 | * It's okay for this to fail silently, so don't let it affect errno |
---|
403 | */ |
---|
404 | { |
---|
405 | const int err = errno; |
---|
406 | fcntl( file->fd, F_NOCACHE, 1 ); |
---|
407 | fcntl( file->fd, F_RDAHEAD, 1 ); |
---|
408 | errno = err; |
---|
409 | } |
---|
410 | #endif |
---|
411 | |
---|
412 | return 0; |
---|
413 | } |
---|
414 | |
---|
415 | static inline tr_bool |
---|
416 | fileIsOpen( const struct tr_openfile * o ) |
---|
417 | { |
---|
418 | return o->fd >= 0; |
---|
419 | } |
---|
420 | |
---|
421 | static void |
---|
422 | TrCloseFile( struct tr_openfile * o ) |
---|
423 | { |
---|
424 | assert( o != NULL ); |
---|
425 | assert( fileIsOpen( o ) ); |
---|
426 | |
---|
427 | tr_close_file( o->fd ); |
---|
428 | o->fd = -1; |
---|
429 | } |
---|
430 | |
---|
431 | int |
---|
432 | tr_fdFileGetCached( tr_session * session, |
---|
433 | int torrentId, |
---|
434 | tr_file_index_t fileNum, |
---|
435 | tr_bool doWrite ) |
---|
436 | { |
---|
437 | struct tr_openfile * match = NULL; |
---|
438 | struct tr_fdInfo * gFd; |
---|
439 | |
---|
440 | assert( tr_isSession( session ) ); |
---|
441 | assert( session->fdInfo != NULL ); |
---|
442 | assert( torrentId > 0 ); |
---|
443 | assert( tr_isBool( doWrite ) ); |
---|
444 | |
---|
445 | gFd = session->fdInfo; |
---|
446 | |
---|
447 | /* is it already open? */ |
---|
448 | { |
---|
449 | int i; |
---|
450 | struct tr_openfile * o; |
---|
451 | for( i=0; i<gFd->openFileLimit; ++i ) |
---|
452 | { |
---|
453 | o = &gFd->openFiles[i]; |
---|
454 | |
---|
455 | if( torrentId != o->torrentId ) |
---|
456 | continue; |
---|
457 | if( fileNum != o->fileNum ) |
---|
458 | continue; |
---|
459 | if( !fileIsOpen( o ) ) |
---|
460 | continue; |
---|
461 | |
---|
462 | match = o; |
---|
463 | break; |
---|
464 | } |
---|
465 | } |
---|
466 | |
---|
467 | if( ( match != NULL ) && ( !doWrite || match->isWritable ) ) |
---|
468 | { |
---|
469 | match->date = tr_date( ); |
---|
470 | return match->fd; |
---|
471 | } |
---|
472 | |
---|
473 | return -1; |
---|
474 | } |
---|
475 | |
---|
476 | /* returns an fd on success, or a -1 on failure and sets errno */ |
---|
477 | int |
---|
478 | tr_fdFileCheckout( tr_session * session, |
---|
479 | int torrentId, |
---|
480 | tr_file_index_t fileNum, |
---|
481 | const char * filename, |
---|
482 | tr_bool doWrite, |
---|
483 | tr_preallocation_mode preallocationMode, |
---|
484 | uint64_t desiredFileSize ) |
---|
485 | { |
---|
486 | int i, winner = -1; |
---|
487 | struct tr_fdInfo * gFd; |
---|
488 | struct tr_openfile * o; |
---|
489 | |
---|
490 | assert( tr_isSession( session ) ); |
---|
491 | assert( session->fdInfo != NULL ); |
---|
492 | assert( torrentId > 0 ); |
---|
493 | assert( filename && *filename ); |
---|
494 | assert( tr_isBool( doWrite ) ); |
---|
495 | |
---|
496 | gFd = session->fdInfo; |
---|
497 | |
---|
498 | dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' ); |
---|
499 | |
---|
500 | /* is it already open? */ |
---|
501 | for( i=0; i<gFd->openFileLimit; ++i ) |
---|
502 | { |
---|
503 | o = &gFd->openFiles[i]; |
---|
504 | |
---|
505 | if( torrentId != o->torrentId ) |
---|
506 | continue; |
---|
507 | if( fileNum != o->fileNum ) |
---|
508 | continue; |
---|
509 | if( !fileIsOpen( o ) ) |
---|
510 | continue; |
---|
511 | |
---|
512 | if( doWrite && !o->isWritable ) |
---|
513 | { |
---|
514 | dbgmsg( "found it! it's open and available, but isn't writable. closing..." ); |
---|
515 | TrCloseFile( o ); |
---|
516 | break; |
---|
517 | } |
---|
518 | |
---|
519 | dbgmsg( "found it! it's ready for use!" ); |
---|
520 | winner = i; |
---|
521 | break; |
---|
522 | } |
---|
523 | |
---|
524 | dbgmsg( "it's not already open. looking for an open slot or an old file." ); |
---|
525 | while( winner < 0 ) |
---|
526 | { |
---|
527 | uint64_t date = tr_date( ) + 1; |
---|
528 | |
---|
529 | /* look for the file that's been open longest */ |
---|
530 | for( i=0; i<gFd->openFileLimit; ++i ) |
---|
531 | { |
---|
532 | o = &gFd->openFiles[i]; |
---|
533 | |
---|
534 | if( !fileIsOpen( o ) ) |
---|
535 | { |
---|
536 | winner = i; |
---|
537 | dbgmsg( "found an empty slot in %d", winner ); |
---|
538 | break; |
---|
539 | } |
---|
540 | |
---|
541 | if( date > o->date ) |
---|
542 | { |
---|
543 | date = o->date; |
---|
544 | winner = i; |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | assert( winner >= 0 ); |
---|
549 | |
---|
550 | if( fileIsOpen( &gFd->openFiles[winner] ) ) |
---|
551 | { |
---|
552 | dbgmsg( "closing file \"%s\"", gFd->openFiles[winner].filename ); |
---|
553 | TrCloseFile( &gFd->openFiles[winner] ); |
---|
554 | } |
---|
555 | } |
---|
556 | |
---|
557 | assert( winner >= 0 ); |
---|
558 | o = &gFd->openFiles[winner]; |
---|
559 | if( !fileIsOpen( o ) ) |
---|
560 | { |
---|
561 | const int err = TrOpenFile( session, winner, filename, doWrite, |
---|
562 | preallocationMode, desiredFileSize ); |
---|
563 | if( err ) { |
---|
564 | errno = err; |
---|
565 | return -1; |
---|
566 | } |
---|
567 | |
---|
568 | dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner, |
---|
569 | doWrite ? 'y' : 'n' ); |
---|
570 | tr_strlcpy( o->filename, filename, sizeof( o->filename ) ); |
---|
571 | o->isWritable = doWrite; |
---|
572 | } |
---|
573 | |
---|
574 | dbgmsg( "checking out '%s' in slot %d", filename, winner ); |
---|
575 | o->torrentId = torrentId; |
---|
576 | o->fileNum = fileNum; |
---|
577 | o->date = tr_date( ); |
---|
578 | return o->fd; |
---|
579 | } |
---|
580 | |
---|
581 | void |
---|
582 | tr_fdFileClose( tr_session * session, |
---|
583 | const tr_torrent * tor, |
---|
584 | tr_file_index_t fileNum ) |
---|
585 | { |
---|
586 | struct tr_openfile * o; |
---|
587 | struct tr_fdInfo * gFd; |
---|
588 | const struct tr_openfile * end; |
---|
589 | const int torrentId = tr_torrentId( tor ); |
---|
590 | |
---|
591 | assert( tr_isSession( session ) ); |
---|
592 | assert( session->fdInfo != NULL ); |
---|
593 | assert( tr_isTorrent( tor ) ); |
---|
594 | assert( fileNum < tor->info.fileCount ); |
---|
595 | |
---|
596 | gFd = session->fdInfo; |
---|
597 | |
---|
598 | for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o ) |
---|
599 | { |
---|
600 | if( torrentId != o->torrentId ) |
---|
601 | continue; |
---|
602 | if( fileNum != o->fileNum ) |
---|
603 | continue; |
---|
604 | if( !fileIsOpen( o ) ) |
---|
605 | continue; |
---|
606 | |
---|
607 | dbgmsg( "tr_fdFileClose closing \"%s\"", o->filename ); |
---|
608 | TrCloseFile( o ); |
---|
609 | } |
---|
610 | } |
---|
611 | |
---|
612 | void |
---|
613 | tr_fdTorrentClose( tr_session * session, int torrentId ) |
---|
614 | { |
---|
615 | assert( tr_isSession( session ) ); |
---|
616 | |
---|
617 | if( session->fdInfo != NULL ) |
---|
618 | { |
---|
619 | struct tr_openfile * o; |
---|
620 | const struct tr_openfile * end; |
---|
621 | struct tr_fdInfo * gFd = session->fdInfo; |
---|
622 | |
---|
623 | for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o ) |
---|
624 | if( fileIsOpen( o ) && ( o->torrentId == torrentId ) ) |
---|
625 | TrCloseFile( o ); |
---|
626 | } |
---|
627 | } |
---|
628 | |
---|
629 | /*** |
---|
630 | **** |
---|
631 | **** Sockets |
---|
632 | **** |
---|
633 | ***/ |
---|
634 | |
---|
635 | int |
---|
636 | tr_fdSocketCreate( tr_session * session, int domain, int type ) |
---|
637 | { |
---|
638 | int s = -1; |
---|
639 | struct tr_fdInfo * gFd; |
---|
640 | |
---|
641 | assert( tr_isSession( session ) ); |
---|
642 | assert( session->fdInfo != NULL ); |
---|
643 | |
---|
644 | gFd = session->fdInfo; |
---|
645 | |
---|
646 | if( gFd->socketCount < gFd->socketLimit ) |
---|
647 | if( ( s = socket( domain, type, 0 ) ) < 0 ) |
---|
648 | { |
---|
649 | if( sockerrno != EAFNOSUPPORT ) |
---|
650 | tr_err( _( "Couldn't create socket: %s" ), |
---|
651 | tr_strerror( sockerrno ) ); |
---|
652 | } |
---|
653 | |
---|
654 | if( s > -1 ) |
---|
655 | ++gFd->socketCount; |
---|
656 | |
---|
657 | assert( gFd->socketCount >= 0 ); |
---|
658 | |
---|
659 | if( s >= 0 ) |
---|
660 | { |
---|
661 | static tr_bool buf_logged = FALSE; |
---|
662 | if( !buf_logged ) |
---|
663 | { |
---|
664 | int i; |
---|
665 | socklen_t size = sizeof( int ); |
---|
666 | buf_logged = TRUE; |
---|
667 | getsockopt( s, SOL_SOCKET, SO_SNDBUF, &i, &size ); |
---|
668 | tr_dbg( "SO_SNDBUF size is %d", i ); |
---|
669 | getsockopt( s, SOL_SOCKET, SO_RCVBUF, &i, &size ); |
---|
670 | tr_dbg( "SO_RCVBUF size is %d", i ); |
---|
671 | } |
---|
672 | } |
---|
673 | |
---|
674 | return s; |
---|
675 | } |
---|
676 | |
---|
677 | int |
---|
678 | tr_fdSocketAccept( tr_session * session, |
---|
679 | int b, |
---|
680 | tr_address * addr, |
---|
681 | tr_port * port ) |
---|
682 | { |
---|
683 | int s; |
---|
684 | unsigned int len; |
---|
685 | struct tr_fdInfo * gFd; |
---|
686 | struct sockaddr_storage sock; |
---|
687 | |
---|
688 | assert( tr_isSession( session ) ); |
---|
689 | assert( session->fdInfo != NULL ); |
---|
690 | assert( addr ); |
---|
691 | assert( port ); |
---|
692 | |
---|
693 | gFd = session->fdInfo; |
---|
694 | |
---|
695 | len = sizeof( struct sockaddr_storage ); |
---|
696 | s = accept( b, (struct sockaddr *) &sock, &len ); |
---|
697 | |
---|
698 | if( ( s >= 0 ) && gFd->socketCount > gFd->socketLimit ) |
---|
699 | { |
---|
700 | tr_netCloseSocket( s ); |
---|
701 | s = -1; |
---|
702 | } |
---|
703 | |
---|
704 | if( s >= 0 ) |
---|
705 | { |
---|
706 | /* "The ss_family field of the sockaddr_storage structure will always |
---|
707 | * align with the family field of any protocol-specific structure." */ |
---|
708 | if( sock.ss_family == AF_INET ) |
---|
709 | { |
---|
710 | struct sockaddr_in *si; |
---|
711 | union { struct sockaddr_storage dummy; struct sockaddr_in si; } s; |
---|
712 | s.dummy = sock; |
---|
713 | si = &s.si; |
---|
714 | addr->type = TR_AF_INET; |
---|
715 | addr->addr.addr4.s_addr = si->sin_addr.s_addr; |
---|
716 | *port = si->sin_port; |
---|
717 | } |
---|
718 | else |
---|
719 | { |
---|
720 | struct sockaddr_in6 *si; |
---|
721 | union { struct sockaddr_storage dummy; struct sockaddr_in6 si; } s; |
---|
722 | s.dummy = sock; |
---|
723 | si = &s.si; |
---|
724 | addr->type = TR_AF_INET6; |
---|
725 | addr->addr.addr6 = si->sin6_addr; |
---|
726 | *port = si->sin6_port; |
---|
727 | } |
---|
728 | ++gFd->socketCount; |
---|
729 | } |
---|
730 | |
---|
731 | return s; |
---|
732 | } |
---|
733 | |
---|
734 | void |
---|
735 | tr_fdSocketClose( tr_session * session, int fd ) |
---|
736 | { |
---|
737 | assert( tr_isSession( session ) ); |
---|
738 | |
---|
739 | if( session->fdInfo != NULL ) |
---|
740 | { |
---|
741 | struct tr_fdInfo * gFd = session->fdInfo; |
---|
742 | |
---|
743 | if( fd >= 0 ) |
---|
744 | { |
---|
745 | tr_netCloseSocket( fd ); |
---|
746 | --gFd->socketCount; |
---|
747 | } |
---|
748 | |
---|
749 | assert( gFd->socketCount >= 0 ); |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | /*** |
---|
754 | **** |
---|
755 | **** Startup / Shutdown |
---|
756 | **** |
---|
757 | ***/ |
---|
758 | |
---|
759 | static void |
---|
760 | ensureSessionFdInfoExists( tr_session * session ) |
---|
761 | { |
---|
762 | assert( tr_isSession( session ) ); |
---|
763 | |
---|
764 | if( session->fdInfo == NULL ) |
---|
765 | session->fdInfo = tr_new0( struct tr_fdInfo, 1 ); |
---|
766 | } |
---|
767 | |
---|
768 | void |
---|
769 | tr_fdClose( tr_session * session ) |
---|
770 | { |
---|
771 | struct tr_fdInfo * gFd; |
---|
772 | struct tr_openfile * o; |
---|
773 | const struct tr_openfile * end; |
---|
774 | |
---|
775 | assert( tr_isSession( session ) ); |
---|
776 | assert( session->fdInfo != NULL ); |
---|
777 | |
---|
778 | gFd = session->fdInfo; |
---|
779 | |
---|
780 | for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o ) |
---|
781 | if( fileIsOpen( o ) ) |
---|
782 | TrCloseFile( o ); |
---|
783 | |
---|
784 | tr_free( gFd->openFiles ); |
---|
785 | tr_free( gFd ); |
---|
786 | session->fdInfo = NULL; |
---|
787 | } |
---|
788 | |
---|
789 | /*** |
---|
790 | **** |
---|
791 | ***/ |
---|
792 | |
---|
793 | void |
---|
794 | tr_fdSetFileLimit( tr_session * session, int limit ) |
---|
795 | { |
---|
796 | struct tr_fdInfo * gFd; |
---|
797 | |
---|
798 | ensureSessionFdInfoExists( session ); |
---|
799 | |
---|
800 | gFd = session->fdInfo; |
---|
801 | |
---|
802 | if( gFd->openFileLimit != limit ) |
---|
803 | { |
---|
804 | int i; |
---|
805 | struct tr_openfile * o; |
---|
806 | const struct tr_openfile * end; |
---|
807 | |
---|
808 | /* close any files we've got open */ |
---|
809 | for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o ) |
---|
810 | if( fileIsOpen( o ) ) |
---|
811 | TrCloseFile( o ); |
---|
812 | |
---|
813 | /* rebuild the openFiles array */ |
---|
814 | tr_free( gFd->openFiles ); |
---|
815 | gFd->openFiles = tr_new0( struct tr_openfile, limit ); |
---|
816 | gFd->openFileLimit = limit; |
---|
817 | for( i=0; i<gFd->openFileLimit; ++i ) |
---|
818 | gFd->openFiles[i].fd = -1; |
---|
819 | } |
---|
820 | } |
---|
821 | |
---|
822 | int |
---|
823 | tr_fdGetFileLimit( const tr_session * session ) |
---|
824 | { |
---|
825 | return session && session->fdInfo ? session->fdInfo->openFileLimit : -1; |
---|
826 | } |
---|
827 | |
---|
828 | void |
---|
829 | tr_fdSetPeerLimit( tr_session * session, int socketLimit ) |
---|
830 | { |
---|
831 | struct tr_fdInfo * gFd; |
---|
832 | |
---|
833 | ensureSessionFdInfoExists( session ); |
---|
834 | |
---|
835 | gFd = session->fdInfo; |
---|
836 | |
---|
837 | #ifdef HAVE_GETRLIMIT |
---|
838 | { |
---|
839 | struct rlimit rlim; |
---|
840 | const int NOFILE_BUFFER = 512; |
---|
841 | const int open_max = sysconf( _SC_OPEN_MAX ); |
---|
842 | getrlimit( RLIMIT_NOFILE, &rlim ); |
---|
843 | rlim.rlim_cur = MAX( 1024, open_max ); |
---|
844 | rlim.rlim_cur = MIN( rlim.rlim_cur, rlim.rlim_max ); |
---|
845 | setrlimit( RLIMIT_NOFILE, &rlim ); |
---|
846 | tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur ); |
---|
847 | gFd->socketLimit = MIN( socketLimit, (int)rlim.rlim_cur - NOFILE_BUFFER ); |
---|
848 | } |
---|
849 | #else |
---|
850 | gFd->socketLimit = socketLimit; |
---|
851 | #endif |
---|
852 | gFd->publicSocketLimit = socketLimit; |
---|
853 | |
---|
854 | tr_dbg( "socket limit is %d", (int)gFd->socketLimit ); |
---|
855 | } |
---|
856 | |
---|
857 | int |
---|
858 | tr_fdGetPeerLimit( const tr_session * session ) |
---|
859 | { |
---|
860 | return session && session->fdInfo ? session->fdInfo->publicSocketLimit : -1; |
---|
861 | } |
---|