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

Comparing Urlader/urlader.c (file contents):
Revision 1.6 by root, Fri Dec 30 12:21:28 2011 UTC vs.
Revision 1.8 by root, Fri Dec 30 23:34:08 2011 UTC

1#ifndef URLADER 1#include "urlib.c"
2# define URLADER "urlader"
3#endif
4#define URLADER_VERSION "1.0" // a decimal number, not a version string
5
6#define MAX_ARGC 32
7#define MAX_ARGS 256
8 2
9#include <stdio.h> 3#include <stdio.h>
10#include <stdlib.h> 4#include <stdlib.h>
11#include <unistd.h> 5#include <unistd.h>
12#include <errno.h> 6#include <errno.h>
13#include <string.h> 7#include <string.h>
14#include <time.h> 8#include <time.h>
15#include <fcntl.h>
16 9
17#include "liblzf/lzf_d.c" 10#include "liblzf/lzf_d.c"
18 11
19#define TAIL_MAGIC "SCHMORPPACK0" 12#include "urlib.h"
20
21#ifdef _WIN32
22
23 #include <windows.h>
24 //#include <winbase.h>
25 #include <shlobj.h>
26 #include <shlwapi.h>
27 #include <wininet.h>
28
29 static DWORD dword;
30
31 #define u_handle HANDLE
32 #define u_invalid_handle 0
33 #define u_valid(handle) (!!handle)
34
35 #define u_setenv(name,value) SetEnvironmentVariable (name, value)
36 #define u_mkdir(path) !CreateDirectory (path, NULL)
37 #define u_chdir(path) !SetCurrentDirectory (path)
38 #define u_rename(fr,to) !MoveFile (fr, to)
39 #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 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)
42 #define u_close(handle) CloseHandle (handle)
43 #define u_append(path,add) PathAppend (path, add)
44 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1)
45
46 #define u_fsync(handle) FlushFileBuffers (handle)
47 #define u_sync()
48
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);
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
60 #include <sys/mman.h>
61 #include <sys/types.h>
62 #include <pwd.h>
63
64 #if defined (MAP_ANON) && !defined (MAP_ANONYMOUS)
65 #define MAP_ANONYMOUS MAP_ANON
66 #endif
67
68 #ifdef PATH_MAX
69 #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX)
70 #else
71 #define MAX_PATH 4096
72 #endif
73
74 #define u_handle int
75 #define u_invalid_handle -1
76 #define u_valid(fd) ((fd) >= 0)
77
78 #define u_setenv(name,value) setenv (name, value, 1)
79 #define u_mkdir(path) mkdir (path, 0777)
80 #define u_chdir(path) chdir (path)
81 #define u_rename(fr,to) rename (fr, to)
82 #define u_open(path) open (path, O_RDONLY)
83 #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666)
84 #define u_close(handle) close (handle)
85 #define u_append(path,add) strcat (strcat (path, "/"), add)
86 #define u_write(handle,data,len) write (handle, data, len)
87
88 // on a mostly idle system, a sync at the end is certainly faster, hope for the best
89 #define u_fsync(handle)
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);
95
96#endif
97
98#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1])
99#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3])
100 13
101/* some simple? dynamic memory management for paths might save ltos of ram */ 14/* some simple? dynamic memory management for paths might save ltos of ram */
102static u_handle pack_handle; 15static u_handle pack_handle;
103static u_handle lock_handle; 16static u_handle lock_handle;
104static char tmppath[MAX_PATH]; 17static char tmppath[MAX_PATH];
126#endif 39#endif
127 40
128 _exit (1); 41 _exit (1);
129} 42}
130 43
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
201}
202
203static void 44static void
204tmpdir (const char *dir) 45tmpdir (const char *dir)
205{ 46{
206 static int cnt; 47 static int cnt;
207 48
214// #endif 55// #endif
215 56
216 if (!u_mkdir (tmppath)) 57 if (!u_mkdir (tmppath))
217 return; 58 return;
218 } 59 }
219}
220
221enum
222{
223 U_UNLOCK,
224 U_LOCK_READ,
225 U_LOCK_WRITE
226};
227
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} 60}
257 61
258static void 62static void
259systemv (const char *const argv[]) 63systemv (const char *const argv[])
260{ 64{
288 const char *argv[] = { "/bin/rm", "-rf", path, 0 }; 92 const char *argv[] = { "/bin/rm", "-rf", path, 0 };
289 #endif 93 #endif
290 systemv (argv); 94 systemv (argv);
291} 95}
292 96
293enum
294{
295 T_NULL, // 5
296 T_META, // 1 : exe_id, exe_ver
297 T_ENV, // 2 : name, value
298 T_ARG, // 3 : arg
299 T_DIR, // 4+: path
300 T_FILE, // 4+: path, data
301 T_NUM
302};
303
304enum
305{
306 F_EXEC = 1,
307 F_LZF = 2,
308 F_NULL = 0
309};
310
311struct hdr
312{
313 unsigned char type;
314 unsigned char flags;
315 unsigned char namelen[2];
316 unsigned char datalen[4];
317};
318
319struct tail {
320 unsigned char max_filesize[4];
321 unsigned char size[4];
322 char magic[12];
323};
324
325static char *pack_base, *pack_end; 97static char *pack_base, *pack_end;
326static struct hdr *pack_cur; 98static struct u_pack_hdr *pack_cur;
327static char *scratch; 99static char *scratch;
328static unsigned int scratch_size; 100static unsigned int scratch_size;
329 101
330#define PACK_NAME ((char *)(pack_cur + 1)) 102#define PACK_NAME ((char *)(pack_cur + 1))
331#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) 103#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1)
334static void 106static void
335pack_next (void) 107pack_next (void)
336{ 108{
337 unsigned int d = u_32 (pack_cur->datalen); 109 unsigned int d = u_32 (pack_cur->datalen);
338 110
339 pack_cur = (struct hdr *)(PACK_DATA + d); 111 pack_cur = (struct u_pack_hdr *)(PACK_DATA + d);
340} 112}
341 113
342static void 114static void
343pack_unmap (void) 115pack_unmap (void)
344{ 116{
379 pack_unmap (); 151 pack_unmap ();
380 152
381 pack_base = addr; 153 pack_base = addr;
382 pack_end = pack_base + size; 154 pack_end = pack_base + size;
383 155
384 struct tail *tail; 156 struct u_pack_tail *tail;
385 157
386 tail = (void *)(pack_end - sizeof (*tail)); 158 tail = (void *)(pack_end - sizeof (*tail));
387 159
388 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 160 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
389 return 0; 161 return 0;
390 162
391 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); 163 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size));
392 164
393 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); 165 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize));
394 if (!scratch) 166 if (!scratch)
395 return 0; 167 return 0;
396 168
401exe_info (void) 173exe_info (void)
402{ 174{
403 if (!pack_map ()) 175 if (!pack_map ())
404 fatal ("unable to locate packfile in executable - executable corrupted?"); 176 fatal ("unable to locate packfile in executable - executable corrupted?");
405 177
406 if (pack_cur->type != T_META) 178 if (pack_cur->type != U_T_META)
407 fatal ("unable to locate executable metadata - executable corrupted?"); 179 fatal ("unable to locate executable metadata - executable corrupted?");
408 180
409 strcpy (exe_id, PACK_NAME); 181 strcpy (exe_id, PACK_NAME);
410} 182}
411 183
434 } 206 }
435 207
436 u_close (oh); 208 u_close (oh);
437 } 209 }
438 210
439 if (pack_cur->type != T_META) 211 if (pack_cur->type != U_T_META)
440 fatal ("unable to locate override metadata"); 212 fatal ("unable to locate override metadata");
441 213
442 strcpy (exe_ver, PACK_DATA); 214 strcpy (exe_ver, PACK_DATA);
443 pack_next (); 215 pack_next ();
444 216
445 for (;;) 217 for (;;)
446 { 218 {
447 if (pack_cur->type == T_ENV) 219 if (pack_cur->type == U_T_ENV)
448 u_setenv (PACK_NAME, PACK_DATA); 220 u_setenv (PACK_NAME, PACK_DATA);
449 else if (pack_cur->type == T_ARG) 221 else if (pack_cur->type == U_T_ARG)
450 { 222 {
451 int len = u_16 (pack_cur->namelen) + 1; 223 int len = u_16 (pack_cur->namelen) + 1;
452 exe_argv [exe_argc++] = exe_args + exe_argo; 224 exe_argv [exe_argc++] = exe_args + exe_argo;
453 memcpy (exe_args + exe_argo, PACK_NAME, len); 225 memcpy (exe_args + exe_argo, PACK_NAME, len);
454 exe_argo += len; 226 exe_argo += len;
463 strcpy (execdir, exe_dir); 235 strcpy (execdir, exe_dir);
464 u_append (execdir, "i-"); 236 u_append (execdir, "i-");
465 strcat (execdir, exe_ver); 237 strcat (execdir, exe_ver);
466 238
467 strcat (strcpy (tmppath, execdir), ".lck"); 239 strcat (strcpy (tmppath, execdir), ".lck");
468 lock_handle = u_lockfile (tmppath); 240 lock_handle = u_lock (tmppath, 0, 1);
469 if (!lock_handle) 241 if (!lock_handle)
470 fatal ("unable to create lock file"); 242 fatal ("unable to lock application instance");
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);
478 243
479 if (access (execdir, F_OK)) 244 if (access (execdir, F_OK))
480 { 245 {
481 // does not exist yet, so unpack and move 246 // does not exist yet, so unpack and move
482 tmpdir (exe_dir); 247 tmpdir (exe_dir);
486 251
487 for (;;) 252 for (;;)
488 { 253 {
489 switch (pack_cur->type) 254 switch (pack_cur->type)
490 { 255 {
491 case T_DIR: 256 case U_T_DIR:
492 u_mkdir (PACK_NAME); 257 u_mkdir (PACK_NAME);
493 break; 258 break;
494 259
495 case T_FILE: 260 case U_T_FILE:
496 { 261 {
497 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); 262 u_handle h = u_creat (PACK_NAME, pack_cur->flags & U_F_EXEC);
498 unsigned int dlen, len = u_32 (pack_cur->datalen); 263 unsigned int dlen, len = u_32 (pack_cur->datalen);
499 char *data = PACK_DATA; 264 char *data = PACK_DATA;
500 265
501 if (pack_cur->flags & F_LZF) 266 if (pack_cur->flags & U_F_LZF)
502 if (dlen = lzf_decompress (data, len, scratch, scratch_size)) 267 if (dlen = lzf_decompress (data, len, scratch, scratch_size))
503 { 268 {
504 data = scratch; 269 data = scratch;
505 len = dlen; 270 len = dlen;
506 } 271 }
516 u_fsync (h); 281 u_fsync (h);
517 u_close (h); 282 u_close (h);
518 } 283 }
519 break; 284 break;
520 285
521 case T_NULL: 286 case U_T_NULL:
522 goto done; 287 goto done;
523 } 288 }
524 289
525 pack_next (); 290 pack_next ();
526 } 291 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines