Skip to content

Commit 64c3b3e

Browse files
navi-desuwilliamh
authored andcommitted
misc: Replace rc_getline and getline with xgetline.
rc_getline is an implementation from when getline wasn't available on most libcs, and is planned for removal, however much of of the code relies on it's behaviour of removing new lines at the end of strings. To avoid duplicating the new line removal logic in a dozen places, we introduce a new helper function xgetline, which just wraps around getline but removes the new line at the end.
1 parent 7c31e50 commit 64c3b3e

11 files changed

Lines changed: 51 additions & 48 deletions

File tree

src/kill_all/kill_all.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static bool is_user_process(pid_t pid)
115115
temp_pid = -1;
116116
while (!feof(fp)) {
117117
buf = NULL;
118-
if (getline(&buf, &size, fp) != -1) {
118+
if (xgetline(&buf, &size, fp) != -1) {
119119
sscanf(buf, "PPid: %d", &temp_pid);
120120
free(buf);
121121
} else {

src/librc/librc-daemon.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid)
140140
if (exists("/proc/self/status")) {
141141
fp = fopen("/proc/self/status", "r");
142142
if (fp) {
143-
while (!feof(fp)) {
144-
rc_getline(&line, &len, fp);
143+
while (xgetline(&line, &len, fp) != -1) {
145144
if (strncmp(line, "envID:\t0", 8) == 0) {
146145
openvz_host = true;
147146
break;
@@ -196,8 +195,7 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid)
196195
free(buffer);
197196
if (!fp)
198197
continue;
199-
while (!feof(fp)) {
200-
rc_getline(&line, &len, fp);
198+
while (xgetline(&line, &len, fp) != -1) {
201199
if (strncmp(line, "envID:", 6) == 0) {
202200
container_pid = !(strncmp(line, "envID:\t0", 8) == 0);
203201
break;
@@ -346,7 +344,7 @@ _match_daemon(const char *path, const char *file, RC_STRINGLIST *match)
346344
if (!fp)
347345
return false;
348346

349-
while ((rc_getline(&line, &len, fp))) {
347+
while (xgetline(&line, &len, fp) != -1) {
350348
TAILQ_FOREACH(m, match, entries)
351349
if (strcmp(line, m->value) == 0) {
352350
TAILQ_REMOVE(match, m, entries);
@@ -559,7 +557,7 @@ rc_service_daemons_crashed(const char *service)
559557
if (!fp)
560558
break;
561559

562-
while ((rc_getline(&line, &len, fp))) {
560+
while (xgetline(&line, &len, fp) != -1) {
563561
p = line;
564562
if ((token = strsep(&p, "=")) == NULL || !p)
565563
continue;

src/librc/librc-depend.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ rc_deptree_load_file(const char *deptree_file)
160160
RC_DEPINFO *depinfo = NULL;
161161
RC_DEPTYPE *deptype = NULL;
162162
char *line = NULL;
163-
size_t len = 0;
164-
ssize_t size;
163+
size_t size;
165164
char *type;
166165
char *p;
167166
char *e;
@@ -171,8 +170,7 @@ rc_deptree_load_file(const char *deptree_file)
171170
return NULL;
172171

173172
deptree = make_deptree();
174-
while ((size = getline(&line, &len, fp)) != -1) {
175-
line[size - 1] = '\0';
173+
while (xgetline(&line, &size, fp) != -1) {
176174
p = line;
177175
e = strsep(&p, "_");
178176
if (!e || strcmp(e, "depinfo") != 0)
@@ -788,8 +786,7 @@ rc_deptree_update(void)
788786
RC_STRINGLIST *config, *dupes, *types, *sorted, *visited;
789787
RC_STRING *s, *s2, *s2_np, *s3, *s4;
790788
char *line = NULL;
791-
size_t len = 0;
792-
ssize_t size;
789+
size_t size;
793790
char *depend, *depends, *service, *type;
794791
size_t i, l;
795792
bool retval = true;
@@ -811,8 +808,7 @@ rc_deptree_update(void)
811808
config = rc_stringlist_new();
812809

813810
deptree = make_deptree();
814-
while ((size = getline(&line, &len, fp)) != -1) {
815-
line[size - 1] = '\0';
811+
while (xgetline(&line, &size, fp) != -1) {
816812
depends = line;
817813
service = strsep(&depends, " ");
818814
if (!service || !*service)
@@ -894,8 +890,8 @@ rc_deptree_update(void)
894890
* work for them. This doesn't stop them from being run directly. */
895891
if (sys) {
896892
char *nosys, *onosys;
893+
size_t len = strlen(sys);
897894

898-
len = strlen(sys);
899895
nosys = xmalloc(len + 2);
900896
nosys[0] = '-';
901897
for (i = 0; i < len; i++)

src/librc/librc-misc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ rc_proc_getent(const char *ent RC_UNUSED)
133133
FILE *fp;
134134
char *proc = NULL, *p, *value = NULL, *save;
135135
size_t i, len;
136-
ssize_t size;
137136

138137
if (!exists("/proc/cmdline"))
139138
return NULL;
@@ -142,11 +141,10 @@ rc_proc_getent(const char *ent RC_UNUSED)
142141
return NULL;
143142

144143
i = 0;
145-
if ((size = getline(&proc, &i, fp)) == -1) {
144+
if (xgetline(&proc, &i, fp) == -1) {
146145
free(proc);
147146
return NULL;
148147
}
149-
proc[size - 1] = '\0';
150148
save = proc;
151149

152150
len = strlen(ent);
@@ -184,7 +182,7 @@ rc_config_list(const char *file)
184182
if (!(fp = fopen(file, "r")))
185183
return list;
186184

187-
while ((rc_getline(&buffer, &len, fp))) {
185+
while (xgetline(&buffer, &len, fp) != -1) {
188186
p = buffer;
189187
/* Strip leading spaces/tabs */
190188
while ((*p == ' ') || (*p == '\t'))

src/librc/librc.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ file_regex(const char *file, const char *regex)
175175
{
176176
FILE *fp;
177177
char *line = NULL;
178-
size_t size = 0, len = 0;
178+
size_t size = 0;
179+
ssize_t len = 0;
179180
regex_t re;
180181
bool retval = true;
181182
int result;
@@ -192,7 +193,7 @@ file_regex(const char *file, const char *regex)
192193
return false;
193194
}
194195

195-
while ((len = rc_getline(&line, &size, fp))) {
196+
while ((len = xgetline(&line, &size, fp)) != -1) {
196197
char *str = line;
197198
/* some /proc files have \0 separated content so we have to
198199
loop through the 'line' */
@@ -682,19 +683,22 @@ rc_service_extra_commands(const char *service)
682683
snprintf(cmd, l, OPTSTR, svc);
683684
free(svc);
684685

685-
if ((fp = popen(cmd, "r"))) {
686-
rc_getline(&buffer, &len, fp);
686+
if (!(fp = popen(cmd, "r"))) {
687+
free(cmd);
688+
return NULL;
689+
}
690+
691+
if (xgetline(&buffer, &len, fp) != -1) {
687692
p = buffer;
688693
commands = rc_stringlist_new();
689694

690695
while ((token = strsep(&p, " ")))
691696
if (token[0] != '\0')
692697
rc_stringlist_add(commands, token);
693-
694-
pclose(fp);
695-
free(buffer);
696698
}
697699

700+
pclose(fp);
701+
free(buffer);
698702
free(cmd);
699703
return commands;
700704
}
@@ -706,7 +710,7 @@ rc_service_description(const char *service, const char *option)
706710
char *svc;
707711
char *cmd;
708712
char *desc = NULL;
709-
size_t len = 0;
713+
size_t size = 0;
710714
FILE *fp;
711715
size_t l;
712716

@@ -721,7 +725,10 @@ rc_service_description(const char *service, const char *option)
721725
snprintf(cmd, l, DESCSTR, svc, *option ? "_" : "", option);
722726
free(svc);
723727
if ((fp = popen(cmd, "r"))) {
724-
rc_getline(&desc, &len, fp);
728+
if (xgetline(&desc, &size, fp) == -1) {
729+
free(desc);
730+
desc = NULL;
731+
}
725732
pclose(fp);
726733
}
727734
free(cmd);

src/mountinfo/mountinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ find_mounts(struct args *args)
327327
list = rc_stringlist_new();
328328

329329
buffer = NULL;
330-
while (getline(&buffer, &size, fp) != -1) {
330+
while (xgetline(&buffer, &size, fp) != -1) {
331331
netdev = -1;
332332
p = buffer;
333333
from = strsep(&p, " ");

src/openrc/rc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,11 @@ static char *get_krunlevel(void)
335335
return NULL;
336336
}
337337

338-
if (getline(&buffer, &i, fp) != -1) {
339-
i = strlen(buffer);
340-
if (buffer[i - 1] == '\n')
341-
buffer[i - 1] = 0;
338+
if (xgetline(&buffer, &i, fp) == -1) {
339+
free(buffer);
340+
buffer = NULL;
342341
}
342+
343343
fclose(fp);
344344
return buffer;
345345
}

src/shared/helpers.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,16 @@ RC_UNUSED RC_PRINTF(2,3) static int xasprintf(char **strp, const char *fmt, ...)
165165
return len;
166166
}
167167

168+
RC_UNUSED static ssize_t xgetline(char **restrict lineptr, size_t *restrict n, FILE *restrict stream)
169+
{
170+
ssize_t ret = getline(lineptr, n, stream);
171+
if (ret <= 0)
172+
return ret;
173+
174+
if ((*lineptr)[ret - 1] == '\n')
175+
(*lineptr)[--ret] = '\0';
176+
177+
return ret;
178+
}
179+
168180
#endif

src/shared/misc.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,14 @@ env_config(void)
183183
free(e);
184184

185185
if ((fp = fopen(RC_KRUNLEVEL, "r"))) {
186-
if (getline(&buffer, &size, fp) != -1) {
187-
l = strlen (buffer) - 1;
188-
if (buffer[l] == '\n')
189-
buffer[l] = 0;
186+
if (xgetline(&buffer, &size, fp) != -1)
190187
setenv("RC_DEFAULTLEVEL", buffer, 1);
191-
}
188+
free(buffer);
192189
fclose(fp);
193-
} else
190+
} else {
194191
setenv("RC_DEFAULTLEVEL", RC_LEVEL_DEFAULT, 1);
192+
}
195193

196-
free(buffer);
197194
if (sys)
198195
setenv("RC_SYS", sys, 1);
199196

src/shared/selinux.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ static int read_context_file(const char *filename, char **context)
261261
char *p;
262262
char *p2;
263263
size_t len = 0;
264-
ssize_t read;
265264

266265
xasprintf(&filepath, "%s/%s", selinux_contexts_path(), filename);
267266

@@ -272,7 +271,7 @@ static int read_context_file(const char *filename, char **context)
272271
return -1;
273272
}
274273

275-
while ((read = getline(&line, &len, fp)) != -1) {
274+
while (xgetline(&line, &len, fp) != -1) {
276275
/* cut off spaces before the string */
277276
p = line;
278277
while (isspace(*p) && *p != '\0')

0 commit comments

Comments
 (0)