1 | /* |
---|
2 | * This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.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: platform.c 6896 2008-10-14 03:03:29Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifdef __BEOS__ |
---|
14 | #include <signal.h> |
---|
15 | #include <fs_info.h> |
---|
16 | #include <FindDirectory.h> |
---|
17 | #include <kernel/OS.h> |
---|
18 | #define BEOS_MAX_THREADS 256 |
---|
19 | #elif defined( WIN32 ) |
---|
20 | #include <windows.h> |
---|
21 | #include <shlobj.h> /* for CSIDL_APPDATA, CSIDL_MYDOCUMENTS */ |
---|
22 | #else |
---|
23 | #ifdef SYS_DARWIN |
---|
24 | #include <CoreFoundation/CoreFoundation.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #define _XOPEN_SOURCE 500 /* needed for recursive locks. */ |
---|
28 | #ifndef __USE_UNIX98 |
---|
29 | #define __USE_UNIX98 /* some older Linuxes need it spelt out for them */ |
---|
30 | #endif |
---|
31 | #include <pthread.h> |
---|
32 | #endif |
---|
33 | |
---|
34 | #include <assert.h> |
---|
35 | #include <stdio.h> |
---|
36 | #include <stdlib.h> |
---|
37 | #include <string.h> |
---|
38 | |
---|
39 | #include <sys/stat.h> |
---|
40 | #include <sys/types.h> |
---|
41 | #ifdef WIN32 |
---|
42 | #include <libgen.h> |
---|
43 | #endif |
---|
44 | #include <dirent.h> |
---|
45 | #include <fcntl.h> |
---|
46 | #include <unistd.h> /* getuid getpid close */ |
---|
47 | |
---|
48 | #include "transmission.h" |
---|
49 | #include "list.h" |
---|
50 | #include "platform.h" |
---|
51 | #include "utils.h" |
---|
52 | |
---|
53 | /*** |
---|
54 | **** THREADS |
---|
55 | ***/ |
---|
56 | |
---|
57 | #ifdef __BEOS__ |
---|
58 | typedef thread_id tr_thread_id; |
---|
59 | #elif defined( WIN32 ) |
---|
60 | typedef DWORD tr_thread_id; |
---|
61 | #else |
---|
62 | typedef pthread_t tr_thread_id; |
---|
63 | #endif |
---|
64 | |
---|
65 | static tr_thread_id |
---|
66 | tr_getCurrentThread( void ) |
---|
67 | { |
---|
68 | #ifdef __BEOS__ |
---|
69 | return find_thread( NULL ); |
---|
70 | #elif defined( WIN32 ) |
---|
71 | return GetCurrentThreadId( ); |
---|
72 | #else |
---|
73 | return pthread_self( ); |
---|
74 | #endif |
---|
75 | } |
---|
76 | |
---|
77 | static int |
---|
78 | tr_areThreadsEqual( tr_thread_id a, |
---|
79 | tr_thread_id b ) |
---|
80 | { |
---|
81 | #ifdef __BEOS__ |
---|
82 | return a == b; |
---|
83 | #elif defined( WIN32 ) |
---|
84 | return a == b; |
---|
85 | #else |
---|
86 | return pthread_equal( a, b ); |
---|
87 | #endif |
---|
88 | } |
---|
89 | |
---|
90 | struct tr_thread |
---|
91 | { |
---|
92 | void ( * func )( void * ); |
---|
93 | void * arg; |
---|
94 | tr_thread_id thread; |
---|
95 | #ifdef WIN32 |
---|
96 | HANDLE thread_handle; |
---|
97 | #endif |
---|
98 | }; |
---|
99 | |
---|
100 | int |
---|
101 | tr_amInThread( const tr_thread * t ) |
---|
102 | { |
---|
103 | return tr_areThreadsEqual( tr_getCurrentThread( ), t->thread ); |
---|
104 | } |
---|
105 | |
---|
106 | #ifdef WIN32 |
---|
107 | #define ThreadFuncReturnType unsigned WINAPI |
---|
108 | #else |
---|
109 | #define ThreadFuncReturnType void |
---|
110 | #endif |
---|
111 | |
---|
112 | static ThreadFuncReturnType |
---|
113 | ThreadFunc( void * _t ) |
---|
114 | { |
---|
115 | tr_thread * t = _t; |
---|
116 | |
---|
117 | #ifdef __BEOS__ |
---|
118 | /* This is required because on BeOS, SIGINT is sent to each thread, |
---|
119 | which kills them not nicely */ |
---|
120 | signal( SIGINT, SIG_IGN ); |
---|
121 | #endif |
---|
122 | |
---|
123 | t->func( t->arg ); |
---|
124 | |
---|
125 | #ifdef WIN32 |
---|
126 | _endthreadex( 0 ); |
---|
127 | return 0; |
---|
128 | #endif |
---|
129 | } |
---|
130 | |
---|
131 | tr_thread * |
---|
132 | tr_threadNew( void ( *func )(void *), |
---|
133 | void * arg ) |
---|
134 | { |
---|
135 | tr_thread * t = tr_new0( tr_thread, 1 ); |
---|
136 | |
---|
137 | t->func = func; |
---|
138 | t->arg = arg; |
---|
139 | |
---|
140 | #ifdef __BEOS__ |
---|
141 | t->thread = |
---|
142 | spawn_thread( (void*)ThreadFunc, "beos thread", B_NORMAL_PRIORITY, |
---|
143 | t ); |
---|
144 | resume_thread( t->thread ); |
---|
145 | #elif defined( WIN32 ) |
---|
146 | { |
---|
147 | unsigned int id; |
---|
148 | t->thread_handle = |
---|
149 | (HANDLE) _beginthreadex( NULL, 0, &ThreadFunc, t, 0, |
---|
150 | &id ); |
---|
151 | t->thread = (DWORD) id; |
---|
152 | } |
---|
153 | #else |
---|
154 | pthread_create( &t->thread, NULL, ( void * ( * )( |
---|
155 | void * ) )ThreadFunc, t ); |
---|
156 | #endif |
---|
157 | |
---|
158 | return t; |
---|
159 | } |
---|
160 | |
---|
161 | /*** |
---|
162 | **** LOCKS |
---|
163 | ***/ |
---|
164 | |
---|
165 | struct tr_lock |
---|
166 | { |
---|
167 | int depth; |
---|
168 | #ifdef __BEOS__ |
---|
169 | sem_id lock; |
---|
170 | thread_id lockThread; |
---|
171 | #elif defined( WIN32 ) |
---|
172 | CRITICAL_SECTION lock; |
---|
173 | DWORD lockThread; |
---|
174 | #else |
---|
175 | pthread_mutex_t lock; |
---|
176 | pthread_t lockThread; |
---|
177 | #endif |
---|
178 | }; |
---|
179 | |
---|
180 | tr_lock* |
---|
181 | tr_lockNew( void ) |
---|
182 | { |
---|
183 | tr_lock * l = tr_new0( tr_lock, 1 ); |
---|
184 | |
---|
185 | #ifdef __BEOS__ |
---|
186 | l->lock = create_sem( 1, "" ); |
---|
187 | #elif defined( WIN32 ) |
---|
188 | InitializeCriticalSection( &l->lock ); /* supports recursion */ |
---|
189 | #else |
---|
190 | pthread_mutexattr_t attr; |
---|
191 | pthread_mutexattr_init( &attr ); |
---|
192 | pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE ); |
---|
193 | pthread_mutex_init( &l->lock, &attr ); |
---|
194 | #endif |
---|
195 | |
---|
196 | return l; |
---|
197 | } |
---|
198 | |
---|
199 | void |
---|
200 | tr_lockFree( tr_lock * l ) |
---|
201 | { |
---|
202 | #ifdef __BEOS__ |
---|
203 | delete_sem( l->lock ); |
---|
204 | #elif defined( WIN32 ) |
---|
205 | DeleteCriticalSection( &l->lock ); |
---|
206 | #else |
---|
207 | pthread_mutex_destroy( &l->lock ); |
---|
208 | #endif |
---|
209 | tr_free( l ); |
---|
210 | } |
---|
211 | |
---|
212 | void |
---|
213 | tr_lockLock( tr_lock * l ) |
---|
214 | { |
---|
215 | #ifdef __BEOS__ |
---|
216 | acquire_sem( l->lock ); |
---|
217 | #elif defined( WIN32 ) |
---|
218 | EnterCriticalSection( &l->lock ); |
---|
219 | #else |
---|
220 | pthread_mutex_lock( &l->lock ); |
---|
221 | #endif |
---|
222 | assert( l->depth >= 0 ); |
---|
223 | if( l->depth ) |
---|
224 | assert( tr_areThreadsEqual( l->lockThread, tr_getCurrentThread( ) ) ); |
---|
225 | l->lockThread = tr_getCurrentThread( ); |
---|
226 | ++l->depth; |
---|
227 | } |
---|
228 | |
---|
229 | int |
---|
230 | tr_lockHave( const tr_lock * l ) |
---|
231 | { |
---|
232 | return ( l->depth > 0 ) |
---|
233 | && ( tr_areThreadsEqual( l->lockThread, tr_getCurrentThread( ) ) ); |
---|
234 | } |
---|
235 | |
---|
236 | void |
---|
237 | tr_lockUnlock( tr_lock * l ) |
---|
238 | { |
---|
239 | assert( l->depth > 0 ); |
---|
240 | assert( tr_areThreadsEqual( l->lockThread, tr_getCurrentThread( ) ) ); |
---|
241 | |
---|
242 | --l->depth; |
---|
243 | assert( l->depth >= 0 ); |
---|
244 | #ifdef __BEOS__ |
---|
245 | release_sem( l->lock ); |
---|
246 | #elif defined( WIN32 ) |
---|
247 | LeaveCriticalSection( &l->lock ); |
---|
248 | #else |
---|
249 | pthread_mutex_unlock( &l->lock ); |
---|
250 | #endif |
---|
251 | } |
---|
252 | |
---|
253 | /*** |
---|
254 | **** PATHS |
---|
255 | ***/ |
---|
256 | |
---|
257 | #if !defined( WIN32 ) && !defined( __BEOS__ ) && !defined( __AMIGAOS4__ ) |
---|
258 | #include <pwd.h> |
---|
259 | #endif |
---|
260 | |
---|
261 | static const char * |
---|
262 | getHomeDir( void ) |
---|
263 | { |
---|
264 | static char * home = NULL; |
---|
265 | |
---|
266 | if( !home ) |
---|
267 | { |
---|
268 | home = tr_strdup( getenv( "HOME" ) ); |
---|
269 | |
---|
270 | if( !home ) |
---|
271 | { |
---|
272 | #ifdef WIN32 |
---|
273 | SHGetFolderPath( NULL, CSIDL_MYDOCUMENTS, NULL, 0, home ); |
---|
274 | #elif defined( __BEOS__ ) || defined( __AMIGAOS4__ ) |
---|
275 | home = tr_strdup( "" ); |
---|
276 | #else |
---|
277 | struct passwd * pw = getpwuid( getuid( ) ); |
---|
278 | if( pw ) |
---|
279 | home = tr_strdup( pw->pw_dir ); |
---|
280 | endpwent( ); |
---|
281 | #endif |
---|
282 | } |
---|
283 | |
---|
284 | if( !home ) |
---|
285 | home = tr_strdup( "" ); |
---|
286 | } |
---|
287 | |
---|
288 | return home; |
---|
289 | } |
---|
290 | |
---|
291 | static const char * |
---|
292 | getOldConfigDir( void ) |
---|
293 | { |
---|
294 | static char * path = NULL; |
---|
295 | |
---|
296 | if( !path ) |
---|
297 | { |
---|
298 | #ifdef __BEOS__ |
---|
299 | char buf[MAX_PATH_LENGTH]; |
---|
300 | find_directory( B_USER_SETTINGS_DIRECTORY, |
---|
301 | dev_for_path( "/boot" ), true, |
---|
302 | buf, sizeof( buf ) ); |
---|
303 | path = tr_buildPath( buf, "Transmission", NULL ); |
---|
304 | #elif defined( SYS_DARWIN ) |
---|
305 | path = tr_buildPath( getHomeDir( ), "Library", |
---|
306 | "Application Support", |
---|
307 | "Transmission", NULL ); |
---|
308 | #elif defined( __AMIGAOS4__ ) |
---|
309 | path = tr_strdup( "PROGDIR:.transmission" ); |
---|
310 | #elif defined( WIN32 ) |
---|
311 | char appdata[MAX_PATH_LENGTH]; |
---|
312 | SHGetFolderPath( NULL, CSIDL_APPDATA, NULL, 0, appdata ); |
---|
313 | path = tr_buildPath( appdata, "Transmission", NULL ); |
---|
314 | #else |
---|
315 | path = tr_buildPath( getHomeDir( ), ".transmission", NULL ); |
---|
316 | #endif |
---|
317 | } |
---|
318 | |
---|
319 | return path; |
---|
320 | } |
---|
321 | |
---|
322 | #if defined(SYS_DARWIN) || defined(WIN32) |
---|
323 | #define RESUME_SUBDIR "Resume" |
---|
324 | #define TORRENT_SUBDIR "Torrents" |
---|
325 | #else |
---|
326 | #define RESUME_SUBDIR "resume" |
---|
327 | #define TORRENT_SUBDIR "torrents" |
---|
328 | #endif |
---|
329 | |
---|
330 | static const char * |
---|
331 | getOldTorrentsDir( void ) |
---|
332 | { |
---|
333 | static char * path = NULL; |
---|
334 | |
---|
335 | if( !path ) |
---|
336 | path = tr_buildPath( getOldConfigDir( ), TORRENT_SUBDIR, NULL ); |
---|
337 | |
---|
338 | return path; |
---|
339 | } |
---|
340 | |
---|
341 | static const char * |
---|
342 | getOldCacheDir( void ) |
---|
343 | { |
---|
344 | static char * path = NULL; |
---|
345 | |
---|
346 | if( !path ) |
---|
347 | { |
---|
348 | #if defined( __BEOS__ ) || defined( WIN32 ) |
---|
349 | path = tr_buildPath( getOldConfigDir( ), "Cache", NULL ); |
---|
350 | #elif defined( SYS_DARWIN ) |
---|
351 | path = tr_buildPath( getHomeDir( ), "Library", "Caches", "Transmission", NULL ); |
---|
352 | #else |
---|
353 | path = tr_buildPath( getOldConfigDir( ), "cache", NULL ); |
---|
354 | #endif |
---|
355 | } |
---|
356 | |
---|
357 | return path; |
---|
358 | } |
---|
359 | |
---|
360 | static void |
---|
361 | moveFiles( const char * oldDir, |
---|
362 | const char * newDir ) |
---|
363 | { |
---|
364 | if( oldDir && newDir && strcmp( oldDir, newDir ) ) |
---|
365 | { |
---|
366 | DIR * dirh = opendir( oldDir ); |
---|
367 | if( dirh ) |
---|
368 | { |
---|
369 | int count = 0; |
---|
370 | struct dirent * dirp; |
---|
371 | while( ( dirp = readdir( dirh ) ) ) |
---|
372 | { |
---|
373 | if( strcmp( dirp->d_name, |
---|
374 | "." ) && strcmp( dirp->d_name, ".." ) ) |
---|
375 | { |
---|
376 | char * o = tr_buildPath( oldDir, dirp->d_name, NULL ); |
---|
377 | char * n = tr_buildPath( newDir, dirp->d_name, NULL ); |
---|
378 | rename( o, n ); |
---|
379 | ++count; |
---|
380 | tr_free( n ); |
---|
381 | tr_free( o ); |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | if( count ) |
---|
386 | tr_inf( _( "Migrated %1$d files from \"%2$s\" to \"%3$s\"" ), |
---|
387 | count, oldDir, newDir ); |
---|
388 | closedir( dirh ); |
---|
389 | } |
---|
390 | } |
---|
391 | } |
---|
392 | |
---|
393 | static void |
---|
394 | migrateFiles( const tr_handle * handle ) |
---|
395 | { |
---|
396 | static int migrated = FALSE; |
---|
397 | |
---|
398 | if( !migrated ) |
---|
399 | { |
---|
400 | const char * oldDir; |
---|
401 | const char * newDir; |
---|
402 | migrated = TRUE; |
---|
403 | |
---|
404 | oldDir = getOldTorrentsDir( ); |
---|
405 | newDir = tr_getTorrentDir( handle ); |
---|
406 | moveFiles( oldDir, newDir ); |
---|
407 | |
---|
408 | oldDir = getOldCacheDir( ); |
---|
409 | newDir = tr_getResumeDir( handle ); |
---|
410 | moveFiles( oldDir, newDir ); |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | void |
---|
415 | tr_setConfigDir( tr_handle * handle, |
---|
416 | const char * configDir ) |
---|
417 | { |
---|
418 | char * path; |
---|
419 | |
---|
420 | handle->configDir = tr_strdup( configDir ); |
---|
421 | |
---|
422 | path = tr_buildPath( configDir, RESUME_SUBDIR, NULL ); |
---|
423 | tr_mkdirp( path, 0777 ); |
---|
424 | handle->resumeDir = path; |
---|
425 | |
---|
426 | path = tr_buildPath( configDir, TORRENT_SUBDIR, NULL ); |
---|
427 | tr_mkdirp( path, 0777 ); |
---|
428 | handle->torrentDir = path; |
---|
429 | |
---|
430 | migrateFiles( handle ); |
---|
431 | } |
---|
432 | |
---|
433 | const char * |
---|
434 | tr_sessionGetConfigDir( const tr_handle * handle ) |
---|
435 | { |
---|
436 | return handle->configDir; |
---|
437 | } |
---|
438 | |
---|
439 | const char * |
---|
440 | tr_getTorrentDir( const tr_handle * handle ) |
---|
441 | { |
---|
442 | return handle->torrentDir; |
---|
443 | } |
---|
444 | |
---|
445 | const char * |
---|
446 | tr_getResumeDir( const tr_handle * handle ) |
---|
447 | { |
---|
448 | return handle->resumeDir; |
---|
449 | } |
---|
450 | |
---|
451 | const char* |
---|
452 | tr_getDefaultConfigDir( void ) |
---|
453 | { |
---|
454 | static char * s = NULL; |
---|
455 | |
---|
456 | if( !s ) |
---|
457 | { |
---|
458 | if( ( s = getenv( "TRANSMISSION_HOME" ) ) ) |
---|
459 | { |
---|
460 | s = tr_strdup( s ); |
---|
461 | } |
---|
462 | else |
---|
463 | { |
---|
464 | #ifdef SYS_DARWIN |
---|
465 | s = tr_buildPath( getHomeDir( ), "Library", |
---|
466 | "Application Support", "Transmission", NULL ); |
---|
467 | #elif defined( WIN32 ) |
---|
468 | char appdata[MAX_PATH_LENGTH]; |
---|
469 | SHGetFolderPath( NULL, CSIDL_APPDATA, NULL, 0, appdata ); |
---|
470 | s = tr_buildPath( appdata, "Transmission", NULL ); |
---|
471 | #else |
---|
472 | if( ( s = getenv( "XDG_CONFIG_HOME" ) ) ) |
---|
473 | s = tr_buildPath( s, "transmission", NULL ); |
---|
474 | else |
---|
475 | s = tr_buildPath( getHomeDir( ), ".config", "transmission", NULL ); |
---|
476 | #endif |
---|
477 | } |
---|
478 | } |
---|
479 | |
---|
480 | return s; |
---|
481 | } |
---|
482 | |
---|
483 | /*** |
---|
484 | **** |
---|
485 | ***/ |
---|
486 | |
---|
487 | static int |
---|
488 | isClutchDir( const char * path ) |
---|
489 | { |
---|
490 | struct stat sb; |
---|
491 | char * tmp = tr_buildPath( path, "javascript", "transmission.js", NULL ); |
---|
492 | const int ret = !stat( tmp, &sb ); |
---|
493 | tr_inf( _( "Searching for web interface file \"%s\"" ), tmp ); |
---|
494 | tr_free( tmp ); |
---|
495 | return ret; |
---|
496 | |
---|
497 | } |
---|
498 | |
---|
499 | const char * |
---|
500 | tr_getClutchDir( const tr_session * session UNUSED ) |
---|
501 | { |
---|
502 | static char * s = NULL; |
---|
503 | |
---|
504 | if( !s ) |
---|
505 | { |
---|
506 | if( ( s = getenv( "CLUTCH_HOME" ) ) ) |
---|
507 | { |
---|
508 | s = tr_strdup( s ); |
---|
509 | } |
---|
510 | else if( ( s = getenv( "TRANSMISSION_WEB_HOME" ) ) ) |
---|
511 | { |
---|
512 | s = tr_strdup( s ); |
---|
513 | } |
---|
514 | else |
---|
515 | { |
---|
516 | #ifdef SYS_DARWIN |
---|
517 | CFURLRef appURL = CFBundleCopyBundleURL( |
---|
518 | CFBundleGetMainBundle( ) ); |
---|
519 | CFStringRef appRef = CFURLCopyFileSystemPath( |
---|
520 | appURL, kCFURLPOSIXPathStyle ); |
---|
521 | const char * appString = CFStringGetCStringPtr( |
---|
522 | appRef, CFStringGetFastestEncoding( appRef ) ); |
---|
523 | CFRelease( appURL ); |
---|
524 | CFRelease( appRef ); |
---|
525 | |
---|
526 | s = tr_buildPath( appString, "Contents", "Resources", "web", NULL ); |
---|
527 | #elif defined( WIN32 ) |
---|
528 | |
---|
529 | #warning hey win32 people is this good or is there a better implementation of the next four lines |
---|
530 | char appdata[MAX_PATH_LENGTH]; |
---|
531 | SHGetFolderPath( NULL, CSIDL_APPDATA, NULL, 0, appdata ); |
---|
532 | s = tr_buildPath( appdata, "Transmission", NULL ); |
---|
533 | #else |
---|
534 | tr_list *candidates = NULL, *l; |
---|
535 | |
---|
536 | /* XDG_DATA_HOME should be the first in the list of candidates */ |
---|
537 | s = getenv( "XDG_DATA_HOME" ); |
---|
538 | if( s && *s ) |
---|
539 | tr_list_append( &candidates, tr_strdup( s ) ); |
---|
540 | else { |
---|
541 | char * dhome = tr_buildPath( getHomeDir( ), ".local", "share", NULL ); |
---|
542 | tr_list_append( &candidates, dhome ); |
---|
543 | } |
---|
544 | |
---|
545 | /* XDG_DATA_DIRS are the backup directories */ |
---|
546 | s = getenv( "XDG_DATA_DIRS" ); |
---|
547 | if( !s || !*s ) |
---|
548 | s = PACKAGE_DATA_DIR ":/usr/local/share/:/usr/share/"; |
---|
549 | while( s && *s ) |
---|
550 | { |
---|
551 | char * end = strchr( s, ':' ); |
---|
552 | if( end ) |
---|
553 | { |
---|
554 | tr_list_append( &candidates, tr_strndup( s, end - s ) ); |
---|
555 | s = end + 1; |
---|
556 | } |
---|
557 | else |
---|
558 | { |
---|
559 | tr_list_append( &candidates, tr_strdup( s ) ); |
---|
560 | break; |
---|
561 | } |
---|
562 | } |
---|
563 | |
---|
564 | for( l = candidates; l; l = l->next ) |
---|
565 | { |
---|
566 | char * path = tr_buildPath( l->data, "transmission", "web", NULL ); |
---|
567 | const int found = isClutchDir( path ); |
---|
568 | tr_free( path ); |
---|
569 | if( found ) { |
---|
570 | s = path; |
---|
571 | break; |
---|
572 | } |
---|
573 | } |
---|
574 | |
---|
575 | tr_list_free( &candidates, tr_free ); |
---|
576 | #endif |
---|
577 | } |
---|
578 | } |
---|
579 | |
---|
580 | return s; |
---|
581 | } |
---|
582 | |
---|
583 | /*** |
---|
584 | **** |
---|
585 | ***/ |
---|
586 | |
---|
587 | tr_lockfile_state_t |
---|
588 | tr_lockfile( const char * filename ) |
---|
589 | { |
---|
590 | tr_lockfile_state_t ret; |
---|
591 | |
---|
592 | #ifdef WIN32 |
---|
593 | |
---|
594 | HANDLE file = CreateFile( |
---|
595 | filename, |
---|
596 | GENERIC_READ | GENERIC_WRITE, |
---|
597 | FILE_SHARE_READ | |
---|
598 | FILE_SHARE_WRITE, |
---|
599 | NULL, |
---|
600 | OPEN_ALWAYS, |
---|
601 | FILE_ATTRIBUTE_NORMAL, |
---|
602 | NULL ); |
---|
603 | if( file == INVALID_HANDLE_VALUE ) |
---|
604 | ret = TR_LOCKFILE_EOPEN; |
---|
605 | else if( !LockFile( file, 0, 0, 1, 1 ) ) |
---|
606 | ret = TR_LOCKFILE_ELOCK; |
---|
607 | else |
---|
608 | ret = TR_LOCKFILE_SUCCESS; |
---|
609 | |
---|
610 | #else |
---|
611 | |
---|
612 | int fd = open( filename, O_RDWR | O_CREAT, 0666 ); |
---|
613 | if( fd < 0 ) |
---|
614 | ret = TR_LOCKFILE_EOPEN; |
---|
615 | else |
---|
616 | { |
---|
617 | struct flock lk; |
---|
618 | memset( &lk, 0, sizeof( lk ) ); |
---|
619 | lk.l_start = 0; |
---|
620 | lk.l_len = 0; |
---|
621 | lk.l_type = F_WRLCK; |
---|
622 | lk.l_whence = SEEK_SET; |
---|
623 | if( -1 == fcntl( fd, F_SETLK, &lk ) ) |
---|
624 | ret = TR_LOCKFILE_ELOCK; |
---|
625 | else |
---|
626 | ret = TR_LOCKFILE_SUCCESS; |
---|
627 | } |
---|
628 | |
---|
629 | #endif |
---|
630 | |
---|
631 | return ret; |
---|
632 | } |
---|
633 | |
---|
634 | #ifdef WIN32 |
---|
635 | |
---|
636 | /* The following mmap functions are by Joerg Walter, and were taken from |
---|
637 | * his paper at: http://www.genesys-e.de/jwalter/mix4win.htm |
---|
638 | */ |
---|
639 | |
---|
640 | static LONG volatile g_sl __attribute__ ( ( aligned ( 4 ) ) ); |
---|
641 | |
---|
642 | /* Wait for spin lock */ |
---|
643 | static int |
---|
644 | slwait( LONG volatile *sl ) |
---|
645 | { |
---|
646 | while( InterlockedCompareExchange ( sl, 1, 0 ) != 0 ) |
---|
647 | Sleep ( 0 ); |
---|
648 | |
---|
649 | return 0; |
---|
650 | } |
---|
651 | |
---|
652 | /* Release spin lock */ |
---|
653 | static int |
---|
654 | slrelease( LONG *sl ) |
---|
655 | { |
---|
656 | InterlockedExchange ( sl, 0 ); |
---|
657 | return 0; |
---|
658 | } |
---|
659 | |
---|
660 | /* getpagesize for windows */ |
---|
661 | static long |
---|
662 | getpagesize( void ) |
---|
663 | { |
---|
664 | static long g_pagesize = 0; |
---|
665 | |
---|
666 | if( !g_pagesize ) |
---|
667 | { |
---|
668 | SYSTEM_INFO system_info; |
---|
669 | GetSystemInfo ( &system_info ); |
---|
670 | g_pagesize = system_info.dwPageSize; |
---|
671 | } |
---|
672 | return g_pagesize; |
---|
673 | } |
---|
674 | |
---|
675 | static long |
---|
676 | getregionsize( void ) |
---|
677 | { |
---|
678 | static long g_regionsize = 0; |
---|
679 | |
---|
680 | if( !g_regionsize ) |
---|
681 | { |
---|
682 | SYSTEM_INFO system_info; |
---|
683 | GetSystemInfo ( &system_info ); |
---|
684 | g_regionsize = system_info.dwAllocationGranularity; |
---|
685 | } |
---|
686 | return g_regionsize; |
---|
687 | } |
---|
688 | |
---|
689 | void * |
---|
690 | mmap( void *ptr, |
---|
691 | long size, |
---|
692 | long prot, |
---|
693 | long type, |
---|
694 | long handle, |
---|
695 | long arg ) |
---|
696 | { |
---|
697 | static long g_pagesize; |
---|
698 | static long g_regionsize; |
---|
699 | |
---|
700 | /* Wait for spin lock */ |
---|
701 | slwait ( &g_sl ); |
---|
702 | /* First time initialization */ |
---|
703 | if( !g_pagesize ) |
---|
704 | g_pagesize = getpagesize ( ); |
---|
705 | if( !g_regionsize ) |
---|
706 | g_regionsize = getregionsize ( ); |
---|
707 | /* Allocate this */ |
---|
708 | ptr = VirtualAlloc ( ptr, size, |
---|
709 | MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, |
---|
710 | PAGE_READWRITE ); |
---|
711 | if( !ptr ) |
---|
712 | { |
---|
713 | ptr = (void *) -1; |
---|
714 | goto mmap_exit; |
---|
715 | } |
---|
716 | mmap_exit: |
---|
717 | /* Release spin lock */ |
---|
718 | slrelease ( &g_sl ); |
---|
719 | return ptr; |
---|
720 | } |
---|
721 | |
---|
722 | long |
---|
723 | munmap( void *ptr, |
---|
724 | long size ) |
---|
725 | { |
---|
726 | static long g_pagesize; |
---|
727 | static long g_regionsize; |
---|
728 | int rc = -1; |
---|
729 | |
---|
730 | /* Wait for spin lock */ |
---|
731 | slwait ( &g_sl ); |
---|
732 | /* First time initialization */ |
---|
733 | if( !g_pagesize ) |
---|
734 | g_pagesize = getpagesize ( ); |
---|
735 | if( !g_regionsize ) |
---|
736 | g_regionsize = getregionsize ( ); |
---|
737 | /* Free this */ |
---|
738 | if( !VirtualFree ( ptr, 0, |
---|
739 | MEM_RELEASE ) ) |
---|
740 | goto munmap_exit; |
---|
741 | rc = 0; |
---|
742 | munmap_exit: |
---|
743 | /* Release spin lock */ |
---|
744 | slrelease ( &g_sl ); |
---|
745 | return rc; |
---|
746 | } |
---|
747 | |
---|
748 | #endif |
---|
749 | |
---|