From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1031747-garchives=archives.gentoo.org@lists.gentoo.org>
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 0E1E6138334
	for <garchives@archives.gentoo.org>; Fri, 22 Jun 2018 20:57:22 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id EF51DE085B;
	Fri, 22 Jun 2018 20:57:20 +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 AB61EE085B
	for <gentoo-commits@lists.gentoo.org>; Fri, 22 Jun 2018 20:57:20 +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 A577B335C92
	for <gentoo-commits@lists.gentoo.org>; Fri, 22 Jun 2018 20:57:18 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 10FD62D6
	for <gentoo-commits@lists.gentoo.org>; Fri, 22 Jun 2018 20:57:17 +0000 (UTC)
From: "William Hubbs" <williamh@gentoo.org>
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" <williamh@gentoo.org>
Message-ID: <1529700085.72df51e17ba0e1a0f94451b4bbfb338288c4625c.williamh@OpenRC>
Subject: [gentoo-commits] proj/openrc:master commit in: src/librc/
X-VCS-Repository: proj/openrc
X-VCS-Files: src/librc/librc-daemon.c
X-VCS-Directories: src/librc/
X-VCS-Committer: williamh
X-VCS-Committer-Name: William Hubbs
X-VCS-Revision: 72df51e17ba0e1a0f94451b4bbfb338288c4625c
X-VCS-Branch: master
Date: Fri, 22 Jun 2018 20:57:17 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Archives-Salt: e6bc1604-79ca-4cc6-9e40-74fe13d08b49
X-Archives-Hash: 7fb9eeb088e19a5917d739a965b794ea

commit:     72df51e17ba0e1a0f94451b4bbfb338288c4625c
Author:     William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Fri Jun 22 20:41:25 2018 +0000
Commit:     William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Fri Jun 22 20:41:25 2018 +0000
URL:        https://gitweb.gentoo.org/proj/openrc.git/commit/?id=72df51e1

librc-daemon: convert most snprintf calls to xasprintf

 src/librc/librc-daemon.c | 71 ++++++++++++++++++++++++------------------------
 1 file changed, 36 insertions(+), 35 deletions(-)

diff --git a/src/librc/librc-daemon.c b/src/librc/librc-daemon.c
index a40150a6..173fcb83 100644
--- a/src/librc/librc-daemon.c
+++ b/src/librc/librc-daemon.c
@@ -23,13 +23,13 @@
 static bool
 pid_is_exec(pid_t pid, const char *exec)
 {
-	char buffer[32];
+	char *buffer = NULL;
 	FILE *fp;
 	int c;
 	bool retval = false;
 
 	exec = basename_c(exec);
-	snprintf(buffer, sizeof(buffer), "/proc/%d/stat", pid);
+	xasprintf(&buffer, "/proc/%d/stat", pid);
 	if ((fp = fopen(buffer, "r"))) {
 		while ((c = getc(fp)) != EOF && c != '(')
 			;
@@ -41,23 +41,27 @@ pid_is_exec(pid_t pid, const char *exec)
 		}
 		fclose(fp);
 	}
+	free(buffer);
 	return retval;
 }
 
 static bool
 pid_is_argv(pid_t pid, const char *const *argv)
 {
-	char cmdline[32];
+	char *cmdline = NULL;
 	int fd;
 	char buffer[PATH_MAX];
 	char *p;
 	ssize_t bytes;
 
-	snprintf(cmdline, sizeof(cmdline), "/proc/%u/cmdline", pid);
-	if ((fd = open(cmdline, O_RDONLY)) < 0)
+	xasprintf(&cmdline, "/proc/%u/cmdline", pid);
+	if ((fd = open(cmdline, O_RDONLY)) < 0) {
+		free(cmdline);
 		return false;
+	}
 	bytes = read(fd, buffer, sizeof(buffer));
 	close(fd);
+	free(cmdline);
 	if (bytes == -1)
 		return false;
 
@@ -88,7 +92,7 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid)
 	char proc_ns[30];
 	size_t len = 0;
 	pid_t p;
-	char buffer[PATH_MAX];
+	char *buffer = NULL;
 	struct stat sb;
 	pid_t openrc_pid = 0;
 	char *pp;
@@ -149,18 +153,22 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid)
 			continue;
 		if (pid != 0 && pid != p)
 			continue;
-		snprintf(buffer, sizeof(buffer), "/proc/%d/ns/pid", p);
+		xasprintf(&buffer, "/proc/%d/ns/pid", p);
 		if (exists(buffer)) {
 			rc = readlink(buffer, proc_ns, sizeof(proc_ns));
 			if (rc <= 0)
 				proc_ns[0] = '\0';
 		}
+		free(buffer);
 		if (strlen(my_ns) && strlen (proc_ns) && strcmp(my_ns, proc_ns))
 			continue;
 		if (uid) {
-			snprintf(buffer, sizeof(buffer), "/proc/%d", p);
-			if (stat(buffer, &sb) != 0 || sb.st_uid != uid)
+			xasprintf(&buffer, "/proc/%d", p);
+			if (stat(buffer, &sb) != 0 || sb.st_uid != uid) {
+				free(buffer);
 				continue;
+			}
+			free(buffer);
 		}
 		if (exec && !pid_is_exec(p, exec))
 			continue;
@@ -169,9 +177,10 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid)
 			continue;
 		/* If this is an OpenVZ host, filter out container processes */
 		if (openvz_host) {
-			snprintf(buffer, sizeof(buffer), "/proc/%d/status", p);
+			xasprintf(&buffer, "/proc/%d/status", p);
 			if (exists(buffer)) {
 				fp = fopen(buffer, "r");
+				free(buffer);
 				if (! fp)
 					continue;
 				while (! feof(fp)) {
@@ -315,12 +324,13 @@ _match_daemon(const char *path, const char *file, RC_STRINGLIST *match)
 {
 	char *line = NULL;
 	size_t len = 0;
-	char ffile[PATH_MAX];
+	char *ffile = NULL;
 	FILE *fp;
 	RC_STRING *m;
 
-	snprintf(ffile, sizeof(ffile), "%s/%s", path, file);
+	xasprintf(&ffile, "%s/%s", path, file);
 	fp = fopen(ffile, "r");
+	free(ffile);
 
 	if (!fp)
 		return false;
@@ -346,29 +356,22 @@ _match_list(const char *exec, const char *const *argv, const char *pidfile)
 {
 	RC_STRINGLIST *match = rc_stringlist_new();
 	int i = 0;
-	size_t l;
 	char *m;
 
 	if (exec) {
-		l = strlen(exec) + 6;
-		m = xmalloc(sizeof(char) * l);
-		snprintf(m, l, "exec=%s", exec);
+		xasprintf(&m, "exec=%s", exec);
 		rc_stringlist_add(match, m);
 		free(m);
 	}
 
 	while (argv && argv[i]) {
-		l = strlen(*argv) + strlen("argv_=") + 16;
-		m = xmalloc(sizeof(char) * l);
-		snprintf(m, l, "argv_0=%s", argv[i++]);
+		xasprintf(&m, "argv_0=%s", argv[i++]);
 		rc_stringlist_add(match, m);
 		free(m);
 	}
 
 	if (pidfile) {
-		l = strlen(pidfile) + 9;
-		m = xmalloc(sizeof(char) * l);
-		snprintf(m, l, "pidfile=%s", pidfile);
+		xasprintf(&m, "pidfile=%s", pidfile);
 		rc_stringlist_add(match, m);
 		free(m);
 	}
@@ -381,8 +384,8 @@ rc_service_daemon_set(const char *service, const char *exec,
     const char *const *argv,
     const char *pidfile, bool started)
 {
-	char dirpath[PATH_MAX];
-	char file[PATH_MAX];
+	char *dirpath = NULL;
+	char *file = NULL;
 	int nfiles = 0;
 	char oldfile[PATH_MAX] = { '\0' };
 	bool retval = false;
@@ -397,8 +400,7 @@ rc_service_daemon_set(const char *service, const char *exec,
 		return false;
 	}
 
-	snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s",
-	    basename_c(service));
+	xasprintf(&dirpath, RC_SVCDIR "/daemons/%s", basename_c(service));
 
 	/* Regardless, erase any existing daemon info */
 	if ((dp = opendir(dirpath))) {
@@ -407,8 +409,7 @@ rc_service_daemon_set(const char *service, const char *exec,
 			if (d->d_name[0] == '.')
 				continue;
 
-			snprintf(file, sizeof(file), "%s/%s",
-			    dirpath, d->d_name);
+			xasprintf(&file, "%s/%s", dirpath, d->d_name);
 			nfiles++;
 
 			if (!*oldfile) {
@@ -429,8 +430,7 @@ rc_service_daemon_set(const char *service, const char *exec,
 	/* Now store our daemon info */
 	if (started) {
 		if (mkdir(dirpath, 0755) == 0 || errno == EEXIST) {
-			snprintf(file, sizeof(file), "%s/%03d",
-			    dirpath, nfiles + 1);
+			xasprintf(&file, "%s/%03d", dirpath, nfiles + 1);
 			if ((fp = fopen(file, "w"))) {
 				fprintf(fp, "exec=");
 				if (exec)
@@ -458,8 +458,8 @@ bool
 rc_service_started_daemon(const char *service,
     const char *exec, const char *const *argv, int indx)
 {
-	char dirpath[PATH_MAX];
-	char file[16];
+	char *dirpath = NULL;
+	char *file = NULL;
 	RC_STRINGLIST *match;
 	bool retval = false;
 	DIR *dp;
@@ -468,13 +468,13 @@ rc_service_started_daemon(const char *service,
 	if (!service || !exec)
 		return false;
 
-	snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s",
-	    basename_c(service));
+	xasprintf(&dirpath, RC_SVCDIR "/daemons/%s", basename_c(service));
 	match = _match_list(exec, argv, NULL);
 
 	if (indx > 0) {
-		snprintf(file, sizeof(file), "%03d", indx);
+		xasprintf(&file, "%03d", indx);
 		retval = _match_daemon(dirpath, file, match);
+		free(file);
 	} else {
 		if ((dp = opendir(dirpath))) {
 			while ((d = readdir(dp))) {
@@ -489,6 +489,7 @@ rc_service_started_daemon(const char *service,
 	}
 
 	rc_stringlist_free(match);
+	free(dirpath);
 	return retval;
 }
 librc_hidden_def(rc_service_started_daemon)