]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Call OpenProcess() from within __archive_create_child()
authorEmil Velikov <emil.l.velikov@gmail.com>
Sat, 25 Apr 2020 14:42:45 +0000 (15:42 +0100)
committerEmil Velikov <emil.l.velikov@gmail.com>
Sat, 25 Apr 2020 19:12:25 +0000 (20:12 +0100)
Move the Windows specific to the Windows version of the helper, making
the existing code shorter and simpler.

This means that the child "handle" type will vary across the two
platforms - something that we've already been doing with waitpid.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
libarchive/archive_read_support_filter_program.c
libarchive/archive_write_add_filter_program.c
libarchive/filter_fork.h
libarchive/filter_fork_windows.c

index 29c8a489a211295314a40fef6da85c3d147611f3..bf5b6f2b3c8bd474478de76ca900e8211e158d47 100644 (file)
@@ -400,7 +400,6 @@ __archive_read_program(struct archive_read_filter *self, const char *cmd)
        static const size_t out_buf_len = 65536;
        char *out_buf;
        const char *prefix = "Program: ";
-       pid_t child;
        int ret;
        size_t l;
 
@@ -428,7 +427,7 @@ __archive_read_program(struct archive_read_filter *self, const char *cmd)
        state->out_buf_len = out_buf_len;
 
        ret = __archive_create_child(cmd, &state->child_stdin,
-           &state->child_stdout, &child);
+           &state->child_stdout, &state->child);
        if (ret != ARCHIVE_OK) {
                free(state->out_buf);
                archive_string_free(&state->description);
@@ -438,21 +437,6 @@ __archive_read_program(struct archive_read_filter *self, const char *cmd)
                    cmd);
                return (ARCHIVE_FATAL);
        }
-#if defined(_WIN32) && !defined(__CYGWIN__)
-       state->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child);
-       if (state->child == NULL) {
-               child_stop(self, state);
-               free(state->out_buf);
-               archive_string_free(&state->description);
-               free(state);
-               archive_set_error(&self->archive->archive, EINVAL,
-                   "Can't initialize filter; unable to run program \"%s\"",
-                   cmd);
-               return (ARCHIVE_FATAL);
-       }
-#else
-       state->child = child;
-#endif
 
        self->data = state;
        self->read = program_filter_read;
index f64ae4a20b40deed0f16d59fe60db51a8f7efd53..3dac86dc3bded94b2fccc605d75f2b56b3b271e8 100644 (file)
@@ -211,7 +211,6 @@ int
 __archive_write_program_open(struct archive_write_filter *f,
     struct archive_write_program_data *data, const char *cmd)
 {
-       pid_t child;
        int ret;
 
        if (data->child_buf == NULL) {
@@ -227,26 +226,12 @@ __archive_write_program_open(struct archive_write_filter *f,
        }
 
        ret = __archive_create_child(cmd, &data->child_stdin,
-                   &data->child_stdout, &child);
+                   &data->child_stdout, &data->child);
        if (ret != ARCHIVE_OK) {
                archive_set_error(f->archive, EINVAL,
                    "Can't launch external program: %s", cmd);
                return (ARCHIVE_FATAL);
        }
-#if defined(_WIN32) && !defined(__CYGWIN__)
-       data->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child);
-       if (data->child == NULL) {
-               close(data->child_stdin);
-               data->child_stdin = -1;
-               close(data->child_stdout);
-               data->child_stdout = -1;
-               archive_set_error(f->archive, EINVAL,
-                   "Can't launch external program: %s", cmd);
-               return (ARCHIVE_FATAL);
-       }
-#else
-       data->child = child;
-#endif
        return (ARCHIVE_OK);
 }
 
index be1bc7e8e320dcecfebd3a62fed63152112face1..2bf290c4d9e4596019015b6eff94cc6f83d92e2c 100644 (file)
 
 int
 __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
+#if defined(_WIN32) && !defined(__CYGWIN__)
+       HANDLE *out_child);
+#else
        pid_t *out_child);
+#endif
 
 void
 __archive_check_child(int in, int out);
index 7db1a0018b9aebe8b20ea0b218c3858771eae447..8d11179f307ecc03f59dc7ac09d3247862cc028e 100644 (file)
@@ -33,7 +33,7 @@
 
 int
 __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
-               pid_t *out_child)
+               HANDLE *out_child)
 {
        HANDLE childStdout[2], childStdin[2],childStderr;
        SECURITY_ATTRIBUTES secAtts;
@@ -45,6 +45,7 @@ __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
        char *arg0, *ext;
        int i, l;
        DWORD fl, fl_old;
+       HANDLE child;
 
        childStdout[0] = childStdout[1] = INVALID_HANDLE_VALUE;
        childStdin[0] = childStdin[1] = INVALID_HANDLE_VALUE;
@@ -155,13 +156,19 @@ __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
        *child_stdout = _open_osfhandle((intptr_t)childStdout[0], _O_RDONLY);
        *child_stdin = _open_osfhandle((intptr_t)childStdin[1], _O_WRONLY);
        
+       child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
+               childInfo.dwProcessId);
+       if (child == NULL) // INVALID_HANDLE_VALUE ?
+               goto fail;
+
+       *out_child = child;
+
        CloseHandle(childStdout[1]);
        CloseHandle(childStdin[0]);
 
        archive_string_free(&cmdline);
        archive_string_free(&fullpath);
        __archive_cmdline_free(acmd);
-       *out_child = childInfo.dwProcessId;
        return ARCHIVE_OK;
 
 fail: