Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xgetline helper and drop rc_getline #747

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions FEATURE-REMOVAL-SCHEDULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,3 @@ Who:
If you have a c program that links to librc and uses functions from
there, this section will list API functions which are deprecated and
will be removed along with the reason they are being removed.

## rc_getline()

When: 1.0

Why: The getline() function was standardized in POSIX.1-2008, so it
should be available on POSIX systems.

Who:

2 changes: 1 addition & 1 deletion src/kill_all/kill_all.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static bool is_user_process(pid_t pid)
temp_pid = -1;
while (!feof(fp)) {
buf = NULL;
if (getline(&buf, &size, fp) != -1) {
if (xgetline(&buf, &size, fp) != -1) {
sscanf(buf, "PPid: %d", &temp_pid);
free(buf);
} else {
Expand Down
10 changes: 4 additions & 6 deletions src/librc/librc-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid)
if (exists("/proc/self/status")) {
fp = fopen("/proc/self/status", "r");
if (fp) {
while (!feof(fp)) {
rc_getline(&line, &len, fp);
while (xgetline(&line, &len, fp) != -1) {
if (strncmp(line, "envID:\t0", 8) == 0) {
openvz_host = true;
break;
Expand Down Expand Up @@ -196,8 +195,7 @@ rc_find_pids(const char *exec, const char *const *argv, uid_t uid, pid_t pid)
free(buffer);
if (!fp)
continue;
while (!feof(fp)) {
rc_getline(&line, &len, fp);
while (xgetline(&line, &len, fp) != -1) {
if (strncmp(line, "envID:", 6) == 0) {
container_pid = !(strncmp(line, "envID:\t0", 8) == 0);
break;
Expand Down Expand Up @@ -346,7 +344,7 @@ _match_daemon(const char *path, const char *file, RC_STRINGLIST *match)
if (!fp)
return false;

while ((rc_getline(&line, &len, fp))) {
while (xgetline(&line, &len, fp) != -1) {
TAILQ_FOREACH(m, match, entries)
if (strcmp(line, m->value) == 0) {
TAILQ_REMOVE(match, m, entries);
Expand Down Expand Up @@ -559,7 +557,7 @@ rc_service_daemons_crashed(const char *service)
if (!fp)
break;

while ((rc_getline(&line, &len, fp))) {
while (xgetline(&line, &len, fp) != -1) {
p = line;
if ((token = strsep(&p, "=")) == NULL || !p)
continue;
Expand Down
14 changes: 5 additions & 9 deletions src/librc/librc-depend.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ rc_deptree_load_file(const char *deptree_file)
RC_DEPINFO *depinfo = NULL;
RC_DEPTYPE *deptype = NULL;
char *line = NULL;
size_t len = 0;
ssize_t size;
size_t size;
char *type;
char *p;
char *e;
Expand All @@ -171,8 +170,7 @@ rc_deptree_load_file(const char *deptree_file)
return NULL;

deptree = make_deptree();
while ((size = getline(&line, &len, fp)) != -1) {
line[size - 1] = '\0';
while (xgetline(&line, &size, fp) != -1) {
p = line;
e = strsep(&p, "_");
if (!e || strcmp(e, "depinfo") != 0)
Expand Down Expand Up @@ -788,8 +786,7 @@ rc_deptree_update(void)
RC_STRINGLIST *config, *dupes, *types, *sorted, *visited;
RC_STRING *s, *s2, *s2_np, *s3, *s4;
char *line = NULL;
size_t len = 0;
ssize_t size;
size_t size;
char *depend, *depends, *service, *type;
size_t i, l;
bool retval = true;
Expand All @@ -811,8 +808,7 @@ rc_deptree_update(void)
config = rc_stringlist_new();

deptree = make_deptree();
while ((size = getline(&line, &len, fp)) != -1) {
line[size - 1] = '\0';
while (xgetline(&line, &size, fp) != -1) {
depends = line;
service = strsep(&depends, " ");
if (!service || !*service)
Expand Down Expand Up @@ -894,8 +890,8 @@ rc_deptree_update(void)
* work for them. This doesn't stop them from being run directly. */
if (sys) {
char *nosys, *onosys;
size_t len = strlen(sys);

len = strlen(sys);
nosys = xmalloc(len + 2);
nosys[0] = '-';
for (i = 0; i < len; i++)
Expand Down
30 changes: 2 additions & 28 deletions src/librc/librc-misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,38 +102,13 @@ rc_getfile(const char *file, char **buffer, size_t *len)
return ret;
}

ssize_t
rc_getline(char **line, size_t *len, FILE *fp)
{
char *p;
size_t last = 0;

while (!feof(fp)) {
if (*line == NULL || last != 0) {
*len += BUFSIZ;
*line = xrealloc(*line, *len);
}
p = *line + last;
memset(p, 0, BUFSIZ);
if (fgets(p, BUFSIZ, fp) == NULL)
break;
last += strlen(p);
if (last && (*line)[last - 1] == '\n') {
(*line)[last - 1] = '\0';
break;
}
}
return last;
}

char *
rc_proc_getent(const char *ent RC_UNUSED)
{
#ifdef __linux__
FILE *fp;
char *proc = NULL, *p, *value = NULL, *save;
size_t i, len;
ssize_t size;

if (!exists("/proc/cmdline"))
return NULL;
Expand All @@ -142,11 +117,10 @@ rc_proc_getent(const char *ent RC_UNUSED)
return NULL;

i = 0;
if ((size = getline(&proc, &i, fp)) == -1) {
if (xgetline(&proc, &i, fp) == -1) {
free(proc);
return NULL;
}
proc[size - 1] = '\0';
save = proc;

len = strlen(ent);
Expand Down Expand Up @@ -184,7 +158,7 @@ rc_config_list(const char *file)
if (!(fp = fopen(file, "r")))
return list;

while ((rc_getline(&buffer, &len, fp))) {
while (xgetline(&buffer, &len, fp) != -1) {
p = buffer;
/* Strip leading spaces/tabs */
while ((*p == ' ') || (*p == '\t'))
Expand Down
25 changes: 16 additions & 9 deletions src/librc/librc.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ file_regex(const char *file, const char *regex)
{
FILE *fp;
char *line = NULL;
size_t size = 0, len = 0;
size_t size = 0;
ssize_t len = 0;
regex_t re;
bool retval = true;
int result;
Expand All @@ -192,7 +193,7 @@ file_regex(const char *file, const char *regex)
return false;
}

while ((len = rc_getline(&line, &size, fp))) {
while ((len = xgetline(&line, &size, fp)) != -1) {
char *str = line;
/* some /proc files have \0 separated content so we have to
loop through the 'line' */
Expand Down Expand Up @@ -702,19 +703,22 @@ rc_service_extra_commands(const char *service)
snprintf(cmd, l, OPTSTR, svc);
free(svc);

if ((fp = popen(cmd, "r"))) {
rc_getline(&buffer, &len, fp);
if (!(fp = popen(cmd, "r"))) {
free(cmd);
return NULL;
}

if (xgetline(&buffer, &len, fp) != -1) {
p = buffer;
commands = rc_stringlist_new();

while ((token = strsep(&p, " ")))
if (token[0] != '\0')
rc_stringlist_add(commands, token);

pclose(fp);
free(buffer);
}

pclose(fp);
free(buffer);
free(cmd);
return commands;
}
Expand All @@ -726,7 +730,7 @@ rc_service_description(const char *service, const char *option)
char *svc;
char *cmd;
char *desc = NULL;
size_t len = 0;
size_t size = 0;
FILE *fp;
size_t l;

Expand All @@ -741,7 +745,10 @@ 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 (xgetline(&desc, &size, fp) == -1) {
free(desc);
desc = NULL;
}
pclose(fp);
}
free(cmd);
Expand Down
6 changes: 1 addition & 5 deletions src/librc/rc.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,9 @@ typedef LIST_HEAD(rc_pidlist, rc_pid) RC_PIDLIST;
* @return NULL terminated list of pids */
RC_PIDLIST *rc_find_pids(const char *, const char *const *, uid_t, pid_t);

/* Basically the same as rc_getline() below, it just returns multiple lines */
/* Basically the same as getline(), it just returns multiple lines */
bool rc_getfile(const char *, char **, size_t *);

/* getline is a handy glibc function that not all libcs have, so
* we have our own */
ssize_t rc_getline(char **, size_t *, FILE *);

/* __END_DECLS */
#ifdef __cplusplus
}
Expand Down
1 change: 0 additions & 1 deletion src/librc/rc.map
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ global:
rc_environ_fd;
rc_find_pids;
rc_getfile;
rc_getline;
rc_newer_than;
rc_older_than;
rc_proc_getent;
Expand Down
2 changes: 1 addition & 1 deletion src/mountinfo/mountinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ find_mounts(struct args *args)
list = rc_stringlist_new();

buffer = NULL;
while (getline(&buffer, &size, fp) != -1) {
while (xgetline(&buffer, &size, fp) != -1) {
netdev = -1;
p = buffer;
from = strsep(&p, " ");
Expand Down
8 changes: 4 additions & 4 deletions src/openrc/rc.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,11 @@ static char *get_krunlevel(void)
return NULL;
}

if (getline(&buffer, &i, fp) != -1) {
i = strlen(buffer);
if (buffer[i - 1] == '\n')
buffer[i - 1] = 0;
if (xgetline(&buffer, &i, fp) == -1) {
free(buffer);
buffer = NULL;
}

fclose(fp);
return buffer;
}
Expand Down
12 changes: 12 additions & 0 deletions src/shared/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,16 @@ RC_UNUSED RC_PRINTF(2,3) static int xasprintf(char **strp, const char *fmt, ...)
return len;
}

RC_UNUSED static ssize_t xgetline(char **restrict lineptr, size_t *restrict n, FILE *restrict stream)
{
ssize_t ret = getline(lineptr, n, stream);
if (ret <= 0)
return ret;

if ((*lineptr)[ret - 1] == '\n')
navi-desu marked this conversation as resolved.
Show resolved Hide resolved
(*lineptr)[--ret] = '\0';

return ret;
}

#endif
11 changes: 4 additions & 7 deletions src/shared/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,14 @@ env_config(void)
free(e);

if ((fp = fopen(RC_KRUNLEVEL, "r"))) {
if (getline(&buffer, &size, fp) != -1) {
l = strlen (buffer) - 1;
if (buffer[l] == '\n')
buffer[l] = 0;
if (xgetline(&buffer, &size, fp) != -1)
setenv("RC_DEFAULTLEVEL", buffer, 1);
}
free(buffer);
fclose(fp);
} else
} else {
setenv("RC_DEFAULTLEVEL", RC_LEVEL_DEFAULT, 1);
}

free(buffer);
if (sys)
setenv("RC_SYS", sys, 1);

Expand Down
3 changes: 1 addition & 2 deletions src/shared/selinux.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ static int read_context_file(const char *filename, char **context)
char *p;
char *p2;
size_t len = 0;
ssize_t read;

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

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

while ((read = getline(&line, &len, fp)) != -1) {
while (xgetline(&line, &len, fp) != -1) {
/* cut off spaces before the string */
p = line;
while (isspace(*p) && *p != '\0')
Expand Down
6 changes: 1 addition & 5 deletions src/start-stop-daemon/start-stop-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ int main(int argc, char **argv)
fp = fopen(exec_file, "r");
if (fp) {
line = NULL;
if (getline(&line, &size, fp) == -1)
if (xgetline(&line, &size, fp) == -1)
eerrorx("%s: %s", applet, strerror(errno));
p = line;
fclose(fp);
Expand All @@ -754,10 +754,6 @@ int main(int argc, char **argv)
/* Strip leading spaces */
while (*p == ' ' || *p == '\t')
p++;
/* Remove the trailing newline */
len = strlen(p) - 1;
if (p[len] == '\n')
p[len] = '\0';
token = strsep(&p, " ");
free(exec_file);
xasprintf(&exec_file, "%s", token);
Expand Down
Loading