lib/process: replace pb_run_cmd_pipe
authorJeremy Kerr <jk@ozlabs.org>
Mon, 12 Aug 2013 08:39:36 +0000 (16:39 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 19 Aug 2013 05:27:59 +0000 (13:27 +0800)
Replace pb_run_cmd_pipe with process_create / process_run_sync.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/boot.c
discover/pb-discover.c
lib/pb-config/storage-powerpc-nvram.c
lib/system/system.c
lib/system/system.h
ui/ncurses/nc-cui.c
ui/twin/pbt-client.c

index f9b0223bdf43c42adc30ba439b71ee156eb59a30..d9c606f9a5914acbf79104f1f90e79f337f2a2b3 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <log/log.h>
 #include <pb-protocol/pb-protocol.h>
+#include <process/process.h>
 #include <system/system.h>
 #include <talloc/talloc.h>
 #include <url/url.h>
@@ -244,29 +245,44 @@ static void run_boot_hooks(struct boot_task *task, boot_status_fn status_fn,
 
        for (i = 0; i < n; i++) {
                const char *argv[2] = { NULL, NULL };
-               char *path, *buf;
-               int buf_len, rc;
+               struct process *process;
+               char *path;
+               int rc;
 
                path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
 
-               if (access(path, X_OK))
+               if (access(path, X_OK)) {
+                       talloc_free(path);
                        continue;
+               }
 
-               pb_log("running boot hook %s\n", hooks[i]->d_name);
+               process = process_create(task);
 
                argv[0] = path;
-               rc = pb_run_cmd_pipe(argv, 1, task->dry_run, task,
-                               &buf, &buf_len);
-
-               /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
-                * then we process stdout to look for updated params
-                */
-               if (rc == BOOT_HOOK_EXIT_UPDATE) {
-                       boot_hook_update(task, hooks[i]->d_name, buf);
-                       boot_hook_setenv(task);
+               process->path = path;
+               process->argv = argv;
+               process->keep_stdout = true;
+
+               pb_log("running boot hook %s\n", hooks[i]->d_name);
+
+               rc = process_run_sync(process);
+               if (rc) {
+                       pb_log("boot hook exec failed!\n");
+
+               } else if (WIFEXITED(process->exit_status) &&
+                          WEXITSTATUS(process->exit_status)
+                               == BOOT_HOOK_EXIT_UPDATE) {
+                       /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
+                        * then we process stdout to look for updated params
+                        */
+                       if (rc == BOOT_HOOK_EXIT_UPDATE) {
+                               boot_hook_update(task, hooks[i]->d_name,
+                                               process->stdout_buf);
+                               boot_hook_setenv(task);
+                       }
                }
 
-               talloc_free(buf);
+               process_release(process);
                talloc_free(path);
        }
 
index a0535c2f5e6639a423ffba3bcd534aae408ba8fc..61da5097035ff6aedf12f6ba2c3b6692a790c170 100644 (file)
@@ -12,6 +12,7 @@
 #include <waiter/waiter.h>
 #include <log/log.h>
 #include <pb-config/pb-config.h>
+#include <process/process.h>
 
 #include "udev.h"
 #include "user-event.h"
@@ -118,6 +119,7 @@ int main(int argc, char *argv[])
        struct discover_server *server;
        struct network *network;
        struct waitset *waitset;
+       struct procset *procset;
        struct opts opts;
        struct pb_udev *udev;
        struct user_event *uev;
@@ -166,6 +168,10 @@ int main(int argc, char *argv[])
        if (!server)
                return EXIT_FAILURE;
 
+       procset = process_init(server, waitset);
+       if (!procset)
+               return EXIT_FAILURE;
+
        network = network_init(server, waitset, opts.dry_run == opt_yes);
        if (!network)
                return EXIT_FAILURE;
index 3ed3c46dbd1b716ec0643381f137698f880cfd97..7c8b9e156dfc6a1e4540d24fc3bc4c61f689d52d 100644 (file)
@@ -7,6 +7,7 @@
 #include <talloc/talloc.h>
 #include <list/list.h>
 #include <log/log.h>
+#include <process/process.h>
 
 #include "pb-config.h"
 #include "storage.h"
@@ -117,64 +118,35 @@ static int parse_nvram_params(struct powerpc_nvram_storage *nv,
 
 static int parse_nvram(struct powerpc_nvram_storage *nv)
 {
-       int rc, len, buf_len;
-       int pipefds[2], status;
-       char *buf;
-       pid_t pid;
-
-       rc = pipe(pipefds);
-       if (rc) {
-               perror("pipe");
-               return -1;
-       }
-
-       pid = fork();
-
-       if (pid < 0) {
-               perror("fork");
-               return -1;
-       }
-
-       if (pid == 0) {
-               close(STDIN_FILENO);
-               close(pipefds[0]);
-               dup2(pipefds[1], STDOUT_FILENO);
-               execlp("nvram", "nvram", "--print-config",
-                               "--partition", partition, NULL);
-               exit(EXIT_FAILURE);
-       }
-
-       close(pipefds[1]);
-
-       len = 0;
-       buf_len = max_partition_size;
-       buf = talloc_array(nv, char, buf_len);
+       struct process *process;
+       const char *argv[5];
+       int rc;
 
-       for (;;) {
-               rc = read(pipefds[0], buf + len, buf_len - len);
+       argv[0] = "nvram";
+       argv[1] = "--print-config";
+       argv[2] = "--partition";
+       argv[3] = partition;
+       argv[4] = NULL;
 
-               if (rc < 0) {
-                       perror("read");
-                       break;
-               }
-
-               if (rc == 0)
-                       break;
+       process = process_create(nv);
+       process->path = "nvram";
+       process->argv = argv;
+       process->keep_stdout = true;
 
-               len += rc;
-       }
+       rc = process_run_sync(process);
 
-       waitpid(pid, &status, 0);
-       if (!WIFEXITED(status) || WEXITSTATUS(status)) {
+       if (rc || !WIFEXITED(process->exit_status)
+                       || WEXITSTATUS(process->exit_status)) {
                fprintf(stderr, "nvram process returned "
                                "non-zero exit status\n");
-               return -1;
+               rc = -1;
+       } else {
+               rc = parse_nvram_params(nv, process->stdout_buf,
+                                           process->stdout_len);
        }
 
-       if (rc < 0)
-               return rc;
-
-       return parse_nvram_params(nv, buf, len);
+       process_release(process);
+       return rc;
 }
 
 static const char *get_param(struct powerpc_nvram_storage *nv,
index 528e134127f307f17d9dcfaa70b2b1ef60eb7a52..ff4ae991221a9db0630a446194bbaed5f5cef3ab 100644 (file)
@@ -102,67 +102,22 @@ int pb_rmdir_recursive(const char *base, const char *dir)
        return 0;
 }
 
-static int read_pipe(void *ctx, int fd, char **bufp, int *lenp)
-{
-       int rc, len, alloc_len;
-       char *buf;
-
-       alloc_len = 4096;
-       len = 0;
-
-       buf = talloc_array(ctx, char, alloc_len);
-
-       for (;;) {
-               rc = read(fd, buf, alloc_len - len - 1);
-               if (rc <= 0)
-                       break;
-
-               len += rc;
-               if (len == alloc_len - 1) {
-                       alloc_len *= 2;
-                       buf = talloc_realloc(ctx, buf, char, alloc_len);
-               }
-       }
-
-       if (rc < 0) {
-               talloc_free(buf);
-               return rc;
-       }
-
-       buf[len] = '\0';
-       *bufp = buf;
-       *lenp = len;
-
-       return 0;
-}
-
 /**
  * pb_run_cmd - Run the supplied command.
  * @cmd_argv: An argument list array for execv.
  * @wait: Wait for the child process to complete before returning.
  * @dry_run: Don't actually fork and exec.
  */
-
 int pb_run_cmd(const char *const *cmd_argv, int wait, int dry_run)
-{
-       return pb_run_cmd_pipe(cmd_argv, wait, dry_run, NULL, NULL, NULL);
-}
-
-int pb_run_cmd_pipe(const char *const *cmd_argv, int wait, int dry_run,
-               void *ctx, char **stdout_buf, int *stdout_buf_len)
 {
 #if defined(DEBUG)
        enum {do_debug = 1};
 #else
        enum {do_debug = 0};
 #endif
-       int status, pipefd[2];
+       int status;
        pid_t pid;
 
-       assert(!stdout_buf || wait);
-       assert(!stdout_buf || ctx);
-       assert(!stdout_buf || stdout_buf_len);
-
        if (do_debug) {
                const char *const *p = cmd_argv;
 
@@ -177,22 +132,9 @@ int pb_run_cmd_pipe(const char *const *cmd_argv, int wait, int dry_run,
                pb_log("%s: %s%s\n", __func__, (dry_run ? "(dry-run) " : ""),
                        cmd_argv[0]);
 
-       if (stdout_buf) {
-               *stdout_buf = NULL;
-               *stdout_buf_len = 0;
-       }
-
        if (dry_run)
                return 0;
 
-       if (stdout_buf) {
-               status = pipe(pipefd);
-               if (status) {
-                       pb_log("pipe failed");
-                       return -1;
-               }
-       }
-
        pid = fork();
 
        if (pid == -1) {
@@ -206,13 +148,8 @@ int pb_run_cmd_pipe(const char *const *cmd_argv, int wait, int dry_run,
 
                /* Redirect child output to log. */
 
-               if (stdout_buf) {
-                       status = dup2(pipefd[1], STDOUT_FILENO);
-               } else {
-                       status = dup2(log, STDOUT_FILENO);
-               }
+               status = dup2(log, STDOUT_FILENO);
                assert(status != -1);
-
                status = dup2(log, STDERR_FILENO);
                assert(status != -1);
 
@@ -224,13 +161,6 @@ int pb_run_cmd_pipe(const char *const *cmd_argv, int wait, int dry_run,
        if (!wait && !waitpid(pid, &status, WNOHANG))
                return 0;
 
-       if (stdout_buf) {
-               close(pipefd[1]);
-               status = read_pipe(ctx, pipefd[0], stdout_buf, stdout_buf_len);
-               if (status)
-                       return -1;
-       }
-
        if (waitpid(pid, &status, 0) == -1) {
                pb_log("%s: waitpid failed: %s\n", __func__,
                                strerror(errno));
index a7a1a120c3142756bd05b644b509a446890b707e..271c4355d3066cb46c10fe224abd65fa277f82b8 100644 (file)
@@ -18,8 +18,6 @@ struct pb_system_apps {
 extern const struct pb_system_apps pb_system_apps;
 
 int pb_run_cmd(const char *const *cmd_argv, int wait, int dry_run);
-int pb_run_cmd_pipe(const char *const *cmd_argv, int wait, int dry_run,
-               void *ctx, char **buf, int *buf_len);
 int pb_mkdir_recursive(const char *dir);
 int pb_rmdir_recursive(const char *base, const char *dir);
 
index 71fec5e7d18345a3fc93b815d014400a29c47b86..0dc8d4b9f378dbe8456b1f1498f178a09be55862 100644 (file)
@@ -31,6 +31,7 @@
 #include "pb-protocol/pb-protocol.h"
 #include "talloc/talloc.h"
 #include "waiter/waiter.h"
+#include "process/process.h"
 #include "ui/common/discover-client.h"
 #include "nc-cui.h"
 
@@ -507,6 +508,8 @@ struct cui *cui_init(void* platform_info,
        cui->platform_info = platform_info;
        cui->waitset = waitset_create(cui);
 
+       process_init(cui, cui->waitset);
+
        setlocale(LC_ALL, "");
 
        /* Loop here for scripts that just started the server. */
index 445c865d82ed54b7588bd4dc731afaeb13ba7f0b..1de532d5aa78fb15c99ca51fed6febbca7fed8d7 100644 (file)
@@ -30,6 +30,7 @@
 #include "log/log.h"
 #include "talloc/talloc.h"
 #include "waiter/waiter.h"
+#include "process/process.h"
 #include "ui/common/discover-client.h"
 
 static struct pb_opt_data *pbt_opt_data_from_item(struct pbt_item *item)
@@ -269,6 +270,8 @@ struct pbt_client *pbt_client_init(enum pbt_twin_backend backend,
 
        pbt_client->waitset = waitset_create(pbt_client);
 
+       process_init(pbt_client, pbt_client->waitset);
+
        pbt_client->sig = "pbt_client";
        pbt_client->frame.scr = pbt_scr_init(pbt_client, pbt_client->waitset,
                        backend, width, height, NULL, NULL);