ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Urlader/urlader.c
(Generate patch)

Comparing Urlader/urlader.c (file contents):
Revision 1.5 by root, Fri Dec 30 09:34:20 2011 UTC vs.
Revision 1.7 by root, Fri Dec 30 14:51:45 2011 UTC

1#ifndef URLADER 1#ifndef URLADER
2# define URLADER "urlader" 2# define URLADER "urlader"
3#endif 3#endif
4#define URLADER_VERSION "1.0" // a decimal number, not a version string 4#define URLADER_VERSION "1.0" // a decimal number, not a version string
5
6#define MAX_ARGC 32
7#define MAX_ARGS 256
5 8
6#include <stdio.h> 9#include <stdio.h>
7#include <stdlib.h> 10#include <stdlib.h>
8#include <unistd.h> 11#include <unistd.h>
9#include <errno.h> 12#include <errno.h>
15 18
16#define TAIL_MAGIC "SCHMORPPACK0" 19#define TAIL_MAGIC "SCHMORPPACK0"
17 20
18#ifdef _WIN32 21#ifdef _WIN32
19 22
20 #include <windows.h> 23 #include <windows.h>
21 //#include <winbase.h> 24 //#include <winbase.h>
22 #include <shlobj.h> 25 #include <shlobj.h>
23 #include <shlwapi.h> 26 #include <shlwapi.h>
24 #include <wininet.h> 27 #include <wininet.h>
25 28
26 static DWORD dword; 29 static DWORD dword;
27 30
28 #define u_handle HANDLE 31 #define u_handle HANDLE
32 #define u_invalid_handle 0
29 #define u_valid(handle) (!!handle) 33 #define u_valid(handle) (!!handle)
30 34
31 #define u_setenv(name,value) SetEnvironmentVariable (name, value) 35 #define u_setenv(name,value) SetEnvironmentVariable (name, value)
32 #define u_mkdir(path) !CreateDirectory (path, NULL) 36 #define u_mkdir(path) !CreateDirectory (path, NULL)
33 #define u_chdir(path) !SetCurrentDirectory (path) 37 #define u_chdir(path) !SetCurrentDirectory (path)
34 #define u_rename(fr,to) !MoveFile (fr, to) 38 #define u_rename(fr,to) !MoveFile (fr, to)
35 #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) 39 #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
36 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) 40 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
41 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
37 #define u_close(handle) CloseHandle (handle) 42 #define u_close(handle) CloseHandle (handle)
38 #define u_append(path,add) PathAppend (path, add) 43 #define u_append(path,add) PathAppend (path, add)
39 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) 44 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1)
40 45
41 #define u_fsync(handle) FlushFileBuffers (handle) 46 #define u_fsync(handle) FlushFileBuffers (handle)
42 #define u_sync() 47 #define u_sync()
43 48
44#else 49 #define u_lockfile(path) CreateFile (path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)
50 #define u_cloexec(handle)
45 51
52#else
53
54 #define _GNU_SOURCE 1
55 #define _BSD_SOURCE 1
56 // the above increases our chances of getting MAP_ANONYMOUS
57
46 #include <sys/mman.h> 58 #include <sys/mman.h>
47 #include <sys/types.h> 59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <unistd.h>
48 #include <pwd.h> 62 #include <pwd.h>
49 63
64 #if defined (MAP_ANON) && !defined (MAP_ANONYMOUS)
65 #define MAP_ANONYMOUS MAP_ANON
66 #endif
67
50 #ifdef PATH_MAX 68 #ifdef PATH_MAX
51 #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX) 69 #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX)
52 #else 70 #else
53 #define MAX_PATH 4096 71 #define MAX_PATH 4096
54 #endif 72 #endif
55 73
56 #define u_handle int 74 #define u_handle int
75 #define u_invalid_handle -1
57 #define u_valid(fd) ((fd) >= 0) 76 #define u_valid(fd) ((fd) >= 0)
58 77
59 #define u_setenv(name,value) setenv (name, value, 1) 78 #define u_setenv(name,value) setenv (name, value, 1)
60 #define u_mkdir(path) mkdir (path, 0777) 79 #define u_mkdir(path) mkdir (path, 0777)
61 #define u_chdir(path) chdir (path) 80 #define u_chdir(path) chdir (path)
62 #define u_rename(fr,to) rename (fr, to) 81 #define u_rename(fr,to) rename (fr, to)
63 #define u_open(path) open (path, O_RDONLY) 82 #define u_open(path) open (path, O_RDONLY)
64 #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) 83 #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666)
65 #define u_close(handle) close (handle) 84 #define u_close(handle) close (handle)
66 #define u_append(path,add) strcat (strcat (path, "/"), add) 85 #define u_append(path,add) strcat (strcat (path, "/"), add)
67 #define u_write(handle,data,len) write (handle, data, len) 86 #define u_write(handle,data,len) write (handle, data, len)
68 87
69 // on a mostly idle system, a sync at the end is certainly faster, hope for the best 88 // on a mostly idle system, a sync at the end is certainly faster, hope for the best
70 #define u_fsync(handle) 89 #define u_fsync(handle)
71 #define u_sync() sync () 90 #define u_sync() sync ()
91
92 #define u_lockfile(path) open (path, O_RDWR | O_CREAT, 0666)
93 #define u_cloexec(handle) fcntl (handle, F_SETFD, FD_CLOEXEC)
72 94
73#endif 95#endif
74 96
75#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1]) 97#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1])
76#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3]) 98#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3])
77 99
100/* some simple? dynamic memory management for paths might save ltos of ram */
78static u_handle pack_handle; 101static u_handle pack_handle;
102static u_handle lock_handle;
79static char tmppath[MAX_PATH]; 103static char tmppath[MAX_PATH];
80static char currdir[MAX_PATH]; 104static char currdir[MAX_PATH];
81static char datadir[MAX_PATH]; // %AppData%/urlader 105static char datadir[MAX_PATH]; // %AppData%/urlader
82static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID 106static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID
83static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER 107static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER
84static char exe_id[MAX_PATH]; 108static char exe_id[MAX_PATH];
85static char exe_ver[MAX_PATH]; 109static char exe_ver[MAX_PATH];
86 110
87static int exe_argc; 111static int exe_argc;
88static const char *exe_argv[32]; 112static const char *exe_argv[MAX_ARGC];
113static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */
114static unsigned int exe_argo;
89 115
90static void 116static void
91fatal (const char *msg) 117fatal (const char *msg)
92{ 118{
93#ifdef _WIN32 119#ifdef _WIN32
94 MessageBox (0, msg, URLADER, 0); 120 MessageBox (0, msg, URLADER, 0);
95#else 121#else
96 fprintf (stderr, "%s: %s\n", URLADER, msg); 122 write (2, URLADER ": ", sizeof (URLADER ": ") - 1);
123 write (2, msg, strlen (msg));
124 write (2, "\n", 1);
97#endif 125#endif
98 126
99 _exit (1); 127 _exit (1);
128}
129
130static void *
131u_malloc (unsigned int size)
132{
133 void *addr;
134
135#ifdef _WIN32
136 HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL);
137
138 if (!handle)
139 return 0;
140
141 addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size);
142
143 CloseHandle (handle);
144#elif defined (MAP_ANONYMOUS)
145 addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
146
147 if (addr == (void *)-1)
148 addr = 0;
149#else
150 addr = malloc (size);
151#endif
152
153 return addr;
154}
155
156static void
157u_free (void *addr, unsigned int size)
158{
159#ifdef _WIN32
160 UnmapViewOfFile (addr);
161#elif defined (MAP_ANONYMOUS)
162 munmap (addr, size);
163#else
164 free (addr);
165#endif
166}
167
168static void *
169u_mmap (u_handle h, unsigned int size)
170{
171 void *addr;
172
173#ifdef _WIN32
174 HANDLE handle = CreateFileMapping (h, 0, PAGE_READONLY, 0, size, NULL);
175
176 if (!handle)
177 return 0;
178
179 addr = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, size);
180
181 CloseHandle (handle);
182#else
183 addr = mmap (0, size, PROT_READ, MAP_SHARED, h, 0);
184
185 if (addr == (void *)-1)
186 addr = 0;
187#endif
188
189 return addr;
190}
191
192static void
193u_munmap (void *addr, unsigned int len)
194{
195#ifdef _WIN32
196 UnmapViewOfFile (addr);
197#else
198 munmap (addr, len);
199#endif
100} 200}
101 201
102static void 202static void
103tmpdir (const char *dir) 203tmpdir (const char *dir)
104{ 204{
115 if (!u_mkdir (tmppath)) 215 if (!u_mkdir (tmppath))
116 return; 216 return;
117 } 217 }
118} 218}
119 219
220static u_handle
221u_lock (const char *path, int excl, int dowait)
222{
223 u_handle h;
224
225 h = u_lockfile (path);
226 if (!u_valid (h))
227 return h;
228
229 u_cloexec (h);
230
231 for (;;)
232 {
233 int success;
234
235 // acquire the lock
236#ifdef _WIN32
237 OVERLAPPED ov = { 0 };
238
239 success = LockFileEx (h,
240 (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0)
241 | (dowait ? 0 : LOCKFILE_FAIL_IMMEDIATELY),
242 0,
243 1, 0,
244 &ov);
245#else
246 struct flock lck = { 0 };
247
248 lck.l_type = excl ? F_WRLCK : F_RDLCK;
249 lck.l_whence = SEEK_SET;
250 lck.l_len = 1;
251
252 success = !fcntl (h, dowait ? F_SETLKW : F_SETLK, &lck);
253#endif
254
255 if (!success)
256 break;
257
258 // we have the lock, now verify that the lockfile still exists
259
260#ifdef _WIN32
261 // apparently, we have to open the file to get its info :(
262 BY_HANDLE_FILE_INFORMATION s1, s2;
263 u_handle h2 = u_lockfile (path);
264 if (!u_valid (h))
265 break;
266
267 success = GetFileInformationByHandle (h, &s1)
268 && GetFileInformationByHandle (h2, &s2);
269
270 u_close (h2);
271
272 if (!success)
273 break;
274
275 success = s1.dwVolumeSerialNumber == s2.dwVolumeSerialNumber
276 && s1.nFileIndexHigh == s2.nFileIndexHigh
277 && s1.nFileIndexLow == s2.nFileIndexLow;
278#else
279 struct stat s1, s2;
280
281 if (fstat (h, &s1) || stat (path, &s2))
282 break;
283
284 success = s1.st_dev == s2.st_dev
285 && s1.st_ino == s2.st_ino;
286#endif
287
288 if (success)
289 return h; // lock successfully acquired
290
291 // files differ, close and retry - should be very rare
292 u_close (h);
293 }
294
295 // failure
296 u_close (h);
297 return u_invalid_handle;
298}
299
120static void 300static void
121systemv (const char *const argv[], int dowait) 301systemv (const char *const argv[])
122{ 302{
123#ifdef _WIN32 303#ifdef _WIN32
124 _spawnv (P_WAIT, argv [0], argv); 304 _spawnv (P_WAIT, argv [0], argv);
125#else 305#else
126 pid_t pid = dowait ? fork () : 0; 306 pid_t pid = fork ();
127 307
128 if (pid < 0) 308 if (pid < 0)
129 fatal ("fork failure"); 309 fatal ("fork failure");
130 310
131 if (!pid) 311 if (!pid)
147 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 }; 327 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 };
148 sprintf (buf, "\"%s\"", path); 328 sprintf (buf, "\"%s\"", path);
149 #else 329 #else
150 const char *argv[] = { "/bin/rm", "-rf", path, 0 }; 330 const char *argv[] = { "/bin/rm", "-rf", path, 0 };
151 #endif 331 #endif
152 systemv (argv, 1); 332 systemv (argv);
153} 333}
154 334
155enum 335enum
156{ 336{
157 T_NULL, // 5 337 T_NULL, // 5
185}; 365};
186 366
187static char *pack_base, *pack_end; 367static char *pack_base, *pack_end;
188static struct hdr *pack_cur; 368static struct hdr *pack_cur;
189static char *scratch; 369static char *scratch;
190static unsigned int scratch_len; 370static unsigned int scratch_size;
191 371
192#define PACK_NAME ((char *)(pack_cur + 1)) 372#define PACK_NAME ((char *)(pack_cur + 1))
193#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) 373#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1)
194#define PACK_VALID pack_cur->type 374#define PACK_VALID pack_cur->type
195 375
202} 382}
203 383
204static void 384static void
205pack_unmap (void) 385pack_unmap (void)
206{ 386{
207 if (!pack_base) 387 if (pack_base)
208 return; 388 {
209
210 free (scratch);
211
212#ifdef _WIN32
213 UnmapViewOfFile (pack_base);
214#else
215 munmap (pack_base, pack_end - pack_base); 389 u_munmap (pack_base, pack_end - pack_base);
216#endif 390 pack_base = 0;
391 }
392
393 if (scratch)
394 {
395 u_free (scratch, scratch_size);
396 scratch = 0;
397 }
217} 398}
218 399
219static int 400static int
220pack_map (void) 401pack_map (void)
221{ 402{
403 char *addr;
404 unsigned int size;
405
222#ifdef _WIN32 406#ifdef _WIN32
223 BY_HANDLE_FILE_INFORMATION fi; 407 BY_HANDLE_FILE_INFORMATION fi;
224 408
225 if (!GetFileInformationByHandle (pack_handle, &fi)) 409 if (!GetFileInformationByHandle (pack_handle, &fi))
226 return 0; 410 return 0;
227 411
228 if (fi.nFileSizeHigh) // too big 412 size = fi.nFileSizeLow;
413#else
414 size = lseek (pack_handle, 0, SEEK_END);
415#endif
416
417 addr = u_mmap (pack_handle, size);
418 if (!addr)
229 return 0; 419 return 0;
230 420
231 HANDLE handle = CreateFileMapping (pack_handle, 0, PAGE_READONLY, 0, fi.nFileSizeLow, NULL);
232
233 if (!handle)
234 return 0;
235
236 pack_unmap (); 421 pack_unmap ();
237 422
238 pack_base = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, fi.nFileSizeLow);
239
240 CloseHandle (handle);
241
242 pack_end = pack_base + fi.nFileSizeLow;
243#else
244 off_t len = lseek (pack_handle - 1, 0, SEEK_END);
245
246 pack_unmap ();
247
248 pack_base = mmap (0, len, PROT_READ, MAP_SHARED, pack_handle - 1, 0);
249 if (pack_base == (void *)-1)
250 pack_base = 0; 423 pack_base = addr;
251
252 pack_end = pack_base + len; 424 pack_end = pack_base + size;
253#endif
254
255 if (!pack_base)
256 return 0;
257 425
258 struct tail *tail; 426 struct tail *tail;
259 427
260 tail = (void *)(pack_end - sizeof (*tail)); 428 tail = (void *)(pack_end - sizeof (*tail));
261 429
262 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 430 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
263 return 0; 431 return 0;
264 432
265 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); 433 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size));
266 434
267 free (scratch);
268 scratch = malloc (scratch_len = u_32 (tail->max_filesize)); 435 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize));
436 if (!scratch)
437 return 0;
269 438
270 return 1; 439 return 1;
271} 440}
272 441
273static void 442static void
318 for (;;) 487 for (;;)
319 { 488 {
320 if (pack_cur->type == T_ENV) 489 if (pack_cur->type == T_ENV)
321 u_setenv (PACK_NAME, PACK_DATA); 490 u_setenv (PACK_NAME, PACK_DATA);
322 else if (pack_cur->type == T_ARG) 491 else if (pack_cur->type == T_ARG)
323 exe_argv [exe_argc++] = strdup (PACK_NAME); 492 {
493 int len = u_16 (pack_cur->namelen) + 1;
494 exe_argv [exe_argc++] = exe_args + exe_argo;
495 memcpy (exe_args + exe_argo, PACK_NAME, len);
496 exe_argo += len;
497 }
324 else 498 else
325 break; 499 break;
326 500
327 pack_next (); 501 pack_next ();
328 } 502 }
329 503
330done_env_arg: 504done_env_arg:
331 strcpy (execdir, exe_dir); 505 strcpy (execdir, exe_dir);
332 u_append (execdir, "i-"); 506 u_append (execdir, "i-");
333 strcat (execdir, exe_ver); 507 strcat (execdir, exe_ver);
508
509 strcat (strcpy (tmppath, execdir), ".lck");
510 lock_handle = u_lock (tmppath, 0, 1);
511 if (!lock_handle)
512 fatal ("unable to lock application instance");
334 513
335 if (access (execdir, F_OK)) 514 if (access (execdir, F_OK))
336 { 515 {
337 // does not exist yet, so unpack and move 516 // does not exist yet, so unpack and move
338 tmpdir (exe_dir); 517 tmpdir (exe_dir);
353 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); 532 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC);
354 unsigned int dlen, len = u_32 (pack_cur->datalen); 533 unsigned int dlen, len = u_32 (pack_cur->datalen);
355 char *data = PACK_DATA; 534 char *data = PACK_DATA;
356 535
357 if (pack_cur->flags & F_LZF) 536 if (pack_cur->flags & F_LZF)
358 if (dlen = lzf_decompress (data, len, scratch, scratch_len)) 537 if (dlen = lzf_decompress (data, len, scratch, scratch_size))
359 { 538 {
360 data = scratch; 539 data = scratch;
361 len = dlen; 540 len = dlen;
362 } 541 }
363 else 542 else
419 598
420static void 599static void
421execute (void) 600execute (void)
422{ 601{
423 exe_argv [exe_argc] = 0; 602 exe_argv [exe_argc] = 0;
424 systemv (exe_argv, 0); 603 systemv (exe_argv);
425} 604}
426 605
427#ifdef _WIN32 606#ifdef _WIN32
428 607
429int APIENTRY 608int APIENTRY
478 657
479 if (!getcwd (currdir, sizeof (currdir))) 658 if (!getcwd (currdir, sizeof (currdir)))
480 strcpy (currdir, "."); 659 strcpy (currdir, ".");
481 660
482 u_mkdir (home); 661 u_mkdir (home);
662 //strcat (strcat (strcpy (datadir, home), "/."), URLADER);
483 sprintf (datadir, "%s/.%s", home, URLADER); 663 sprintf (datadir, "%s/.%s", home, URLADER);
484 u_mkdir (datadir); 664 u_mkdir (datadir);
485 665
486#if 0 666#if 0
487 if (gethostname (tmppath, sizeof (tmppath))) 667 if (gethostname (tmppath, sizeof (tmppath)))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines