diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/Makefile.am | 3 | ||||
-rw-r--r-- | src/main/extract.c | 4 | ||||
-rw-r--r-- | src/main/extractor.c | 175 |
3 files changed, 175 insertions, 7 deletions
diff --git a/src/main/Makefile.am b/src/main/Makefile.am index 9fdc87f..e8f4932 100644 --- a/src/main/Makefile.am +++ b/src/main/Makefile.am | |||
@@ -17,6 +17,7 @@ extract_LDADD = \ | |||
17 | $(top_builddir)/src/main/libextractor.la | 17 | $(top_builddir)/src/main/libextractor.la |
18 | 18 | ||
19 | if !MINGW | 19 | if !MINGW |
20 | rtflag=-lrt | ||
20 | if !SOMEBSD | 21 | if !SOMEBSD |
21 | dlflag=-ldl | 22 | dlflag=-ldl |
22 | endif | 23 | endif |
@@ -32,7 +33,7 @@ endif | |||
32 | libextractor_la_LDFLAGS = \ | 33 | libextractor_la_LDFLAGS = \ |
33 | $(LE_LIB_LDFLAGS) -version-info @LIB_VERSION_CURRENT@:@LIB_VERSION_REVISION@:@LIB_VERSION_AGE@ | 34 | $(LE_LIB_LDFLAGS) -version-info @LIB_VERSION_CURRENT@:@LIB_VERSION_REVISION@:@LIB_VERSION_AGE@ |
34 | libextractor_la_LIBADD = \ | 35 | libextractor_la_LIBADD = \ |
35 | $(LIBLTDL) $(dlflag) $(zlib) $(bz2lib) $(LIBICONV) -lrt | 36 | $(LIBLTDL) $(dlflag) $(zlib) $(bz2lib) $(LIBICONV) $(rtflag) |
36 | 37 | ||
37 | 38 | ||
38 | EXTRA_DIST = \ | 39 | EXTRA_DIST = \ |
diff --git a/src/main/extract.c b/src/main/extract.c index d07ec1c..c310eee 100644 --- a/src/main/extract.c +++ b/src/main/extract.c | |||
@@ -49,6 +49,7 @@ catcher (int sig) | |||
49 | { | 49 | { |
50 | } | 50 | } |
51 | 51 | ||
52 | #ifndef WINDOWS | ||
52 | /** | 53 | /** |
53 | * Install a signal handler to ignore SIGPIPE. | 54 | * Install a signal handler to ignore SIGPIPE. |
54 | */ | 55 | */ |
@@ -69,6 +70,7 @@ ignore_sigpipe () | |||
69 | fprintf (stderr, | 70 | fprintf (stderr, |
70 | "Failed to install SIGPIPE handler: %s\n", strerror (errno)); | 71 | "Failed to install SIGPIPE handler: %s\n", strerror (errno)); |
71 | } | 72 | } |
73 | #endif | ||
72 | 74 | ||
73 | 75 | ||
74 | 76 | ||
@@ -563,7 +565,9 @@ main (int argc, char *argv[]) | |||
563 | setlocale(LC_ALL, ""); | 565 | setlocale(LC_ALL, ""); |
564 | textdomain(PACKAGE); | 566 | textdomain(PACKAGE); |
565 | #endif | 567 | #endif |
568 | #ifndef WINDOWS | ||
566 | ignore_sigpipe (); | 569 | ignore_sigpipe (); |
570 | #endif | ||
567 | print = malloc (sizeof (int) * EXTRACTOR_metatype_get_max ()); | 571 | print = malloc (sizeof (int) * EXTRACTOR_metatype_get_max ()); |
568 | for (i = 0; i < EXTRACTOR_metatype_get_max (); i++) | 572 | for (i = 0; i < EXTRACTOR_metatype_get_max (); i++) |
569 | print[i] = YES; /* default: print everything */ | 573 | print[i] = YES; /* default: print everything */ |
diff --git a/src/main/extractor.c b/src/main/extractor.c index 38d8721..e44f5bd 100644 --- a/src/main/extractor.c +++ b/src/main/extractor.c | |||
@@ -19,11 +19,14 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | #include "platform.h" | 21 | #include "platform.h" |
22 | #include "plibc.h" | ||
22 | #include "extractor.h" | 23 | #include "extractor.h" |
23 | #include <dirent.h> | 24 | #include <dirent.h> |
24 | #include <sys/types.h> | 25 | #include <sys/types.h> |
26 | #ifndef WINDOWS | ||
25 | #include <sys/wait.h> | 27 | #include <sys/wait.h> |
26 | #include <sys/shm.h> | 28 | #include <sys/shm.h> |
29 | #endif | ||
27 | #include <signal.h> | 30 | #include <signal.h> |
28 | 31 | ||
29 | 32 | ||
@@ -120,20 +123,31 @@ struct EXTRACTOR_PluginList | |||
120 | * Process ID of the child process for this plugin. 0 for | 123 | * Process ID of the child process for this plugin. 0 for |
121 | * none. | 124 | * none. |
122 | */ | 125 | */ |
126 | #ifndef WINDOWS | ||
123 | pid_t cpid; | 127 | pid_t cpid; |
128 | #else | ||
129 | DWORD cpid; | ||
130 | #endif | ||
124 | 131 | ||
125 | /** | 132 | /** |
126 | * Pipe used to send information about shared memory segments to | 133 | * Pipe used to send information about shared memory segments to |
127 | * the child process. NULL if not initialized. | 134 | * the child process. NULL if not initialized. |
128 | */ | 135 | */ |
136 | #ifndef WINDOWS | ||
129 | FILE *cpipe_in; | 137 | FILE *cpipe_in; |
138 | #else | ||
139 | HANDLE cpipe_in; | ||
140 | #endif | ||
130 | 141 | ||
131 | /** | 142 | /** |
132 | * Pipe used to read information about extracted meta data from | 143 | * Pipe used to read information about extracted meta data from |
133 | * the child process. -1 if not initialized. | 144 | * the child process. -1 if not initialized. |
134 | */ | 145 | */ |
146 | #ifndef WINDOWS | ||
135 | int cpipe_out; | 147 | int cpipe_out; |
136 | 148 | #else | |
149 | HANDLE cpipe_out; | ||
150 | #endif | ||
137 | }; | 151 | }; |
138 | 152 | ||
139 | 153 | ||
@@ -874,6 +888,9 @@ static void | |||
874 | stop_process (struct EXTRACTOR_PluginList *plugin) | 888 | stop_process (struct EXTRACTOR_PluginList *plugin) |
875 | { | 889 | { |
876 | int status; | 890 | int status; |
891 | #ifdef WINDOWS | ||
892 | HANDLE process; | ||
893 | #endif | ||
877 | 894 | ||
878 | #if DEBUG | 895 | #if DEBUG |
879 | if (plugin->cpid == -1) | 896 | if (plugin->cpid == -1) |
@@ -884,12 +901,23 @@ stop_process (struct EXTRACTOR_PluginList *plugin) | |||
884 | if ( (plugin->cpid == -1) || | 901 | if ( (plugin->cpid == -1) || |
885 | (plugin->cpid == 0) ) | 902 | (plugin->cpid == 0) ) |
886 | return; | 903 | return; |
904 | #ifndef WINDOWS | ||
887 | kill (plugin->cpid, SIGKILL); | 905 | kill (plugin->cpid, SIGKILL); |
888 | waitpid (plugin->cpid, &status, 0); | 906 | waitpid (plugin->cpid, &status, 0); |
907 | #else | ||
908 | process = OpenProcess (PROCESS_TERMINATE, FALSE, plugin->cpid); | ||
909 | TerminateProcess (process, 0); | ||
910 | CloseHandle (process); | ||
911 | #endif | ||
889 | plugin->cpid = -1; | 912 | plugin->cpid = -1; |
913 | #ifndef WINDOWS | ||
890 | close (plugin->cpipe_out); | 914 | close (plugin->cpipe_out); |
891 | plugin->cpipe_out = -1; | ||
892 | fclose (plugin->cpipe_in); | 915 | fclose (plugin->cpipe_in); |
916 | #else | ||
917 | CloseHandle (plugin->cpipe_out); | ||
918 | CloseHandle (plugin->cpipe_in); | ||
919 | #endif | ||
920 | plugin->cpipe_out = -1; | ||
893 | plugin->cpipe_in = NULL; | 921 | plugin->cpipe_in = NULL; |
894 | } | 922 | } |
895 | 923 | ||
@@ -975,7 +1003,12 @@ write_all (int fd, | |||
975 | 1003 | ||
976 | 1004 | ||
977 | static int | 1005 | static int |
978 | read_all (int fd, | 1006 | read_all ( |
1007 | #ifndef WINDOWS | ||
1008 | int fd, | ||
1009 | #else | ||
1010 | HANDLE fd, | ||
1011 | #endif | ||
979 | void *buf, | 1012 | void *buf, |
980 | size_t size) | 1013 | size_t size) |
981 | { | 1014 | { |
@@ -985,7 +1018,11 @@ read_all (int fd, | |||
985 | 1018 | ||
986 | while (off < size) | 1019 | while (off < size) |
987 | { | 1020 | { |
1021 | #ifndef WINDOWS | ||
988 | ret = read (fd, &data[off], size - off); | 1022 | ret = read (fd, &data[off], size - off); |
1023 | #else | ||
1024 | ReadFile (fd, &data[off], size - off, &ret, NULL); | ||
1025 | #endif | ||
989 | if (ret <= 0) | 1026 | if (ret <= 0) |
990 | return -1; | 1027 | return -1; |
991 | off += ret; | 1028 | off += ret; |
@@ -1082,6 +1119,9 @@ process_requests (struct EXTRACTOR_PluginList *plugin, | |||
1082 | int shmid; | 1119 | int shmid; |
1083 | struct IpcHeader hdr; | 1120 | struct IpcHeader hdr; |
1084 | size_t size; | 1121 | size_t size; |
1122 | #ifdef WINDOWS | ||
1123 | HANDLE map; | ||
1124 | #endif | ||
1085 | 1125 | ||
1086 | if (0 != plugin_load (plugin)) | 1126 | if (0 != plugin_load (plugin)) |
1087 | { | 1127 | { |
@@ -1115,10 +1155,16 @@ process_requests (struct EXTRACTOR_PluginList *plugin, | |||
1115 | break; | 1155 | break; |
1116 | ptr = NULL; | 1156 | ptr = NULL; |
1117 | fn[strlen(fn)-1] = '\0'; /* kill newline */ | 1157 | fn[strlen(fn)-1] = '\0'; /* kill newline */ |
1158 | #ifndef WINDOWS | ||
1118 | if ( (-1 != (shmid = shm_open (fn, O_RDONLY, 0))) && | 1159 | if ( (-1 != (shmid = shm_open (fn, O_RDONLY, 0))) && |
1119 | (((off_t)-1) != (size = lseek (shmid, 0, SEEK_END))) && | 1160 | (((off_t)-1) != (size = lseek (shmid, 0, SEEK_END))) && |
1120 | (NULL != (ptr = mmap (NULL, size, PROT_READ, MAP_SHARED, shmid, 0))) && | 1161 | (NULL != (ptr = mmap (NULL, size, PROT_READ, MAP_SHARED, shmid, 0))) && |
1121 | (ptr != (void*) -1) ) | 1162 | (ptr != (void*) -1) ) |
1163 | #else | ||
1164 | map = OpenFileMapping (PAGE_READONLY, FALSE, fn); | ||
1165 | ptr = MapViewOfFile (map, FILE_MAP_READ, 0, 0, 0); | ||
1166 | if (ptr != NULL) | ||
1167 | #endif | ||
1122 | { | 1168 | { |
1123 | if (0 != plugin->extractMethod (ptr, | 1169 | if (0 != plugin->extractMethod (ptr, |
1124 | size, | 1170 | size, |
@@ -1129,11 +1175,18 @@ process_requests (struct EXTRACTOR_PluginList *plugin, | |||
1129 | if (0 != write_all (out, &hdr, sizeof(hdr))) | 1175 | if (0 != write_all (out, &hdr, sizeof(hdr))) |
1130 | break; | 1176 | break; |
1131 | } | 1177 | } |
1178 | #ifndef WINDOWS | ||
1132 | if ( (ptr != NULL) && | 1179 | if ( (ptr != NULL) && |
1133 | (ptr != (void*) -1) ) | 1180 | (ptr != (void*) -1) ) |
1134 | munmap (ptr, size); | 1181 | munmap (ptr, size); |
1135 | if (-1 != shmid) | 1182 | if (-1 != shmid) |
1136 | close (shmid); | 1183 | close (shmid); |
1184 | #else | ||
1185 | if (ptr != NULL && ptr != (void*) -1) | ||
1186 | UnmapViewOfFile (ptr); | ||
1187 | if (map != NULL) | ||
1188 | CloseHandle (map); | ||
1189 | #endif | ||
1137 | if ( (plugin->specials != NULL) && | 1190 | if ( (plugin->specials != NULL) && |
1138 | (NULL != strstr (plugin->specials, | 1191 | (NULL != strstr (plugin->specials, |
1139 | "force-kill")) ) | 1192 | "force-kill")) ) |
@@ -1147,6 +1200,59 @@ process_requests (struct EXTRACTOR_PluginList *plugin, | |||
1147 | close (out); | 1200 | close (out); |
1148 | } | 1201 | } |
1149 | 1202 | ||
1203 | #ifdef WINDOWS | ||
1204 | void write_plugin_data (HANDLE h, const struct EXTRACTOR_PluginList *plugin) | ||
1205 | { | ||
1206 | size_t i; | ||
1207 | |||
1208 | i = strlen (plugin->libname); | ||
1209 | WriteFile (h, &i, sizeof (size_t), NULL, NULL); | ||
1210 | WriteFile (h, plugin->libname, i, NULL, NULL); | ||
1211 | |||
1212 | i = strlen (plugin->short_libname); | ||
1213 | WriteFile (h, &i, sizeof (size_t), NULL, NULL); | ||
1214 | WriteFile (h, plugin->short_libname,i, NULL, NULL); | ||
1215 | |||
1216 | i = strlen (plugin->plugin_options); | ||
1217 | WriteFile (h, &i, sizeof (size_t), NULL, NULL); | ||
1218 | WriteFile (h, plugin->plugin_options, i, NULL, NULL); | ||
1219 | } | ||
1220 | |||
1221 | struct EXTRACTOR_PluginList *read_plugin_data (FILE *f) | ||
1222 | { | ||
1223 | struct EXTRACTOR_PluginList *ret; | ||
1224 | size_t i; | ||
1225 | |||
1226 | ret = malloc (sizeof (struct EXTRACTOR_PluginList)); | ||
1227 | |||
1228 | fread (&i, sizeof (size_t), 1, f); | ||
1229 | ret->libname = malloc (i); | ||
1230 | fread (ret->libname, i, 1, f); | ||
1231 | |||
1232 | fread (&i, sizeof (size_t), 1, f); | ||
1233 | ret->short_libname = malloc (i); | ||
1234 | fread (ret->short_libname, i, 1, f); | ||
1235 | |||
1236 | fread (&i, sizeof (size_t), 1, f); | ||
1237 | ret->plugin_options = malloc (i); | ||
1238 | fread (ret->plugin_options, i, 1, f); | ||
1239 | |||
1240 | return ret; | ||
1241 | } | ||
1242 | |||
1243 | void CALLBACK RundllEntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) | ||
1244 | { | ||
1245 | int in, out; | ||
1246 | |||
1247 | in = fileno (stdin); | ||
1248 | out = fileno (stdout); | ||
1249 | |||
1250 | setmode (in, _O_BINARY); | ||
1251 | setmode (out, _O_BINARY); | ||
1252 | |||
1253 | process_requests (read_plugin_data (stdin), in, out); | ||
1254 | } | ||
1255 | #endif | ||
1150 | 1256 | ||
1151 | /** | 1257 | /** |
1152 | * Start the process for the given plugin. | 1258 | * Start the process for the given plugin. |
@@ -1154,6 +1260,7 @@ process_requests (struct EXTRACTOR_PluginList *plugin, | |||
1154 | static void | 1260 | static void |
1155 | start_process (struct EXTRACTOR_PluginList *plugin) | 1261 | start_process (struct EXTRACTOR_PluginList *plugin) |
1156 | { | 1262 | { |
1263 | #ifndef WINDOWS | ||
1157 | int p1[2]; | 1264 | int p1[2]; |
1158 | int p2[2]; | 1265 | int p2[2]; |
1159 | pid_t pid; | 1266 | pid_t pid; |
@@ -1199,6 +1306,22 @@ start_process (struct EXTRACTOR_PluginList *plugin) | |||
1199 | if (plugin->cpipe_in == NULL) | 1306 | if (plugin->cpipe_in == NULL) |
1200 | perror ("fdopen"); | 1307 | perror ("fdopen"); |
1201 | plugin->cpipe_out = p2[0]; | 1308 | plugin->cpipe_out = p2[0]; |
1309 | #else | ||
1310 | STARTUPINFO startup; | ||
1311 | PROCESS_INFORMATION proc; | ||
1312 | |||
1313 | memset (&startup, 0, sizeof (STARTUPINFO)); | ||
1314 | CreatePipe (&startup.hStdInput, &plugin->cpipe_in, NULL, 0); | ||
1315 | CreatePipe (&plugin->cpipe_out, &startup.hStdOutput, NULL, 0); | ||
1316 | |||
1317 | write_plugin_data (plugin->cpipe_in, plugin); | ||
1318 | |||
1319 | // FIXME library name | ||
1320 | CreateProcess (NULL, "rundll32 libextractor-1.dll,RundllEntryPoint", NULL, NULL, FALSE, 0, NULL, NULL, &startup, &proc); | ||
1321 | CloseHandle (proc.hProcess); | ||
1322 | CloseHandle (proc.hThread); | ||
1323 | plugin->cpid = proc.dwProcessId; | ||
1324 | #endif | ||
1202 | } | 1325 | } |
1203 | 1326 | ||
1204 | 1327 | ||
@@ -1315,7 +1438,11 @@ extract (struct EXTRACTOR_PluginList *plugins, | |||
1315 | void *proc_cls) | 1438 | void *proc_cls) |
1316 | { | 1439 | { |
1317 | struct EXTRACTOR_PluginList *ppos; | 1440 | struct EXTRACTOR_PluginList *ppos; |
1441 | #ifndef WINDOWS | ||
1318 | int shmid; | 1442 | int shmid; |
1443 | #else | ||
1444 | HANDLE map, mappedFile; | ||
1445 | #endif | ||
1319 | enum EXTRACTOR_Options flags; | 1446 | enum EXTRACTOR_Options flags; |
1320 | void *ptr; | 1447 | void *ptr; |
1321 | char fn[255]; | 1448 | char fn[255]; |
@@ -1349,9 +1476,15 @@ extract (struct EXTRACTOR_PluginList *plugins, | |||
1349 | { | 1476 | { |
1350 | snprintf (fn, | 1477 | snprintf (fn, |
1351 | sizeof(fn), | 1478 | sizeof(fn), |
1352 | "/libextractor-shm-%u-%u", | 1479 | #ifdef WINDOWS |
1480 | "%TEMP%\\" | ||
1481 | #else | ||
1482 | "/" | ||
1483 | #endif | ||
1484 | "libextractor-shm-%u-%u", | ||
1353 | getpid(), | 1485 | getpid(), |
1354 | (unsigned int) random()); | 1486 | (unsigned int) RANDOM()); |
1487 | #ifndef WINDOWS | ||
1355 | shmid = shm_open (fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); | 1488 | shmid = shm_open (fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); |
1356 | ptr = NULL; | 1489 | ptr = NULL; |
1357 | if (shmid != -1) | 1490 | if (shmid != -1) |
@@ -1368,16 +1501,41 @@ extract (struct EXTRACTOR_PluginList *plugins, | |||
1368 | memcpy (ptr, data, size); | 1501 | memcpy (ptr, data, size); |
1369 | } | 1502 | } |
1370 | } | 1503 | } |
1504 | #else | ||
1505 | mappedFile = CreateFile (fn, GENERIC_READ | GENERIC_WRITE, | ||
1506 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, | ||
1507 | FILE_FLAG_DELETE_ON_CLOSE, NULL); | ||
1508 | map = CreateFileMapping (mappedFile, NULL, PAGE_READWRITE, 1, 0, NULL); | ||
1509 | ptr = MapViewOfFile (map, FILE_MAP_READ, 0, 0, 0); | ||
1510 | if (ptr == NULL) | ||
1511 | { | ||
1512 | CloseHandle (map); | ||
1513 | CloseHandle (mappedFile); | ||
1514 | map = NULL; | ||
1515 | } | ||
1516 | else | ||
1517 | memcpy (ptr, data, size); | ||
1518 | #endif | ||
1371 | } | 1519 | } |
1372 | else | 1520 | else |
1521 | #ifndef WINDOWS | ||
1373 | shmid = -1; | 1522 | shmid = -1; |
1374 | if (want_shm && (shmid == -1)) | 1523 | if (want_shm && (shmid == -1)) |
1375 | _exit(1); | 1524 | _exit(1); |
1525 | #else | ||
1526 | map = NULL; | ||
1527 | if (want_shm && map == NULL) | ||
1528 | _exit(1); | ||
1529 | #endif | ||
1376 | ppos = plugins; | 1530 | ppos = plugins; |
1377 | while (NULL != ppos) | 1531 | while (NULL != ppos) |
1378 | { | 1532 | { |
1379 | flags = ppos->flags; | 1533 | flags = ppos->flags; |
1534 | #ifndef WINDOWS | ||
1380 | if (shmid == -1) | 1535 | if (shmid == -1) |
1536 | #else | ||
1537 | if (map == NULL) | ||
1538 | #endif | ||
1381 | flags = EXTRACTOR_OPTION_IN_PROCESS; | 1539 | flags = EXTRACTOR_OPTION_IN_PROCESS; |
1382 | switch (flags) | 1540 | switch (flags) |
1383 | { | 1541 | { |
@@ -1416,11 +1574,17 @@ extract (struct EXTRACTOR_PluginList *plugins, | |||
1416 | } | 1574 | } |
1417 | if (want_shm) | 1575 | if (want_shm) |
1418 | { | 1576 | { |
1577 | #ifndef WINDOWS | ||
1419 | if (NULL != ptr) | 1578 | if (NULL != ptr) |
1420 | munmap (ptr, size); | 1579 | munmap (ptr, size); |
1421 | if (shmid != -1) | 1580 | if (shmid != -1) |
1422 | close (shmid); | 1581 | close (shmid); |
1423 | shm_unlink (fn); | 1582 | shm_unlink (fn); |
1583 | #else | ||
1584 | UnmapViewOfFile (ptr); | ||
1585 | CloseHandle (map); | ||
1586 | CloseHandle (mappedFile); | ||
1587 | #endif | ||
1424 | } | 1588 | } |
1425 | } | 1589 | } |
1426 | 1590 | ||
@@ -1789,7 +1953,6 @@ EXTRACTOR_extract (struct EXTRACTOR_PluginList *plugins, | |||
1789 | close(fd); | 1953 | close(fd); |
1790 | } | 1954 | } |
1791 | 1955 | ||
1792 | |||
1793 | /** | 1956 | /** |
1794 | * Initialize gettext and libltdl (and W32 if needed). | 1957 | * Initialize gettext and libltdl (and W32 if needed). |
1795 | */ | 1958 | */ |