From f181d0c88609b486eb2d1914feb07b2c0c5c0d2b Mon Sep 17 00:00:00 2001 From: William Hubbs Date: Mon, 24 Dec 2018 13:57:54 -0600 Subject: [PATCH] Convert internal rc_getline() calls to getline() calls getline has been in posix since POSIX.1-2008, so it should be safe for us to use it instead of using our wrapper function. This fixes #278. --- src/librc/librc-daemon.c | 10 ++++++---- src/librc/librc-depend.c | 4 ++-- src/librc/librc-misc.c | 4 ++-- src/librc/librc.c | 14 +++++++++++--- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/librc/librc-daemon.c b/src/librc/librc-daemon.c index 2b0d9712d..be656b9e7 100644 --- a/src/librc/librc-daemon.c +++ b/src/librc/librc-daemon.c @@ -134,7 +134,8 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) fp = fopen("/proc/self/status", "r"); if (fp) { while (! feof(fp)) { - rc_getline(&line, &len, fp); + if (getline(&line, &len, fp) <= 0) + break; if (strncmp(line, "envID:\t0", 8) == 0) { openvz_host = true; break; @@ -190,7 +191,8 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid) if (! fp) continue; while (! feof(fp)) { - rc_getline(&line, &len, fp); + if (getline(&line, &len, fp) <= 0) + break; if (strncmp(line, "envID:", 6) == 0) { container_pid = ! (strncmp(line, "envID:\t0", 8) == 0); break; @@ -341,7 +343,7 @@ _match_daemon(const char *path, const char *file, RC_STRINGLIST *match) if (!fp) return false; - while ((rc_getline(&line, &len, fp))) { + while ((getline(&line, &len, fp) > 0)) { TAILQ_FOREACH(m, match, entries) if (strcmp(line, m->value) == 0) { TAILQ_REMOVE(match, m, entries); @@ -546,7 +548,7 @@ rc_service_daemons_crashed(const char *service) if (!fp) break; - while ((rc_getline(&line, &len, fp))) { + while ((getline(&line, &len, fp) > 0)) { p = line; if ((token = strsep(&p, "=")) == NULL || !p) continue; diff --git a/src/librc/librc-depend.c b/src/librc/librc-depend.c index 37f0b60d6..19e57883d 100644 --- a/src/librc/librc-depend.c +++ b/src/librc/librc-depend.c @@ -127,7 +127,7 @@ rc_deptree_load_file(const char *deptree_file) deptree = xmalloc(sizeof(*deptree)); TAILQ_INIT(deptree); - while ((rc_getline(&line, &len, fp))) + while ((getline(&line, &len, fp) > 0)) { p = line; e = strsep(&p, "_"); @@ -779,7 +779,7 @@ rc_deptree_update(void) deptree = xmalloc(sizeof(*deptree)); TAILQ_INIT(deptree); config = rc_stringlist_new(); - while ((rc_getline(&line, &len, fp))) + while ((getline(&line, &len, fp) > 0)) { depends = line; service = strsep(&depends, " "); diff --git a/src/librc/librc-misc.c b/src/librc/librc-misc.c index 4ff7f4397..76827bfaf 100644 --- a/src/librc/librc-misc.c +++ b/src/librc/librc-misc.c @@ -134,7 +134,7 @@ rc_proc_getent(const char *ent _unused) proc = NULL; i = 0; - if (rc_getline(&proc, &i, fp) == -1 || proc == NULL) + if (getline(&proc, &i, fp) == -1 || proc == NULL) return NULL; if (proc != NULL) { @@ -178,7 +178,7 @@ rc_config_list(const char *file) if (!(fp = fopen(file, "r"))) return list; - while ((rc_getline(&buffer, &len, fp))) { + while ((getline(&buffer, &len, fp) > 0)) { p = buffer; /* Strip leading spaces/tabs */ while ((*p == ' ') || (*p == '\t')) diff --git a/src/librc/librc.c b/src/librc/librc.c index c38695cc6..366eb92e0 100644 --- a/src/librc/librc.c +++ b/src/librc/librc.c @@ -175,7 +175,7 @@ file_regex(const char *file, const char *regex) return false; } - while ((rc_getline(&line, &len, fp))) { + while ((getline(&line, &len, fp) > 0)) { char *str = line; /* some /proc files have \0 separated content so we have to loop through the 'line' */ @@ -650,7 +650,11 @@ rc_service_extra_commands(const char *service) free(svc); if ((fp = popen(cmd, "r"))) { - rc_getline(&buffer, &len, fp); + if (getline(&buffer, &len, fp) < 0) { + pclose(fp); + free(cmd); + return NULL; + } p = buffer; commands = rc_stringlist_new(); @@ -689,7 +693,11 @@ rc_service_description(const char *service, const char *option) snprintf(cmd, l, DESCSTR, svc, *option ? "_" : "", option); free(svc); if ((fp = popen(cmd, "r"))) { - rc_getline(&desc, &len, fp); + if (getline(&desc, &len, fp) < 0) { + pclose(fp); + free(cmd); + return desc; + } pclose(fp); } free(cmd);