Skip to content

Latest commit

 

History

History
823 lines (681 loc) · 21.1 KB

platform_unix.c

File metadata and controls

823 lines (681 loc) · 21.1 KB
 
May 12, 2007
May 12, 2007
1
2
3
4
5
6
7
8
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
May 8, 2007
May 8, 2007
9
#if PLATFORM_UNIX
May 2, 2007
May 2, 2007
10
Dec 4, 2006
Dec 4, 2006
11
12
#if PLATFORM_MACOSX
#include <Carbon/Carbon.h>
Dec 9, 2006
Dec 9, 2006
13
14
#undef true
#undef false
Dec 4, 2006
Dec 4, 2006
15
16
#endif
Dec 8, 2006
Dec 8, 2006
17
#include <sys/time.h>
Mar 25, 2006
Mar 25, 2006
18
19
20
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
Dec 13, 2006
Dec 13, 2006
21
#include <sys/utsname.h>
Apr 22, 2007
Apr 22, 2007
22
#include <sys/mount.h>
Dec 9, 2006
Dec 9, 2006
23
24
#include <time.h>
#include <unistd.h>
May 7, 2007
May 7, 2007
25
#include <signal.h>
May 9, 2007
May 9, 2007
26
#include <syslog.h>
Dec 13, 2006
Dec 13, 2006
27
Apr 22, 2007
Apr 22, 2007
28
29
30
31
32
33
34
35
36
37
38
#if MOJOSETUP_HAVE_SYS_UCRED_H
# ifdef MOJOSETUP_HAVE_MNTENT_H
# undef MOJOSETUP_HAVE_MNTENT_H /* don't do both... */
# endif
# include <sys/ucred.h>
#endif
#if MOJOSETUP_HAVE_MNTENT_H
# include <mntent.h>
#endif
Dec 13, 2006
Dec 13, 2006
39
#if PLATFORM_BEOS
Dec 13, 2006
Dec 13, 2006
40
41
42
43
#define DLOPEN_ARGS 0
void *beos_dlopen(const char *fname, int unused);
void *beos_dlsym(void *lib, const char *sym);
void beos_dlclose(void *lib);
May 7, 2007
May 7, 2007
44
void beos_usleep(unsigned long ticks);
Dec 13, 2006
Dec 13, 2006
45
46
47
#define dlopen beos_dlopen
#define dlsym beos_dlsym
#define dlclose beos_dlclose
May 7, 2007
May 7, 2007
48
#define usleep beos_usleep
Dec 13, 2006
Dec 13, 2006
49
50
51
52
#else
#include <dlfcn.h>
#define DLOPEN_ARGS (RTLD_NOW | RTLD_GLOBAL)
#endif
Dec 13, 2006
Dec 13, 2006
53
May 8, 2007
May 8, 2007
54
#include "platform.h"
May 17, 2007
May 17, 2007
55
#include "gui.h"
Mar 25, 2006
Mar 25, 2006
56
Dec 4, 2006
Dec 4, 2006
57
58
static struct timeval startup_time;
May 20, 2007
May 20, 2007
59
char *MojoPlatform_currentWorkingDir(void)
Dec 14, 2006
Dec 14, 2006
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{
char *retval = NULL;
size_t len;
// loop a few times in case we don't have a large enough buffer.
for (len = 128; len <= (16*1024); len *= 2)
{
retval = (char *) xrealloc(retval, len);
if (getcwd(retval, len-1) != NULL)
{
size_t slen = strlen(retval);
if (retval[slen-1] != '/') // make sure this ends with '/' ...
{
retval[slen] = '/';
retval[slen+1] = '\0';
} // if
return retval;
} // if
} // for
free(retval);
return NULL;
May 20, 2007
May 20, 2007
82
} // MojoPlatform_currentWorkingDir
Dec 14, 2006
Dec 14, 2006
83
84
Jul 2, 2007
Jul 2, 2007
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
char *MojoPlatform_readlink(const char *linkname)
{
size_t alloclen = 16;
char *retval = NULL;
char *buf = NULL;
size_t len = -1;
do
{
alloclen *= 2;
buf = xrealloc(buf, alloclen);
len = readlink(linkname, buf, alloclen-1);
if ( (len != -1) && (len < (alloclen-1)) ) // !error && !overflow
{
buf[len] = '\0'; // readlink() doesn't null-terminate!
retval = xrealloc(buf, len+1); // shrink it down.
} // if
} while (len >= (alloclen-1)); // loop if we need a bigger buffer.
return retval; // caller must free() this.
} // MojoPlatform_readlink
Dec 14, 2006
Dec 14, 2006
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
static void *guaranteeAllocation(void *ptr, size_t len, size_t *_alloclen)
{
void *retval = NULL;
size_t alloclen = *_alloclen;
if (alloclen > len)
return ptr;
if (!alloclen)
alloclen = 1;
while (alloclen <= len)
alloclen *= 2;
retval = xrealloc(ptr, alloclen);
if (retval != NULL)
*_alloclen = alloclen;
return retval;
} // guaranteeAllocation
// This is a mess, but I'm not sure it can be done more cleanly.
static char *realpathInternal(char *path, const char *cwd, int linkloop)
{
char *linkname = NULL;
char *retval = NULL;
size_t len = 0;
size_t alloclen = 0;
if (*path == '/') // absolute path.
{
retval = xstrdup("/");
path++;
len = 1;
} // if
else // relative path.
{
if (cwd != NULL)
retval = xstrdup(cwd);
else
{
May 20, 2007
May 20, 2007
149
if ((retval = MojoPlatform_currentWorkingDir()) == NULL)
Dec 14, 2006
Dec 14, 2006
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
return NULL;
} // else
len = strlen(retval);
} // else
while (true)
{
struct stat statbuf;
size_t newlen;
char *nextpath = strchr(path, '/');
if (nextpath != NULL)
*nextpath = '\0';
newlen = strlen(path);
retval = guaranteeAllocation(retval, len + newlen + 2, &alloclen);
strcpy(retval + len, path);
if (*path == '\0')
retval[--len] = '\0'; // chop ending "/" bit, it gets readded later.
else if (strcmp(path, ".") == 0)
{
retval[--len] = '\0'; // chop ending "/." bit
newlen = 0;
} // else if
else if (strcmp(path, "..") == 0)
{
char *ptr;
retval[--len] = '\0'; // chop ending "/.." bit
ptr = strrchr(retval, '/');
if ((ptr == NULL) || (ptr == retval))
{
strcpy(retval, "/");
len = 0;
} // if
else
{
*ptr = '\0';
len -= (size_t) ((retval+len)-ptr);
} // else
newlen = 0;
} // else if
// it may be a symlink...check it.
else if (lstat(retval, &statbuf) == -1)
goto realpathInternal_failed;
else if (S_ISLNK(statbuf.st_mode))
{
char *newresolve = NULL;
if (linkloop > 255)
goto realpathInternal_failed;
Jul 2, 2007
Jul 2, 2007
205
206
linkname = MojoPlatform_readlink(retval);
if (linkname == NULL)
Dec 14, 2006
Dec 14, 2006
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
goto realpathInternal_failed;
// chop off symlink name for its cwd.
retval[len] = '\0';
// resolve the link...
newresolve = realpathInternal(linkname, retval, linkloop + 1);
if (newresolve == NULL)
goto realpathInternal_failed;
len = strlen(newresolve);
retval = guaranteeAllocation(retval, len + 2, &alloclen);
strcpy(retval, newresolve);
free(newresolve);
free(linkname);
Jul 2, 2007
Jul 2, 2007
222
linkname = NULL;
Dec 14, 2006
Dec 14, 2006
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
} // else if
else
{
len += newlen;
} // else
if (nextpath == NULL)
break; // holy crap we're done!
else // append a '/' before the next path element.
{
path = nextpath + 1;
retval[len++] = '/';
retval[len] = '\0';
} // else
} // while
// Shrink string if we're using more memory than necessary...
if (alloclen > len+1)
retval = (char *) xrealloc(retval, len+1);
return retval;
realpathInternal_failed:
free(linkname);
free(retval);
return NULL;
} // realpathInternal
// Rolling my own realpath, even if the runtime has one, since apparently
// the spec is a little flakey, and it can overflow PATH_MAX. On BeOS <= 5,
// we'd have to resort to BPath to do this, too, and I'd rather avoid the C++
// dependencies and headers.
char *MojoPlatform_realpath(const char *_path)
{
char *path = xstrdup(_path);
char *retval = realpathInternal(path, NULL, 0);
free(path);
return retval;
} // MojoPlatform_realpath
Dec 2, 2006
Dec 2, 2006
266
267
268
269
270
271
272
273
274
275
// (Stolen from physicsfs: http://icculus.org/physfs/ ...)
/*
* See where program (bin) resides in the $PATH. Returns a copy of the first
* element in $PATH that contains it, or NULL if it doesn't exist or there
* were other problems.
*
* You are expected to free() the return value when you're done with it.
*/
static char *findBinaryInPath(const char *bin)
{
Dec 2, 2006
Dec 2, 2006
276
const char *_envr = getenv("PATH");
Dec 2, 2006
Dec 2, 2006
277
size_t alloc_size = 0;
Dec 2, 2006
Dec 2, 2006
278
char *envr = NULL;
Dec 2, 2006
Dec 2, 2006
279
char *exe = NULL;
Dec 2, 2006
Dec 2, 2006
280
char *start = NULL;
Dec 2, 2006
Dec 2, 2006
281
282
char *ptr = NULL;
Dec 2, 2006
Dec 2, 2006
283
if ((_envr == NULL) || (bin == NULL))
Dec 2, 2006
Dec 2, 2006
284
285
return NULL;
Dec 2, 2006
Dec 2, 2006
286
287
288
289
envr = (char *) alloca(strlen(_envr) + 1);
strcpy(envr, _envr);
start = envr;
Dec 2, 2006
Dec 2, 2006
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
do
{
size_t size;
ptr = strchr(start, ':'); // find next $PATH separator.
if (ptr)
*ptr = '\0';
size = strlen(start) + strlen(bin) + 2;
if (size > alloc_size)
{
char *x = (char *) xrealloc(exe, size);
alloc_size = size;
exe = x;
} // if
// build full binary path...
strcpy(exe, start);
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
strcat(exe, "/");
strcat(exe, bin);
if (access(exe, X_OK) == 0) // Exists as executable? We're done.
{
strcpy(exe, start); // i'm lazy. piss off.
return(exe);
} // if
start = ptr + 1; // start points to beginning of next element.
} while (ptr != NULL);
if (exe != NULL)
free(exe);
return(NULL); // doesn't exist in path.
} // findBinaryInPath
Dec 9, 2006
Dec 9, 2006
327
char *MojoPlatform_appBinaryPath(void)
Mar 25, 2006
Mar 25, 2006
328
{
Dec 2, 2006
Dec 2, 2006
329
const char *argv0 = GArgv[0];
Dec 9, 2006
Dec 9, 2006
330
char *retval = NULL;
Mar 25, 2006
Mar 25, 2006
331
Dec 14, 2006
Dec 14, 2006
332
333
if (strchr(argv0, '/') != NULL)
retval = MojoPlatform_realpath(argv0); // argv[0] contains a path?
Dec 2, 2006
Dec 2, 2006
334
335
else // slow path...have to search the whole $PATH for this one...
{
Dec 2, 2006
Dec 2, 2006
336
char *found = findBinaryInPath(argv0);
Dec 14, 2006
Dec 14, 2006
337
338
if (found)
retval = MojoPlatform_realpath(found);
Dec 2, 2006
Dec 2, 2006
339
340
free(found);
} // else
Mar 25, 2006
Mar 25, 2006
341
342
343
344
return retval;
} // MojoPlatform_appBinaryPath
Dec 3, 2006
Dec 3, 2006
345
May 10, 2007
May 10, 2007
346
347
348
349
350
351
352
char *MojoPlatform_homedir(void)
{
const char *envr = getenv("HOME");
return xstrdup(envr ? envr : "/");
} // MojoPlatform_homedir
Dec 3, 2006
Dec 3, 2006
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// This implementation is a bit naive.
boolean MojoPlatform_locale(char *buf, size_t len)
{
boolean retval = false;
const char *envr = getenv("LANG");
if (envr != NULL)
{
char *ptr = NULL;
xstrncpy(buf, envr, len);
ptr = strchr(buf, '.'); // chop off encoding if explicitly listed.
if (ptr != NULL)
*ptr = '\0';
retval = true;
} // if
Feb 16, 2007
Feb 16, 2007
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#if PLATFORM_MACOSX
else if (CFLocaleCreateCanonicalLocaleIdentifierFromString == NULL)
retval = false; // !!! FIXME: 10.2 compatibility?
else if (CFLocaleCreateCanonicalLocaleIdentifierFromString != NULL)
{
CFPropertyListRef languages = CFPreferencesCopyAppValue(
CFSTR("AppleLanguages"),
kCFPreferencesCurrentApplication);
if (languages != NULL)
{
CFStringRef primary = CFArrayGetValueAtIndex(languages, 0);
if (primary != NULL)
{
CFStringRef locale =
CFLocaleCreateCanonicalLocaleIdentifierFromString(
kCFAllocatorDefault, primary); if (locale != NULL)
if (locale != NULL)
{
CFStringGetCString(locale, buf, len, kCFStringEncodingUTF8);
CFRelease(locale);
retval = true;
} // if
} // if
CFRelease(languages);
} // if
} // else if
#endif
Dec 3, 2006
Dec 3, 2006
397
398
399
return retval;
} // MojoPlatform_locale
Dec 4, 2006
Dec 4, 2006
400
401
402
403
boolean MojoPlatform_osType(char *buf, size_t len)
{
#if PLATFORM_MACOSX
Dec 9, 2006
Dec 9, 2006
404
xstrncpy(buf, "macosx", len);
Dec 13, 2006
Dec 13, 2006
405
406
#elif PLATFORM_BEOS
xstrncpy(buf, "beos", len); // !!! FIXME: zeta? haiku?
Dec 4, 2006
Dec 4, 2006
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#elif defined(linux) || defined(__linux) || defined(__linux__)
xstrncpy(buf, "linux", len);
#elif defined(__FreeBSD__) || defined(__DragonFly__)
xstrncpy(buf, "freebsd", len);
#elif defined(__NetBSD__)
xstrncpy(buf, "netbsd", len);
#elif defined(__OpenBSD__)
xstrncpy(buf, "openbsd", len);
#elif defined(bsdi) || defined(__bsdi) || defined(__bsdi__)
xstrncpy(buf, "bsdi", len);
#elif defined(_AIX)
xstrncpy(buf, "aix", len);
#elif defined(hpux) || defined(__hpux) || defined(__hpux__)
xstrncpy(buf, "hpux", len);
#elif defined(sgi) || defined(__sgi) || defined(__sgi__) || defined(_SGI_SOURCE)
xstrncpy(buf, "irix", len);
#else
# error Please define your platform.
#endif
return true;
} // MojoPlatform_ostype
boolean MojoPlatform_osVersion(char *buf, size_t len)
{
#if PLATFORM_MACOSX
long ver = 0x0000;
if (Gestalt(gestaltSystemVersion, &ver) == noErr)
{
char str[16];
snprintf(str, sizeof (str), "%X", (int) ver);
snprintf(buf, len, "%c%c.%c.%c", str[0], str[1], str[2], str[3]);
return true;
} // if
Dec 13, 2006
Dec 13, 2006
442
443
444
445
446
447
448
449
450
451
#else
// This information may or may not actually MEAN anything. On BeOS, it's
// useful, but on other things, like Linux, it'll give you the kernel
// version, which doesn't necessarily help.
struct utsname un;
if (uname(&un) == 0)
{
xstrncpy(buf, un.release, len);
return true;
} // if
Dec 4, 2006
Dec 4, 2006
452
453
454
455
456
#endif
return false;
} // MojoPlatform_osversion
Dec 4, 2006
Dec 4, 2006
457
May 7, 2007
May 7, 2007
458
459
460
461
462
463
void MojoPlatform_sleep(uint32 ticks)
{
usleep(ticks * 1000);
} // MojoPlatform_sleep
Dec 4, 2006
Dec 4, 2006
464
465
466
467
468
469
470
471
472
473
474
uint32 MojoPlatform_ticks(void)
{
uint64 then_ms, now_ms;
struct timeval now;
gettimeofday(&now, NULL);
then_ms = (((uint64) startup_time.tv_sec) * 1000) +
(((uint64) startup_time.tv_usec) / 1000);
now_ms = (((uint64) now.tv_sec) * 1000) + (((uint64) now.tv_usec) / 1000);
return ((uint32) (now_ms - then_ms));
} // MojoPlatform_ticks
Dec 9, 2006
Dec 9, 2006
475
476
477
478
479
480
void MojoPlatform_die(void)
{
_exit(86);
} // MojoPlatform_die
Dec 9, 2006
Dec 9, 2006
481
482
483
boolean MojoPlatform_unlink(const char *fname)
{
May 7, 2007
May 7, 2007
484
485
486
487
488
489
490
491
492
493
boolean retval = false;
struct stat statbuf;
if (stat(fname, &statbuf) != -1)
{
if (S_ISDIR(statbuf.st_mode))
retval = (rmdir(fname) == 0);
else
retval = (unlink(fname) == 0);
} // if
return retval;
Dec 9, 2006
Dec 9, 2006
494
495
496
} // MojoPlatform_unlink
May 1, 2007
May 1, 2007
497
498
499
500
501
502
boolean MojoPlatform_symlink(const char *src, const char *dst)
{
return (symlink(dst, src) == 0);
} // MojoPlatform_symlink
May 18, 2007
May 18, 2007
503
boolean MojoPlatform_mkdir(const char *path, uint16 perms)
May 1, 2007
May 1, 2007
504
505
{
// !!! FIXME: error if already exists?
May 18, 2007
May 18, 2007
506
return (mkdir(path, perms) == 0);
May 1, 2007
May 1, 2007
507
508
509
510
511
512
513
514
515
} // MojoPlatform_mkdir
boolean MojoPlatform_rename(const char *src, const char *dst)
{
return (rename(src, dst) == 0);
} // MojoPlatform_rename
Apr 22, 2007
Apr 22, 2007
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
boolean MojoPlatform_exists(const char *dir, const char *fname)
{
boolean retval = false;
if (fname == NULL)
retval = (access(dir, F_OK) != -1);
else
{
const size_t len = strlen(dir) + strlen(fname) + 2;
char *buf = (char *) xmalloc(strlen(dir) + strlen(fname) + 2);
snprintf(buf, len, "%s/%s", dir, fname);
retval = (access(buf, F_OK) != -1);
free(buf);
} // else
return retval;
} // MojoPlatform_exists
May 10, 2007
May 10, 2007
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
boolean MojoPlatform_writable(const char *fname)
{
return (access(fname, W_OK) == 0);
} // MojoPlatform_writable
boolean MojoPlatform_isdir(const char *dir)
{
boolean retval = false;
struct stat statbuf;
if (stat(dir, &statbuf) != -1)
{
if (S_ISDIR(statbuf.st_mode))
retval = true;
} // if
return retval;
} // MojoPlatform_isdir
May 3, 2007
May 3, 2007
552
553
554
555
556
557
558
559
560
561
562
563
564
boolean MojoPlatform_perms(const char *fname, uint16 *p)
{
boolean retval = false;
struct stat statbuf;
if (stat(fname, &statbuf) != -1)
{
*p = statbuf.st_mode;
retval = true;
} // if
return retval;
} // MojoPlatform_perms
May 18, 2007
May 18, 2007
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
uint16 MojoPlatform_defaultFilePerms(void)
{
return 0644;
} // MojoPlatform_defaultFilePerms
uint16 MojoPlatform_defaultDirPerms(void)
{
return 0755;
} // MojoPlatform_defaultDirPerms
uint16 MojoPlatform_makePermissions(const char *str, boolean *_valid)
{
uint16 retval = 0644;
boolean valid = true;
if (str != NULL)
{
char *endptr = NULL;
long strval = strtol(str, &endptr, 8);
// complete string was a valid number?
valid = ((*endptr == '\0') && (strval >= 0) && (strval <= 0xFFFF));
if (valid)
retval = (uint16) strval;
} // if
*_valid = valid;
return retval;
} // MojoPlatform_makePermissions
May 3, 2007
May 3, 2007
596
597
598
599
600
601
boolean MojoPlatform_chmod(const char *fname, uint16 p)
{
return (chmod(fname, p) != -1);
} // MojoPlatform_chmod
Apr 22, 2007
Apr 22, 2007
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
char *MojoPlatform_findMedia(const char *uniquefile)
{
#if MOJOSETUP_HAVE_SYS_UCRED_H
int i = 0;
struct statfs *mntbufp = NULL;
int mounts = getmntinfo(&mntbufp, MNT_WAIT);
for (i = 0; i < mounts; i++)
{
const char *mnt = mntbufp[i].f_mntonname;
if (MojoPlatform_exists(mnt, uniquefile))
return xstrdup(mnt);
} // for
#elif MOJOSETUP_HAVE_MNTENT_H
FILE *mounts = setmntent("/etc/mtab", "r");
if (mounts != NULL)
{
struct mntent *ent = NULL;
while ((ent = getmntent(mounts)) != NULL)
{
const char *mnt = ent->mnt_dir;
if (MojoPlatform_exists(mnt, uniquefile))
{
endmntent(mounts);
return xstrdup(mnt);
} // if
} // while
endmntent(mounts);
} // if
#else
# warning No mountpoint detection on this platform...
#endif
return NULL;
} // MojoPlatform_findMedia
Dec 10, 2006
Dec 10, 2006
640
641
void MojoPlatform_log(const char *str)
{
May 9, 2007
May 9, 2007
642
syslog(LOG_USER | LOG_INFO, "%s", str);
May 17, 2007
May 17, 2007
643
644
645
646
647
648
649
#if PLATFORM_MACOSX
// put to stdout too, if this isn't the stdio UI.
// This will let the info show up in /Applications/Utilities/Console.app
if ((GGui != NULL) && (strcmp(GGui->name(), "stdio") != 0))
printf("%s\n", str);
#endif
Dec 10, 2006
Dec 10, 2006
650
651
} // MojoPlatform_log
Dec 9, 2006
Dec 9, 2006
652
Dec 9, 2006
Dec 9, 2006
653
654
static boolean testTmpDir(const char *dname, char *buf,
size_t len, const char *tmpl)
Dec 9, 2006
Dec 9, 2006
655
{
Dec 9, 2006
Dec 9, 2006
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
boolean retval = false;
if ( (dname != NULL) && (access(dname, R_OK | W_OK | X_OK) == 0) )
{
struct stat statbuf;
if ( (stat(dname, &statbuf) == 0) && (S_ISDIR(statbuf.st_mode)) )
{
const size_t rc = snprintf(buf, len, "%s/%s", dname, tmpl);
if (rc < len)
retval = true;
} // if
} // if
return retval;
} // testTmpDir
void *MojoPlatform_dlopen(const uint8 *img, size_t len)
{
// Write the image to a temporary file, dlopen() it, and delete it
// immediately. The inode will be kept around by the Unix kernel until
// we either dlclose() it or the process terminates, but we don't have
// to worry about polluting the /tmp directory or cleaning this up later.
Jan 14, 2007
Jan 14, 2007
678
679
680
// We'll try every reasonable temp directory location until we find one
// that works, in case (say) one lets us write a file, but there
// isn't enough space for the data.
Dec 9, 2006
Dec 9, 2006
681
Jan 14, 2007
Jan 14, 2007
682
683
684
// /dev/shm may be able to avoid writing to physical media...try it first.
const char *dirs[] = { "/dev/shm", getenv("TMPDIR"), P_tmpdir, "/tmp" };
const char *tmpl = "mojosetup-gui-plugin-XXXXXX";
Dec 9, 2006
Dec 9, 2006
685
char fname[PATH_MAX];
Jan 14, 2007
Jan 14, 2007
686
687
void *retval = NULL;
int i = 0;
Dec 13, 2006
Dec 13, 2006
688
689
690
691
if (dlopen == NULL) // weak symbol on older Mac OS X
return NULL;
Jan 14, 2007
Jan 14, 2007
692
693
694
695
696
#ifndef P_tmpdir // glibc defines this, maybe others.
#define P_tmpdir NULL
#endif
for (i = 0; (i < STATICARRAYLEN(dirs)) && (retval == NULL); i++)
Dec 9, 2006
Dec 9, 2006
697
{
Jan 14, 2007
Jan 14, 2007
698
if (testTmpDir(dirs[i], fname, len, tmpl))
Dec 9, 2006
Dec 9, 2006
699
{
Jan 14, 2007
Jan 14, 2007
700
701
702
703
704
705
706
707
708
const int fd = mkstemp(fname);
if (fd != -1)
{
const size_t bw = write(fd, img, len);
const int rc = close(fd);
if ((bw == len) && (rc != -1))
retval = dlopen(fname, DLOPEN_ARGS);
unlink(fname);
} // if
Dec 9, 2006
Dec 9, 2006
709
} // if
Jan 14, 2007
Jan 14, 2007
710
} // for
Dec 9, 2006
Dec 9, 2006
711
712
return retval;
Dec 9, 2006
Dec 9, 2006
713
714
715
716
717
} // MojoPlatform_dlopen
void *MojoPlatform_dlsym(void *lib, const char *sym)
{
Apr 21, 2007
Apr 21, 2007
718
719
720
721
#if PLATFORM_MACOSX
if (dlsym == NULL) return NULL; // weak symbol on older Mac OS X
#endif
Dec 9, 2006
Dec 9, 2006
722
723
724
725
726
727
return dlsym(lib, sym);
} // MojoPlatform_dlsym
void MojoPlatform_dlclose(void *lib)
{
Apr 21, 2007
Apr 21, 2007
728
729
730
731
732
#if PLATFORM_MACOSX
if (dlclose == NULL) return; // weak symbol on older Mac OS X
#endif
dlclose(lib);
Dec 9, 2006
Dec 9, 2006
733
734
} // MojoPlatform_dlclose
May 7, 2007
May 7, 2007
735
May 20, 2007
May 20, 2007
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#if SUPPORT_MULTIARCH
void MojoPlatform_switchBin(const uint8 *img, size_t len)
{
const char *dirs[] = { "/dev/shm", getenv("TMPDIR"), P_tmpdir, "/tmp" };
const char *tmpl = "mojosetup-switch-bin-XXXXXX";
char fname[PATH_MAX];
int i = 0;
for (i = 0; i < STATICARRAYLEN(dirs); i++)
{
if (testTmpDir(dirs[i], fname, len, tmpl))
{
const int fd = mkstemp(fname);
if (fd != -1)
{
const size_t bw = write(fd, img, len);
const int rc = close(fd);
if ((bw == len) && (rc != -1))
{
const char *tmpstr = GArgv[0];
chmod(fname, 0700);
GArgv[0] = fname;
execv(fname, (char * const *) GArgv);
// only hits this line if process wasn't replaced.
GArgv[0] = tmpstr;
} // if
unlink(fname);
} // if
} // if
} // for
// couldn't replace current process.
} // MojoPlatform_switchBin
#endif
May 7, 2007
May 7, 2007
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
static void signal_catcher(int sig)
{
static boolean first_shot = true;
if (first_shot)
{
first_shot = false;
logError("Caught signal #%d", sig);
} // if
} // signal_catcher
static void crash_catcher(int sig)
{
signal_catcher(sig);
MojoSetup_crashed();
} // crash_catcher
static void termination_catcher(int sig)
{
signal_catcher(sig);
MojoSetup_terminated();
} // termination_catcher
static void install_signals(void)
{
static int crash_sigs[] = { SIGSEGV, SIGILL, SIGBUS, SIGFPE, SIGTRAP };
static int term_sigs[] = { SIGQUIT, SIGINT, SIGTERM, SIGHUP };
static int ignore_sigs[] = { SIGPIPE };
int i;
for (i = 0; i < STATICARRAYLEN(crash_sigs); i++)
signal(crash_sigs[i], crash_catcher);
for (i = 0; i < STATICARRAYLEN(term_sigs); i++)
signal(term_sigs[i], termination_catcher);
for (i = 0; i < STATICARRAYLEN(ignore_sigs); i++)
signal(ignore_sigs[i], SIG_IGN);
} // install_signals
int main(int argc, char **argv)
{
gettimeofday(&startup_time, NULL);
May 9, 2007
May 9, 2007
814
815
openlog("mojosetup", LOG_PID, LOG_USER);
atexit(closelog);
May 7, 2007
May 7, 2007
816
817
818
819
install_signals();
return MojoSetup_main(argc, argv);
} // main
May 8, 2007
May 8, 2007
820
821
#endif // PLATFORM_UNIX
Mar 25, 2006
Mar 25, 2006
822
// end of unix.c ...