[systemd-devel] [PATCH 4/5] util.c: use read_one_line_file where possible
Simon Peeters
peeters.simon at gmail.com
Fri Jan 3 17:35:26 PST 2014
---
src/shared/util.c | 48 +++++++++++++++---------------------------------
1 file changed, 15 insertions(+), 33 deletions(-)
diff --git a/src/shared/util.c b/src/shared/util.c
index db3051d..354d7eb 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -404,8 +404,7 @@ char *split(const char *c, size_t *l, const char *separator, bool quoted, char *
int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
int r;
- _cleanup_fclose_ FILE *f = NULL;
- char line[LINE_MAX];
+ _cleanup_free_ char *line = NULL;
long unsigned ppid;
const char *p;
@@ -418,14 +417,9 @@ int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
}
p = procfs_file_alloca(pid, "stat");
- f = fopen(p, "re");
- if (!f)
- return -errno;
-
- if (!fgets(line, sizeof(line), f)) {
- r = feof(f) ? -EIO : -errno;
+ r = read_one_line_file(p, &line);
+ if (r < 0)
return r;
- }
/* Let's skip the pid and comm fields. The latter is enclosed
* in () but does not escape any () in its value, so let's
@@ -452,25 +446,17 @@ int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
}
int get_starttime_of_pid(pid_t pid, unsigned long long *st) {
- _cleanup_fclose_ FILE *f = NULL;
- char line[LINE_MAX];
+ int r;
+ _cleanup_free_ char *line = NULL;
const char *p;
assert(pid >= 0);
assert(st);
p = procfs_file_alloca(pid, "stat");
-
- f = fopen(p, "re");
- if (!f)
- return errno == ENOENT ? -ESRCH : -errno;
-
- if (!fgets(line, sizeof(line), f)) {
- if (ferror(f))
- return -errno;
-
- return -EIO;
- }
+ r = read_one_line_file(p, &line);
+ if (r < 0)
+ return r;
/* Let's skip the pid and comm fields. The latter is enclosed
* in () but does not escape any () in its value, so let's
@@ -2491,21 +2477,17 @@ int getttyname_harder(int fd, char **r) {
}
int get_ctty_devnr(pid_t pid, dev_t *d) {
- _cleanup_fclose_ FILE *f = NULL;
- char line[LINE_MAX], *p;
+ int r;
+ _cleanup_free_ char *line = NULL;
+ const char *p;
unsigned long ttynr;
- const char *fn;
assert(pid >= 0);
- fn = procfs_file_alloca(pid, "stat");
-
- f = fopen(fn, "re");
- if (!f)
- return -errno;
-
- if (!fgets(line, sizeof(line), f))
- return feof(f) ? -EIO : -errno;
+ p = procfs_file_alloca(pid, "stat");
+ r = read_one_line_file(p, &line);
+ if (r < 0)
+ return r;
p = strrchr(line, ')');
if (!p)
--
1.8.5.2
More information about the systemd-devel
mailing list