From patchwork Mon Apr 13 01:11:48 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 25881 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id D37A4DE26A for ; Mon, 13 Apr 2009 11:17:35 +1000 (EST) X-Original-To: cbe-oss-dev@ozlabs.org Delivered-To: cbe-oss-dev@ozlabs.org Received: from hera.kernel.org (hera.kernel.org [140.211.167.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 3BF65DE1AA; Mon, 13 Apr 2009 11:15:52 +1000 (EST) Received: from hera.kernel.org (IDENT:U2FsdGVkX18ngWl9i/kxJU4LRGg1lzAHRHxM5HMhlqg@localhost [127.0.0.1]) by hera.kernel.org (8.14.2/8.14.2) with ESMTP id n3D1FhSv006779 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 13 Apr 2009 01:15:43 GMT Received: (from geoff@localhost) by hera.kernel.org (8.14.2/8.13.1/Submit) id n3D1Fh7i006778; Mon, 13 Apr 2009 01:15:43 GMT Message-Id: <20090413011137.431303139@am.sony.com> References: <20090413011136.475152916@am.sony.com> In-Reply-To: <20090413011136.475152916@am.sony.com> User-Agent: quilt/0.46-1 Date: Sun, 12 Apr 2009 18:11:48 -0700 From: Geoff Levand To: Jeremy Kerr Content-Disposition: inline; filename=lib-system-helpers.diff X-Virus-Scanned: ClamAV 0.93.3/9226/Sun Apr 12 20:02:45 2009 on hera.kernel.org X-Virus-Status: Clean X-Spam-Status: No, score=-1.4 required=5.0 tests=AWL,BAYES_00, UNPARSEABLE_RELAY,URIBL_SBL autolearn=no version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on hera.kernel.org X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.0 (hera.kernel.org [127.0.0.1]); Mon, 13 Apr 2009 01:15:45 +0000 (UTC) Cc: cbe-oss-dev@ozlabs.org Subject: [Cbe-oss-dev] [patch 12/24] petitboot: Move common system routines to lib X-BeenThere: cbe-oss-dev@ozlabs.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: Discussion about Open Source Software for the Cell Broadband Engine List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Errors-To: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Move some of the common system operations to lib system routines. Creates these common routines: pb_mkdir_recursive() pb_rmdir_recursive() pb_run_cmd() Signed-off-by: Geoff Levand --- configure.ac | 4 - lib/system/system.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/system/system.h | 20 ++++++ rules.mk | 5 - 4 files changed, 182 insertions(+), 4 deletions(-) --- a/configure.ac +++ b/configure.ac @@ -50,7 +50,7 @@ AS_IF([test "x$with_twin" != xno], fi], [${twin_LIBS}])]) -mkdir -p discover lib/list lib/log lib/pb-protocol lib/talloc lib/waiter \ - test ui/common ui/ncurses ui/test ui/twin +mkdir -p discover lib/list lib/log lib/pb-protocol lib/system lib/talloc \ + lib/waiter test ui/common ui/ncurses ui/test ui/twin AC_OUTPUT --- /dev/null +++ b/lib/system/system.c @@ -0,0 +1,157 @@ + +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "log/log.h" +#include +#include "system.h" + +const struct pb_system_apps pb_system_apps = { + .cp = "/bin/cp", + .kexec = "/sbin/kexec", + .mount = "/bin/mount", + .sftp = "/usr/bin/sftp", + .tftp = "/usr/bin/tftp", + .umount = "/bin/umount", + .wget = "/usr/bin/wget", +}; + +int pb_mkdir_recursive(const char *dir) +{ + struct stat statbuf; + char *str, *sep; + int mode = 0755; + + if (!*dir) + return 0; + + if (!stat(dir, &statbuf)) { + if (!S_ISDIR(statbuf.st_mode)) { + pb_log("%s: %s exists, but isn't a directory\n", + __func__, dir); + return -1; + } + return 0; + } + + str = talloc_strdup(NULL, dir); + sep = strchr(*str == '/' ? str + 1 : str, '/'); + + while (1) { + + /* terminate the path at sep */ + if (sep) + *sep = '\0'; + + if (mkdir(str, mode) && errno != EEXIST) { + pb_log("mkdir(%s): %s\n", str, strerror(errno)); + return -1; + } + + if (!sep) + break; + + /* reset dir to the full path */ + strcpy(str, dir); + sep = strchr(sep + 1, '/'); + } + + talloc_free(str); + + return 0; +} + +int pb_rmdir_recursive(const char *base, const char *dir) +{ + char *cur, *pos; + + /* sanity check: make sure that dir is within base */ + if (strncmp(base, dir, strlen(base))) + return -1; + + cur = talloc_strdup(NULL, dir); + + while (strcmp(base, dir)) { + + rmdir(dir); + + /* null-terminate at the last slash */ + pos = strrchr(dir, '/'); + if (!pos) + break; + + *pos = '\0'; + } + + talloc_free(cur); + + return 0; +} + +/** + * pb_run_cmd - Run the supplied command. + * @cmd_argv: An argument list array for execv. + */ + +int pb_run_cmd(const char *const *cmd_argv) +{ + int status; + pid_t pid; +#if defined(DEBUG) + enum {do_debug = 1}; +#else + enum {do_debug = 0}; +#endif + + if (do_debug) { + const char *const *p = cmd_argv; + + pb_log("%s: ", __func__); + while (*p) { + pb_log("%s ", *p); + p++; + } + pb_log("\n"); + } else + pb_log("%s: %s\n", __func__, cmd_argv[0]); + + pid = fork(); + if (pid == -1) { + pb_log("%s: fork failed: %s\n", __func__, strerror(errno)); + return -1; + } + + if (pid == 0) { + execvp(cmd_argv[0], (char *const *)cmd_argv); + pb_log("%s: exec failed: %s\n", __func__, strerror(errno)); + exit(EXIT_FAILURE); + } + + if (waitpid(pid, &status, 0) == -1) { + pb_log("%s: waitpid failed: %s\n", __func__, + strerror(errno)); + return -1; + } + + if (do_debug && WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) + pb_log("%s: signaled\n", __func__); + + if (!WIFEXITED(status)) { + pb_log("%s: %s failed\n", __func__, cmd_argv[0]); + return -1; + } + + if (WEXITSTATUS(status)) + pb_log("%s: WEXITSTATUS %d\n", __func__, WEXITSTATUS(status)); + + return WEXITSTATUS(status); +} --- /dev/null +++ b/lib/system/system.h @@ -0,0 +1,20 @@ +#if !defined(_PB_LIB_SYSTEM_H) +#define _PB_LIB_SYSTEM_H + +struct pb_system_apps { + const char *cp; + const char *kexec; + const char *mount; + const char *sftp; + const char *tftp; + const char *umount; + const char *wget; +}; + +extern const struct pb_system_apps pb_system_apps; + +int pb_run_cmd(const char *const *cmd_argv); +int pb_mkdir_recursive(const char *dir); +int pb_rmdir_recursive(const char *base, const char *dir); + +#endif --- a/rules.mk +++ b/rules.mk @@ -33,6 +33,7 @@ rules = utils/99-petitboot.rules list_objs = lib/list/list.o log_objs = lib/log/log.o protocol_objs = lib/pb-protocol/pb-protocol.o +system_objs = lib/system/system.o talloc_objs = lib/talloc/talloc.o waiter_objs = lib/waiter/waiter.o @@ -51,8 +52,8 @@ twin_objs = ui/twin/pb-twin.o makefiles = Makefile $(top_srcdir)/rules.mk # object collections -lib_objs = $(list_objs) $(log_objs) $(protocol_objs) $(talloc_objs) \ - $(waiter_objs) +lib_objs = $(list_objs) $(log_objs) $(protocol_objs) $(system_objs) \ + $(talloc_objs) $(waiter_objs) daemon_objs = $(lib_objs) $(parser_objs) $(discover_objs)