From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id F1FAE138331 for ; Fri, 16 Feb 2018 20:07:46 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 3183AE0AED; Fri, 16 Feb 2018 20:07:46 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 00EB1E0AED for ; Fri, 16 Feb 2018 20:07:45 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id C26E8335C2E for ; Fri, 16 Feb 2018 20:07:44 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 82381215 for ; Fri, 16 Feb 2018 20:07:43 +0000 (UTC) From: "William Hubbs" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "William Hubbs" Message-ID: <1518730000.488d8989c518d9256f183899aac02024c679b93e.williamh@OpenRC> Subject: [gentoo-commits] proj/openrc:master commit in: src/rc/ X-VCS-Repository: proj/openrc X-VCS-Files: src/rc/openrc-run.c X-VCS-Directories: src/rc/ X-VCS-Committer: williamh X-VCS-Committer-Name: William Hubbs X-VCS-Revision: 488d8989c518d9256f183899aac02024c679b93e X-VCS-Branch: master Date: Fri, 16 Feb 2018 20:07:43 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 35438482-8b68-44b6-ac0b-e65f6be71b02 X-Archives-Hash: 88a605db8012764d87738fc909bcf72d commit: 488d8989c518d9256f183899aac02024c679b93e Author: William Hubbs gmail com> AuthorDate: Thu Feb 15 21:10:24 2018 +0000 Commit: William Hubbs gentoo org> CommitDate: Thu Feb 15 21:26:40 2018 +0000 URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=488d8989 openrc-run: clean up string handling - remove references to PATH_MAX - use xasprintf to create strings src/rc/openrc-run.c | 61 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/src/rc/openrc-run.c b/src/rc/openrc-run.c index bb79f42f..7e8d2e50 100644 --- a/src/rc/openrc-run.c +++ b/src/rc/openrc-run.c @@ -109,7 +109,7 @@ static void handle_signal(int sig) { int serrno = errno; - char signame[10] = { '\0' }; + char *signame = NULL; struct winsize ws; switch (sig) { @@ -134,20 +134,22 @@ handle_signal(int sig) break; case SIGINT: - if (!signame[0]) - snprintf(signame, sizeof(signame), "SIGINT"); + if (!signame) + xasprintf(&signame, "SIGINT"); /* FALLTHROUGH */ case SIGTERM: - if (!signame[0]) - snprintf(signame, sizeof(signame), "SIGTERM"); + if (!signame) + xasprintf(&signame, "SIGTERM"); /* FALLTHROUGH */ case SIGQUIT: - if (!signame[0]) - snprintf(signame, sizeof(signame), "SIGQUIT"); + if (!signame) + xasprintf(&signame, "SIGQUIT"); /* Send the signal to our children too */ if (service_pid > 0) kill(service_pid, sig); - eerrorx("%s: caught %s, aborting", applet, signame); + eerror("%s: caught %s, aborting", applet, signame); + free(signame); + exit(EXIT_FAILURE); /* NOTREACHED */ default: @@ -161,11 +163,12 @@ handle_signal(int sig) static void unhotplug() { - char file[PATH_MAX]; + char *file = NULL; - snprintf(file, sizeof(file), RC_SVCDIR "/hotplugged/%s", applet); + xasprintf(&file, RC_SVCDIR "/hotplugged/%s", applet); if (exists(file) && unlink(file) != 0) eerror("%s: unlink `%s': %s", applet, file, strerror(errno)); + free(file); } static void @@ -485,7 +488,7 @@ svc_exec(const char *arg1, const char *arg2) static bool svc_wait(const char *svc) { - char file[PATH_MAX]; + char *file = NULL; int fd; bool forever = false; RC_STRINGLIST *keywords; @@ -498,8 +501,7 @@ svc_wait(const char *svc) forever = true; rc_stringlist_free(keywords); - snprintf(file, sizeof(file), RC_SVCDIR "/exclusive/%s", - basename_c(svc)); + xasprintf(&file, RC_SVCDIR "/exclusive/%s", basename_c(svc)); interval.tv_sec = 0; interval.tv_nsec = WAIT_INTERVAL; @@ -512,23 +514,29 @@ svc_wait(const char *svc) if (fd != -1) { if (flock(fd, LOCK_SH | LOCK_NB) == 0) { close(fd); + free(file); return true; } close(fd); } - if (errno == ENOENT) + if (errno == ENOENT) { + free(file); return true; - if (errno != EWOULDBLOCK) - eerrorx("%s: open `%s': %s", applet, file, + } + if (errno != EWOULDBLOCK) { + eerror("%s: open `%s': %s", applet, file, strerror(errno)); + free(file); + exit(EXIT_FAILURE); + } if (nanosleep(&interval, NULL) == -1) { if (errno != EINTR) - return false; + goto finish; } if (!forever) { timespecsub(&timeout, &interval, &timeout); if (timeout.tv_sec <= 0) - return false; + goto finish; timespecsub(&warn, &interval, &warn); if (warn.tv_sec <= 0) { ewarn("%s: waiting for %s (%d seconds)", @@ -538,6 +546,8 @@ svc_wait(const char *svc) } } } +finish: + free(file); return false; } @@ -1105,9 +1115,10 @@ int main(int argc, char **argv) bool runscript = false; int retval, opt, depoptions = RC_DEP_TRACE; RC_STRING *svc; - char path[PATH_MAX], lnk[PATH_MAX]; + char *path = NULL; + char *lnk = NULL; char *dir, *save = NULL, *saveLnk = NULL; - char pidstr[10]; + char *pidstr = NULL; size_t l = 0, ll; const char *file; struct stat stbuf; @@ -1134,10 +1145,12 @@ int main(int argc, char **argv) * This works fine, provided that we ONLY allow multiplexed services * to exist in the same directory as the master link. * Also, the master link as to be a real file in the init dir. */ - if (!realpath(argv[1], path)) { + path = realpath(argv[1], NULL); + if (!path) { fprintf(stderr, "realpath: %s\n", strerror(errno)); exit(EXIT_FAILURE); } + lnk = xmalloc(4096); memset(lnk, 0, sizeof(lnk)); if (readlink(argv[1], lnk, sizeof(lnk)-1)) { dir = dirname(path); @@ -1153,8 +1166,7 @@ int main(int argc, char **argv) } else file = basename_c(argv[1]); ll = strlen(dir) + strlen(file) + 2; - service = xmalloc(ll); - snprintf(service, ll, "%s/%s", dir, file); + xasprintf(&service, "%s/%s", dir, file); if (stat(service, &stbuf) != 0) { free(service); service = xstrdup(lnk); @@ -1162,6 +1174,7 @@ int main(int argc, char **argv) free(save); free(saveLnk); } + free(lnk); if (!service) service = xstrdup(path); applet = basename_c(service); @@ -1185,7 +1198,7 @@ int main(int argc, char **argv) /* Set an env var so that we always know our pid regardless of any subshells the init script may create so that our mark_service_* functions can always instruct us of this change */ - snprintf(pidstr, sizeof(pidstr), "%d", (int) getpid()); + xasprintf(&pidstr, "%d", (int) getpid()); setenv("RC_OPENRC_PID", pidstr, 1); /* * RC_RUNSCRIPT_PID is deprecated, but we will keep it for a while