--- Urlader/urlader.c 2011/12/30 09:34:20 1.5 +++ Urlader/urlader.c 2011/12/30 12:21:28 1.6 @@ -3,6 +3,9 @@ #endif #define URLADER_VERSION "1.0" // a decimal number, not a version string +#define MAX_ARGC 32 +#define MAX_ARGS 256 + #include #include #include @@ -17,65 +20,87 @@ #ifdef _WIN32 - #include - //#include - #include - #include - #include - - static DWORD dword; - - #define u_handle HANDLE - #define u_valid(handle) (!!handle) - - #define u_setenv(name,value) SetEnvironmentVariable (name, value) - #define u_mkdir(path) !CreateDirectory (path, NULL) - #define u_chdir(path) !SetCurrentDirectory (path) - #define u_rename(fr,to) !MoveFile (fr, to) - #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) - #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) - #define u_close(handle) CloseHandle (handle) - #define u_append(path,add) PathAppend (path, add) - #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) - - #define u_fsync(handle) FlushFileBuffers (handle) - #define u_sync() + #include + //#include + #include + #include + #include + + static DWORD dword; + + #define u_handle HANDLE + #define u_invalid_handle 0 + #define u_valid(handle) (!!handle) + + #define u_setenv(name,value) SetEnvironmentVariable (name, value) + #define u_mkdir(path) !CreateDirectory (path, NULL) + #define u_chdir(path) !SetCurrentDirectory (path) + #define u_rename(fr,to) !MoveFile (fr, to) + #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) + #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) + #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) + #define u_close(handle) CloseHandle (handle) + #define u_append(path,add) PathAppend (path, add) + #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) + + #define u_fsync(handle) FlushFileBuffers (handle) + #define u_sync() + + #define u_lockfile(path) CreateFile (path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) + #define u_cloexec(handle) + // acquire shared in addition to excl, then unlock excl + #define u_lock_write_to_read(wait) u_lock (U_LOCK_READ, 1); u_lock (U_UNLOCK, 1); #else - #include - #include - #include - - #ifdef PATH_MAX - #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX) - #else - #define MAX_PATH 4096 - #endif - - #define u_handle int - #define u_valid(fd) ((fd) >= 0) - - #define u_setenv(name,value) setenv (name, value, 1) - #define u_mkdir(path) mkdir (path, 0777) - #define u_chdir(path) chdir (path) - #define u_rename(fr,to) rename (fr, to) - #define u_open(path) open (path, O_RDONLY) - #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) - #define u_close(handle) close (handle) - #define u_append(path,add) strcat (strcat (path, "/"), add) - #define u_write(handle,data,len) write (handle, data, len) - - // on a mostly idle system, a sync at the end is certainly faster, hope for the best - #define u_fsync(handle) - #define u_sync() sync () + #define _GNU_SOURCE 1 + #define _BSD_SOURCE 1 + // the above increases our chances of getting MAP_ANONYMOUS + + #include + #include + #include + + #if defined (MAP_ANON) && !defined (MAP_ANONYMOUS) + #define MAP_ANONYMOUS MAP_ANON + #endif + + #ifdef PATH_MAX + #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX) + #else + #define MAX_PATH 4096 + #endif + + #define u_handle int + #define u_invalid_handle -1 + #define u_valid(fd) ((fd) >= 0) + + #define u_setenv(name,value) setenv (name, value, 1) + #define u_mkdir(path) mkdir (path, 0777) + #define u_chdir(path) chdir (path) + #define u_rename(fr,to) rename (fr, to) + #define u_open(path) open (path, O_RDONLY) + #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) + #define u_close(handle) close (handle) + #define u_append(path,add) strcat (strcat (path, "/"), add) + #define u_write(handle,data,len) write (handle, data, len) + + // on a mostly idle system, a sync at the end is certainly faster, hope for the best + #define u_fsync(handle) + #define u_sync() sync () + + #define u_lockfile(path) open (path, O_RDWR | O_CREAT, 0666) + #define u_cloexec(handle) fcntl (handle, F_SETFD, FD_CLOEXEC) + #define u_lock_write_to_read(wait) u_lock (U_LOCK_READ, 1); #endif #define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1]) #define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3]) +/* some simple? dynamic memory management for paths might save ltos of ram */ static u_handle pack_handle; +static u_handle lock_handle; static char tmppath[MAX_PATH]; static char currdir[MAX_PATH]; static char datadir[MAX_PATH]; // %AppData%/urlader @@ -85,7 +110,9 @@ static char exe_ver[MAX_PATH]; static int exe_argc; -static const char *exe_argv[32]; +static const char *exe_argv[MAX_ARGC]; +static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ +static unsigned int exe_argo; static void fatal (const char *msg) @@ -93,12 +120,86 @@ #ifdef _WIN32 MessageBox (0, msg, URLADER, 0); #else - fprintf (stderr, "%s: %s\n", URLADER, msg); + write (2, URLADER ": ", sizeof (URLADER ": ") - 1); + write (2, msg, strlen (msg)); + write (2, "\n", 1); #endif _exit (1); } +static void * +u_malloc (unsigned int size) +{ + void *addr; + +#ifdef _WIN32 + HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL); + + if (!handle) + return 0; + + addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size); + + CloseHandle (handle); +#elif defined (MAP_ANONYMOUS) + addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + + if (addr == (void *)-1) + addr = 0; +#else + addr = malloc (size); +#endif + + return addr; +} + +static void +u_free (void *addr, unsigned int size) +{ +#ifdef _WIN32 + UnmapViewOfFile (addr); +#elif defined (MAP_ANONYMOUS) + munmap (addr, size); +#else + free (addr); +#endif +} + +static void * +u_mmap (u_handle h, unsigned int size) +{ + void *addr; + +#ifdef _WIN32 + HANDLE handle = CreateFileMapping (h, 0, PAGE_READONLY, 0, size, NULL); + + if (!handle) + return 0; + + addr = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, size); + + CloseHandle (handle); +#else + addr = mmap (0, size, PROT_READ, MAP_SHARED, h, 0); + + if (addr == (void *)-1) + addr = 0; +#endif + + return addr; +} + +static void +u_munmap (void *addr, unsigned int len) +{ +#ifdef _WIN32 + UnmapViewOfFile (addr); +#else + munmap (addr, len); +#endif +} + static void tmpdir (const char *dir) { @@ -117,13 +218,50 @@ } } +enum +{ + U_UNLOCK, + U_LOCK_READ, + U_LOCK_WRITE +}; + +static int +u_lock (int mode, int dowait) +{ +#ifdef _WIN32 + if (mode == U_UNLOCK) + return !UnlockFile (lock_handle, 0, 0, 1, 0); + else + { + OVERLAPPED ov = { 0 }; + + return !LockFileEx (lock_handle, + 0 + | (mode == U_LOCK_WRITE ? LOCKFILE_EXCLUSIVE_LOCK : 0) + | (dowait ? 0 : LOCKFILE_FAIL_IMMEDIATELY), + 0, + 1, 0, + &ov); + } +#else + static short mode2lck [] = { F_UNLCK, F_RDLCK, F_WRLCK }; + struct flock lck; + lck.l_type = mode2lck [mode]; + lck.l_whence = SEEK_SET; + lck.l_start = 0; + lck.l_len = 1; + + return fcntl (lock_handle, dowait ? F_SETLKW : F_SETLK, &lck); +#endif +} + static void -systemv (const char *const argv[], int dowait) +systemv (const char *const argv[]) { #ifdef _WIN32 _spawnv (P_WAIT, argv [0], argv); #else - pid_t pid = dowait ? fork () : 0; + pid_t pid = fork (); if (pid < 0) fatal ("fork failure"); @@ -149,7 +287,7 @@ #else const char *argv[] = { "/bin/rm", "-rf", path, 0 }; #endif - systemv (argv, 1); + systemv (argv); } enum @@ -187,7 +325,7 @@ static char *pack_base, *pack_end; static struct hdr *pack_cur; static char *scratch; -static unsigned int scratch_len; +static unsigned int scratch_size; #define PACK_NAME ((char *)(pack_cur + 1)) #define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) @@ -204,56 +342,44 @@ static void pack_unmap (void) { - if (!pack_base) - return; - - free (scratch); + if (pack_base) + { + u_munmap (pack_base, pack_end - pack_base); + pack_base = 0; + } -#ifdef _WIN32 - UnmapViewOfFile (pack_base); -#else - munmap (pack_base, pack_end - pack_base); -#endif + if (scratch) + { + u_free (scratch, scratch_size); + scratch = 0; + } } static int pack_map (void) { + char *addr; + unsigned int size; + #ifdef _WIN32 BY_HANDLE_FILE_INFORMATION fi; if (!GetFileInformationByHandle (pack_handle, &fi)) return 0; - if (fi.nFileSizeHigh) // too big - return 0; - - HANDLE handle = CreateFileMapping (pack_handle, 0, PAGE_READONLY, 0, fi.nFileSizeLow, NULL); + size = fi.nFileSizeLow; +#else + size = lseek (pack_handle, 0, SEEK_END); +#endif - if (!handle) + addr = u_mmap (pack_handle, size); + if (!addr) return 0; pack_unmap (); - pack_base = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, fi.nFileSizeLow); - - CloseHandle (handle); - - pack_end = pack_base + fi.nFileSizeLow; -#else - off_t len = lseek (pack_handle - 1, 0, SEEK_END); - - pack_unmap (); - - pack_base = mmap (0, len, PROT_READ, MAP_SHARED, pack_handle - 1, 0); - if (pack_base == (void *)-1) - pack_base = 0; - - pack_end = pack_base + len; -#endif - - if (!pack_base) - return 0; + pack_base = addr; + pack_end = pack_base + size; struct tail *tail; @@ -264,8 +390,9 @@ pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); - free (scratch); - scratch = malloc (scratch_len = u_32 (tail->max_filesize)); + scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); + if (!scratch) + return 0; return 1; } @@ -320,7 +447,12 @@ if (pack_cur->type == T_ENV) u_setenv (PACK_NAME, PACK_DATA); else if (pack_cur->type == T_ARG) - exe_argv [exe_argc++] = strdup (PACK_NAME); + { + int len = u_16 (pack_cur->namelen) + 1; + exe_argv [exe_argc++] = exe_args + exe_argo; + memcpy (exe_args + exe_argo, PACK_NAME, len); + exe_argo += len; + } else break; @@ -332,6 +464,18 @@ u_append (execdir, "i-"); strcat (execdir, exe_ver); + strcat (strcpy (tmppath, execdir), ".lck"); + lock_handle = u_lockfile (tmppath); + if (!lock_handle) + fatal ("unable to create lock file"); + + u_cloexec (lock_handle); + + // all users that uee an instance acquire a shared lock + // acquiring an exclusive lock is enough to delete the dir + // deleeting the lockfile is another topic altogether + u_lock (U_LOCK_READ, 1); + if (access (execdir, F_OK)) { // does not exist yet, so unpack and move @@ -355,7 +499,7 @@ char *data = PACK_DATA; if (pack_cur->flags & F_LZF) - if (dlen = lzf_decompress (data, len, scratch, scratch_len)) + if (dlen = lzf_decompress (data, len, scratch, scratch_size)) { data = scratch; len = dlen; @@ -421,7 +565,7 @@ execute (void) { exe_argv [exe_argc] = 0; - systemv (exe_argv, 0); + systemv (exe_argv); } #ifdef _WIN32 @@ -480,6 +624,7 @@ strcpy (currdir, "."); u_mkdir (home); + //strcat (strcat (strcpy (datadir, home), "/."), URLADER); sprintf (datadir, "%s/.%s", home, URLADER); u_mkdir (datadir);