public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "William Hubbs" <williamh@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/openrc:master commit in: src/librc/
Date: Fri, 22 Jun 2018 20:57:17 +0000 (UTC)	[thread overview]
Message-ID: <1529700085.72df51e17ba0e1a0f94451b4bbfb338288c4625c.williamh@OpenRC> (raw)

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)


             reply	other threads:[~2018-06-22 20:57 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-22 20:57 William Hubbs [this message]
  -- strict thread matches above, loose matches on Subject: below --
2018-08-06 22:52 [gentoo-commits] proj/openrc:master commit in: src/librc/ William Hubbs
2018-06-28 18:03 William Hubbs
2018-06-20 22:45 William Hubbs
2018-05-22 22:12 William Hubbs
2018-05-22 22:12 William Hubbs
2017-11-30 19:58 William Hubbs
2017-11-14 19:22 William Hubbs
2017-10-26 18:58 William Hubbs
2017-10-24 15:43 William Hubbs
2016-12-20  0:29 William Hubbs
2016-08-15 18:48 William Hubbs
2016-01-19  6:12 William Hubbs
2016-01-19  6:12 William Hubbs
2016-01-19  6:12 William Hubbs
2016-01-19  6:12 William Hubbs
2015-12-08 18:53 William Hubbs
2015-11-05 18:53 William Hubbs
2015-11-05 18:53 William Hubbs
2015-05-04 15:10 William Hubbs
2015-01-12 16:44 William Hubbs
2014-11-01 22:05 William Hubbs
2014-10-24 15:45 William Hubbs
2014-10-23 19:13 William Hubbs
2014-10-23 17:57 William Hubbs
2014-10-20 20:59 William Hubbs
2014-07-19 18:06 William Hubbs
2014-06-20 21:22 William Hubbs
2013-10-21 19:53 William Hubbs
2013-10-04 19:07 William Hubbs
2013-10-04 19:07 William Hubbs
2013-04-14  3:45 William Hubbs
2012-10-05  3:28 William Hubbs
2012-02-25 21:02 William Hubbs
2012-02-25 16:57 Christian Ruppert
2012-01-30 19:33 William Hubbs
2012-01-24 18:41 Christian Ruppert
2012-01-21  3:41 William Hubbs
2012-01-16 18:16 William Hubbs
2012-01-15  0:05 Christian Ruppert
2011-11-07 15:58 William Hubbs
2011-07-13 19:39 Christian Ruppert
2011-07-09 21:15 Christian Ruppert
2011-06-28 16:47 Christian Ruppert
2011-05-28 16:02 Mike Frysinger
2011-04-28  0:18 William Hubbs
2011-04-28  0:18 William Hubbs
2011-04-09 19:56 William Hubbs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1529700085.72df51e17ba0e1a0f94451b4bbfb338288c4625c.williamh@OpenRC \
    --to=williamh@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox