From patchwork Tue May 17 15:00:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jo-Philipp Wich X-Patchwork-Id: 623146 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r8LFP6jkqz9ssP for ; Wed, 18 May 2016 01:03:53 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b2gVR-0002zN-Q0; Tue, 17 May 2016 15:01:57 +0000 Received: from mxout01.bytecamp.net ([212.204.60.217]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1b2gVQ-0002uZ-N5 for lede-dev@lists.infradead.org; Tue, 17 May 2016 15:01:57 +0000 Received: by mxout01.bytecamp.net (Postfix, from userid 1001) id 4492F30FE18; Tue, 17 May 2016 17:01:08 +0200 (CEST) Received: from mail.bytecamp.net (mailstore.bytecamp.net [212.204.60.20]) by mxout01.bytecamp.net (Postfix) with ESMTP id B4D3C30FE11 for ; Tue, 17 May 2016 17:01:07 +0200 (CEST) Received: (qmail 36615 invoked by uid 89); 17 May 2016 17:01:07 +0200 Received: from unknown (HELO localhost.localdomain) (jo%wwsnet.net@213.61.250.148) by mail.bytecamp.net with AES128-SHA encrypted SMTP; 17 May 2016 17:01:07 +0200 From: Jo-Philipp Wich To: lede-dev@lists.infradead.org Date: Tue, 17 May 2016 17:00:43 +0200 Message-Id: <0144ce1f32e586dc52bb5cd403d90dd0c1d03fc0.1463496164.git.jo@mein.io> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160517_080156_957627_5AF1E830 X-CRM114-Status: GOOD ( 12.10 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [212.204.60.217 listed in list.dnswl.org] -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Subject: [LEDE-DEV] [PATCH 1/5] utils: add patch_fd() and patch_stdio() helpers X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jo-Philipp Wich , john@phrozen.org MIME-Version: 1.0 Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Introduce two new helper functions to deal with stdio redirecation in a uniform, reliable manner: The patch_fd() function will attempt to redirect the given fd number to the specified file, using the supplied flags for the open() syscall. When the device is NULL, "/dev/null" is asumed, when the device is a relative path, openat() is used to open it relative to the "/dev" directory. When the device cannot be openend, a fallback to "/dev/null" is attempted. The patch_stdio() function is essentially a wrapper around patch_fd(), providing an easy interface to redirect stdin, stdout and stderr to the same given device. Both function return 0 on success and -1 on error. The errno variable will be set accordingly. Signed-off-by: Jo-Philipp Wich --- utils/utils.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils/utils.h | 3 +++ 2 files changed, 57 insertions(+) diff --git a/utils/utils.c b/utils/utils.c index a67c004..ebf5447 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -20,6 +20,10 @@ #include #include #include +#include +#include + +#include "../log.h" void __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp) @@ -152,3 +156,53 @@ char* get_cmdline_val(const char* name, char* out, int len) return NULL; } + +int patch_fd(const char *device, int fd, int flags) +{ + int dfd, nfd; + + if (device == NULL) + device = "/dev/null"; + + if (*device != '/') { + dfd = open("/dev", O_RDONLY); + + if (dfd < 0) + return -1; + + nfd = openat(dfd, device, flags); + + close(dfd); + } else { + nfd = open(device, flags); + } + + if (nfd < 0 && strcmp(device, "/dev/null")) + nfd = open("/dev/null", flags); + + if (nfd < 0) + return -1; + + fd = dup2(nfd, fd); + + if (nfd > STDERR_FILENO) + close(nfd); + + return (fd < 0) ? -1 : 0; +} + +int patch_stdio(const char *device) +{ + int fd, rv = 0; + const char *fdname[3] = { "stdin", "stdout", "stderr" }; + + for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) { + if (patch_fd(device, fd, fd ? O_WRONLY : O_RDONLY)) { + ERROR("Failed to redirect %s to %s: %d (%s)\n", + fdname[fd], device, errno, strerror(errno)); + rv = -1; + } + } + + return rv; +} diff --git a/utils/utils.h b/utils/utils.h index 8b384fc..908c314 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -53,4 +53,7 @@ bool blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2); void blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src); char* get_cmdline_val(const char* name, char* out, int len); +int patch_fd(const char *device, int fd, int flags); +int patch_stdio(const char *device); + #endif