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.6 by root, Fri Dec 30 12:21:28 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)
51 // acquire shared in addition to excl, then unlock excl
52 #define u_lock_write_to_read(wait) u_lock (U_LOCK_READ, 1); u_lock (U_UNLOCK, 1);
45 53
54#else
55
56 #define _GNU_SOURCE 1
57 #define _BSD_SOURCE 1
58 // the above increases our chances of getting MAP_ANONYMOUS
59
46 #include <sys/mman.h> 60 #include <sys/mman.h>
47 #include <sys/types.h> 61 #include <sys/types.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)
94 #define u_lock_write_to_read(wait) u_lock (U_LOCK_READ, 1);
72 95
73#endif 96#endif
74 97
75#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1]) 98#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]) 99#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3])
77 100
101/* some simple? dynamic memory management for paths might save ltos of ram */
78static u_handle pack_handle; 102static u_handle pack_handle;
103static u_handle lock_handle;
79static char tmppath[MAX_PATH]; 104static char tmppath[MAX_PATH];
80static char currdir[MAX_PATH]; 105static char currdir[MAX_PATH];
81static char datadir[MAX_PATH]; // %AppData%/urlader 106static char datadir[MAX_PATH]; // %AppData%/urlader
82static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID 107static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID
83static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER 108static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER
84static char exe_id[MAX_PATH]; 109static char exe_id[MAX_PATH];
85static char exe_ver[MAX_PATH]; 110static char exe_ver[MAX_PATH];
86 111
87static int exe_argc; 112static int exe_argc;
88static const char *exe_argv[32]; 113static const char *exe_argv[MAX_ARGC];
114static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */
115static unsigned int exe_argo;
89 116
90static void 117static void
91fatal (const char *msg) 118fatal (const char *msg)
92{ 119{
93#ifdef _WIN32 120#ifdef _WIN32
94 MessageBox (0, msg, URLADER, 0); 121 MessageBox (0, msg, URLADER, 0);
95#else 122#else
96 fprintf (stderr, "%s: %s\n", URLADER, msg); 123 write (2, URLADER ": ", sizeof (URLADER ": ") - 1);
124 write (2, msg, strlen (msg));
125 write (2, "\n", 1);
97#endif 126#endif
98 127
99 _exit (1); 128 _exit (1);
129}
130
131static void *
132u_malloc (unsigned int size)
133{
134 void *addr;
135
136#ifdef _WIN32
137 HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL);
138
139 if (!handle)
140 return 0;
141
142 addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size);
143
144 CloseHandle (handle);
145#elif defined (MAP_ANONYMOUS)
146 addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
147
148 if (addr == (void *)-1)
149 addr = 0;
150#else
151 addr = malloc (size);
152#endif
153
154 return addr;
155}
156
157static void
158u_free (void *addr, unsigned int size)
159{
160#ifdef _WIN32
161 UnmapViewOfFile (addr);
162#elif defined (MAP_ANONYMOUS)
163 munmap (addr, size);
164#else
165 free (addr);
166#endif
167}
168
169static void *
170u_mmap (u_handle h, unsigned int size)
171{
172 void *addr;
173
174#ifdef _WIN32
175 HANDLE handle = CreateFileMapping (h, 0, PAGE_READONLY, 0, size, NULL);
176
177 if (!handle)
178 return 0;
179
180 addr = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, size);
181
182 CloseHandle (handle);
183#else
184 addr = mmap (0, size, PROT_READ, MAP_SHARED, h, 0);
185
186 if (addr == (void *)-1)
187 addr = 0;
188#endif
189
190 return addr;
191}
192
193static void
194u_munmap (void *addr, unsigned int len)
195{
196#ifdef _WIN32
197 UnmapViewOfFile (addr);
198#else
199 munmap (addr, len);
200#endif
100} 201}
101 202
102static void 203static void
103tmpdir (const char *dir) 204tmpdir (const char *dir)
104{ 205{
115 if (!u_mkdir (tmppath)) 216 if (!u_mkdir (tmppath))
116 return; 217 return;
117 } 218 }
118} 219}
119 220
221enum
222{
223 U_UNLOCK,
224 U_LOCK_READ,
225 U_LOCK_WRITE
226};
227
120static void 228static int
229u_lock (int mode, int dowait)
230{
231#ifdef _WIN32
232 if (mode == U_UNLOCK)
233 return !UnlockFile (lock_handle, 0, 0, 1, 0);
234 else
235 {
236 OVERLAPPED ov = { 0 };
237
238 return !LockFileEx (lock_handle,
239 0
240 | (mode == U_LOCK_WRITE ? LOCKFILE_EXCLUSIVE_LOCK : 0)
241 | (dowait ? 0 : LOCKFILE_FAIL_IMMEDIATELY),
242 0,
243 1, 0,
244 &ov);
245 }
246#else
247 static short mode2lck [] = { F_UNLCK, F_RDLCK, F_WRLCK };
248 struct flock lck;
249 lck.l_type = mode2lck [mode];
250 lck.l_whence = SEEK_SET;
251 lck.l_start = 0;
252 lck.l_len = 1;
253
254 return fcntl (lock_handle, dowait ? F_SETLKW : F_SETLK, &lck);
255#endif
256}
257
258static void
121systemv (const char *const argv[], int dowait) 259systemv (const char *const argv[])
122{ 260{
123#ifdef _WIN32 261#ifdef _WIN32
124 _spawnv (P_WAIT, argv [0], argv); 262 _spawnv (P_WAIT, argv [0], argv);
125#else 263#else
126 pid_t pid = dowait ? fork () : 0; 264 pid_t pid = fork ();
127 265
128 if (pid < 0) 266 if (pid < 0)
129 fatal ("fork failure"); 267 fatal ("fork failure");
130 268
131 if (!pid) 269 if (!pid)
147 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 }; 285 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 };
148 sprintf (buf, "\"%s\"", path); 286 sprintf (buf, "\"%s\"", path);
149 #else 287 #else
150 const char *argv[] = { "/bin/rm", "-rf", path, 0 }; 288 const char *argv[] = { "/bin/rm", "-rf", path, 0 };
151 #endif 289 #endif
152 systemv (argv, 1); 290 systemv (argv);
153} 291}
154 292
155enum 293enum
156{ 294{
157 T_NULL, // 5 295 T_NULL, // 5
185}; 323};
186 324
187static char *pack_base, *pack_end; 325static char *pack_base, *pack_end;
188static struct hdr *pack_cur; 326static struct hdr *pack_cur;
189static char *scratch; 327static char *scratch;
190static unsigned int scratch_len; 328static unsigned int scratch_size;
191 329
192#define PACK_NAME ((char *)(pack_cur + 1)) 330#define PACK_NAME ((char *)(pack_cur + 1))
193#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) 331#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1)
194#define PACK_VALID pack_cur->type 332#define PACK_VALID pack_cur->type
195 333
202} 340}
203 341
204static void 342static void
205pack_unmap (void) 343pack_unmap (void)
206{ 344{
207 if (!pack_base) 345 if (pack_base)
208 return; 346 {
209
210 free (scratch);
211
212#ifdef _WIN32
213 UnmapViewOfFile (pack_base);
214#else
215 munmap (pack_base, pack_end - pack_base); 347 u_munmap (pack_base, pack_end - pack_base);
216#endif 348 pack_base = 0;
349 }
350
351 if (scratch)
352 {
353 u_free (scratch, scratch_size);
354 scratch = 0;
355 }
217} 356}
218 357
219static int 358static int
220pack_map (void) 359pack_map (void)
221{ 360{
361 char *addr;
362 unsigned int size;
363
222#ifdef _WIN32 364#ifdef _WIN32
223 BY_HANDLE_FILE_INFORMATION fi; 365 BY_HANDLE_FILE_INFORMATION fi;
224 366
225 if (!GetFileInformationByHandle (pack_handle, &fi)) 367 if (!GetFileInformationByHandle (pack_handle, &fi))
226 return 0; 368 return 0;
227 369
228 if (fi.nFileSizeHigh) // too big 370 size = fi.nFileSizeLow;
371#else
372 size = lseek (pack_handle, 0, SEEK_END);
373#endif
374
375 addr = u_mmap (pack_handle, size);
376 if (!addr)
229 return 0; 377 return 0;
230 378
231 HANDLE handle = CreateFileMapping (pack_handle, 0, PAGE_READONLY, 0, fi.nFileSizeLow, NULL);
232
233 if (!handle)
234 return 0;
235
236 pack_unmap (); 379 pack_unmap ();
237 380
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; 381 pack_base = addr;
251
252 pack_end = pack_base + len; 382 pack_end = pack_base + size;
253#endif
254
255 if (!pack_base)
256 return 0;
257 383
258 struct tail *tail; 384 struct tail *tail;
259 385
260 tail = (void *)(pack_end - sizeof (*tail)); 386 tail = (void *)(pack_end - sizeof (*tail));
261 387
262 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 388 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
263 return 0; 389 return 0;
264 390
265 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); 391 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size));
266 392
267 free (scratch);
268 scratch = malloc (scratch_len = u_32 (tail->max_filesize)); 393 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize));
394 if (!scratch)
395 return 0;
269 396
270 return 1; 397 return 1;
271} 398}
272 399
273static void 400static void
318 for (;;) 445 for (;;)
319 { 446 {
320 if (pack_cur->type == T_ENV) 447 if (pack_cur->type == T_ENV)
321 u_setenv (PACK_NAME, PACK_DATA); 448 u_setenv (PACK_NAME, PACK_DATA);
322 else if (pack_cur->type == T_ARG) 449 else if (pack_cur->type == T_ARG)
323 exe_argv [exe_argc++] = strdup (PACK_NAME); 450 {
451 int len = u_16 (pack_cur->namelen) + 1;
452 exe_argv [exe_argc++] = exe_args + exe_argo;
453 memcpy (exe_args + exe_argo, PACK_NAME, len);
454 exe_argo += len;
455 }
324 else 456 else
325 break; 457 break;
326 458
327 pack_next (); 459 pack_next ();
328 } 460 }
329 461
330done_env_arg: 462done_env_arg:
331 strcpy (execdir, exe_dir); 463 strcpy (execdir, exe_dir);
332 u_append (execdir, "i-"); 464 u_append (execdir, "i-");
333 strcat (execdir, exe_ver); 465 strcat (execdir, exe_ver);
466
467 strcat (strcpy (tmppath, execdir), ".lck");
468 lock_handle = u_lockfile (tmppath);
469 if (!lock_handle)
470 fatal ("unable to create lock file");
471
472 u_cloexec (lock_handle);
473
474 // all users that uee an instance acquire a shared lock
475 // acquiring an exclusive lock is enough to delete the dir
476 // deleeting the lockfile is another topic altogether
477 u_lock (U_LOCK_READ, 1);
334 478
335 if (access (execdir, F_OK)) 479 if (access (execdir, F_OK))
336 { 480 {
337 // does not exist yet, so unpack and move 481 // does not exist yet, so unpack and move
338 tmpdir (exe_dir); 482 tmpdir (exe_dir);
353 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); 497 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC);
354 unsigned int dlen, len = u_32 (pack_cur->datalen); 498 unsigned int dlen, len = u_32 (pack_cur->datalen);
355 char *data = PACK_DATA; 499 char *data = PACK_DATA;
356 500
357 if (pack_cur->flags & F_LZF) 501 if (pack_cur->flags & F_LZF)
358 if (dlen = lzf_decompress (data, len, scratch, scratch_len)) 502 if (dlen = lzf_decompress (data, len, scratch, scratch_size))
359 { 503 {
360 data = scratch; 504 data = scratch;
361 len = dlen; 505 len = dlen;
362 } 506 }
363 else 507 else
419 563
420static void 564static void
421execute (void) 565execute (void)
422{ 566{
423 exe_argv [exe_argc] = 0; 567 exe_argv [exe_argc] = 0;
424 systemv (exe_argv, 0); 568 systemv (exe_argv);
425} 569}
426 570
427#ifdef _WIN32 571#ifdef _WIN32
428 572
429int APIENTRY 573int APIENTRY
478 622
479 if (!getcwd (currdir, sizeof (currdir))) 623 if (!getcwd (currdir, sizeof (currdir)))
480 strcpy (currdir, "."); 624 strcpy (currdir, ".");
481 625
482 u_mkdir (home); 626 u_mkdir (home);
627 //strcat (strcat (strcpy (datadir, home), "/."), URLADER);
483 sprintf (datadir, "%s/.%s", home, URLADER); 628 sprintf (datadir, "%s/.%s", home, URLADER);
484 u_mkdir (datadir); 629 u_mkdir (datadir);
485 630
486#if 0 631#if 0
487 if (gethostname (tmppath, sizeof (tmppath))) 632 if (gethostname (tmppath, sizeof (tmppath)))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines