source: trunk/libtransmission/transmission.c @ 2765

Last change on this file since 2765 was 2765, checked in by charles, 15 years ago

avoid a race condition between msg_init and the libevent thread's tr_dbg() call

  • Property svn:keywords set to Date Rev Author Id
File size: 8.3 KB
Line 
1/******************************************************************************
2 * $Id: transmission.c 2765 2007-08-15 03:26:55Z charles $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include <assert.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include <signal.h>
31#include <sys/types.h> /* stat */
32#include <sys/stat.h> /* stat */
33#include <unistd.h> /* stat */
34#include <dirent.h> /* opendir */
35
36#include <event.h>
37
38#include "transmission.h"
39#include "fdlimit.h"
40#include "list.h"
41#include "net.h"
42#include "platform.h"
43#include "ratecontrol.h"
44#include "shared.h"
45#include "utils.h"
46
47/* Generate a peer id : "-TRxyzb-" + 12 random alphanumeric
48   characters, where x is the major version number, y is the
49   minor version number, z is the maintenance number, and b
50   designates beta (Azureus-style) */
51void
52tr_peerIdNew ( char * buf, int buflen )
53{
54    int i;
55    assert( buflen == TR_ID_LEN + 1 );
56
57    snprintf( buf, TR_ID_LEN, "%s", PEERID_PREFIX );
58    assert( strlen(buf) == 8 );
59    for( i=8; i<TR_ID_LEN; ++i ) {
60        const int r = tr_rand( 36 );
61        buf[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
62    }
63    buf[TR_ID_LEN] = '\0';
64}
65
66static void
67libeventThreadFunc( void * unused UNUSED )
68{
69    tr_dbg( "libevent thread starting" );
70    event_init( );
71    for ( ;; )
72    {
73        event_dispatch( );
74        tr_wait( 50 ); /* 1/20th of a second */
75    }
76    tr_dbg( "libevent thread exiting" );
77}
78
79
80/***********************************************************************
81 * tr_init
82 ***********************************************************************
83 * Allocates a tr_handle_t structure and initializes a few things
84 **********************************************************************/
85tr_handle_t * tr_init( const char * tag )
86{
87    tr_handle_t * h;
88    int           i;
89
90    tr_msgInit();
91    tr_threadNew( libeventThreadFunc, NULL, "libeventThreadFunc" );
92    tr_netInit();
93    tr_netResolveThreadInit();
94
95    h = calloc( 1, sizeof( tr_handle_t ) );
96    if( NULL == h )
97    {
98        return NULL;
99    }
100
101    h->tag = strdup( tag );
102    if( NULL == h->tag )
103    {
104        free( h );
105        return NULL;
106    }
107
108    /* Random key */
109    for( i=0; i < TR_KEY_LEN; ++i )
110    {
111        const int r = tr_rand( 36 );
112        h->key[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
113    }
114
115    /* Azureus identity */
116    for( i=0; i < TR_AZ_ID_LEN; ++i )
117    {
118        h->azId[i] = tr_rand( 0xff );
119    }
120
121#ifndef WIN32
122    /* Don't exit when writing on a broken socket */
123    signal( SIGPIPE, SIG_IGN );
124#endif
125
126    /* Initialize rate and file descripts controls */
127    h->upload   = tr_rcInit();
128    h->download = tr_rcInit();
129
130    tr_fdInit();
131    h->shared = tr_sharedInit( h );
132
133    tr_inf( TR_NAME " " LONG_VERSION_STRING " started" );
134
135    return h;
136}
137
138/***********************************************************************
139 * tr_setBindPort
140 ***********************************************************************
141 *
142 **********************************************************************/
143void tr_setBindPort( tr_handle_t * h, int port )
144{
145    h->isPortSet = 1;
146    tr_sharedSetPort( h->shared, port );
147}
148
149void tr_natTraversalEnable( tr_handle_t * h, int enable )
150{
151    tr_sharedLock( h->shared );
152    tr_sharedTraversalEnable( h->shared, enable );
153    tr_sharedUnlock( h->shared );
154}
155
156tr_handle_status_t * tr_handleStatus( tr_handle_t * h )
157{
158    tr_handle_status_t * s;
159
160    h->statCur = ( h->statCur + 1 ) % 2;
161    s = &h->stats[h->statCur];
162
163    tr_sharedLock( h->shared );
164
165    s->natTraversalStatus = tr_sharedTraversalStatus( h->shared );
166    s->publicPort = tr_sharedGetPublicPort( h->shared );
167
168    tr_sharedUnlock( h->shared );
169
170    return s;
171}
172
173/***
174****
175***/
176
177void
178tr_setUseGlobalSpeedLimit( tr_handle_t  * h,
179                           int            up_or_down,
180                           int            use_flag )
181{
182    char * ch = up_or_down==TR_UP ? &h->useUploadLimit
183                                  : &h->useDownloadLimit;
184    *ch = use_flag;
185}
186
187void
188tr_setGlobalSpeedLimit( tr_handle_t  * h,
189                        int            up_or_down,
190                        int            KiB_sec )
191{
192    if( up_or_down == TR_DOWN )
193        tr_rcSetLimit( h->download, KiB_sec );
194    else {
195        tr_rcSetLimit( h->upload, KiB_sec );
196        tr_sharedSetLimit( h->shared, KiB_sec );
197    }
198}
199
200void
201tr_getGlobalSpeedLimit( tr_handle_t  * h,
202                        int            up_or_down,
203                        int          * setme_enabled,
204                        int          * setme_KiBsec )
205{
206    if( setme_enabled != NULL )
207       *setme_enabled = up_or_down==TR_UP ? h->useUploadLimit
208                                          : h->useDownloadLimit;
209    if( setme_KiBsec != NULL )
210       *setme_KiBsec = tr_rcGetLimit( up_or_down==TR_UP ? h->upload
211                                                        : h->download );
212}
213
214
215void tr_torrentRates( tr_handle_t * h, float * dl, float * ul )
216{
217    tr_torrent_t * tor;
218
219    *dl = 0.0;
220    *ul = 0.0;
221    tr_sharedLock( h->shared );
222    for( tor = h->torrentList; tor; tor = tor->next )
223    {
224        tr_torrentReaderLock( tor );
225        if( tor->cpStatus == TR_CP_INCOMPLETE )
226            *dl += tr_rcRate( tor->download );
227        *ul += tr_rcRate( tor->upload );
228        tr_torrentReaderUnlock( tor );
229    }
230    tr_sharedUnlock( h->shared );
231}
232
233int tr_torrentCount( tr_handle_t * h )
234{
235    return h->torrentCount;
236}
237
238void tr_torrentIterate( tr_handle_t * h, tr_callback_t func, void * d )
239{
240    tr_torrent_t * tor, * next;
241
242    for( tor = h->torrentList; tor; tor = next )
243    {
244        next = tor->next;
245        func( tor, d );
246    }
247}
248
249void tr_close( tr_handle_t * h )
250{
251    tr_rcClose( h->upload );
252    tr_rcClose( h->download );
253   
254    tr_sharedClose( h->shared );
255    tr_fdClose();
256    free( h->tag );
257    free( h );
258
259    tr_netResolveThreadClose();
260}
261
262tr_torrent_t **
263tr_loadTorrents ( tr_handle_t   * h,
264                  const char    * destination,
265                  int             flags,
266                  int          * setmeCount )
267{
268    int i, n = 0;
269    struct stat sb;
270    DIR * odir = NULL;
271    const char * torrentDir = tr_getTorrentsDirectory( );
272    tr_torrent_t ** torrents;
273    tr_list_t *l=NULL, *list=NULL;
274
275    if( !stat( torrentDir, &sb )
276        && S_ISDIR( sb.st_mode )
277        && (( odir = opendir ( torrentDir ) )) )
278    {
279        struct dirent *d;
280        for (d = readdir( odir ); d!=NULL; d=readdir( odir ) )
281        {
282            if( d->d_name && d->d_name[0]!='.' ) /* skip dotfiles, ., and .. */
283            {
284                tr_torrent_t * tor;
285                char path[MAX_PATH_LENGTH];
286                tr_buildPath( path, sizeof(path), torrentDir, d->d_name, NULL );
287                tor = tr_torrentInit( h, path, destination, flags, NULL );
288                if( tor != NULL ) {
289                    tr_list_append( &list, tor );
290                    //fprintf (stderr, "#%d - %s\n", n, tor->info.name );
291                    n++;
292                }
293            }
294        }
295        closedir( odir );
296    }
297
298    torrents = tr_new( tr_torrent_t*, n );
299    for( i=0, l=list; l!=NULL; l=l->next )
300        torrents[i++] = (tr_torrent_t*) l->data;
301    assert( i==n );
302
303    tr_list_free( &list );
304
305    *setmeCount = n;
306    tr_inf( "Loaded %d torrents from disk", *setmeCount );
307    return torrents;
308}
Note: See TracBrowser for help on using the repository browser.