summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-09-14 10:24:27 +0200
committerLennart Poettering <lennart@poettering.net>2012-09-14 10:24:27 +0200
commita05f97b3ac590bc313e8c9d1613d06de0bf28ae2 (patch)
treec4b980a222b8512a5dc20d462e6f9bdbcaf6113e /src
parente67f47e55b850c2a3a52c550726b1fd7394f9276 (diff)
util: various additional modernizations
Diffstat (limited to 'src')
-rw-r--r--src/shared/macro.h1
-rw-r--r--src/shared/unit-name.c3
-rw-r--r--src/shared/util.c104
-rw-r--r--src/shared/util.h1
4 files changed, 48 insertions, 61 deletions
diff --git a/src/shared/macro.h b/src/shared/macro.h
index eb9a8c0a72..f8c5656538 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -190,5 +190,6 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
#define _cleanup_free_ __attribute__((cleanup(freep)))
#define _cleanup_fclose_ __attribute__((cleanup(fclosep)))
#define _cleanup_close_ __attribute__((cleanup(closep)))
+#define _cleanup_closedir_ __attribute__((cleanup(closedirp)))
#include "log.h"
diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c
index d6391228f2..cfe3133b5a 100644
--- a/src/shared/unit-name.c
+++ b/src/shared/unit-name.c
@@ -477,8 +477,7 @@ char *unit_name_mangle(const char *name) {
/* Try to turn a string that might not be a unit name into a
* sensible unit name. */
- if (path_startswith(name, "/dev/") ||
- path_startswith(name, "/sys/"))
+ if (is_device_path(name))
return unit_name_from_path(name, ".device");
if (path_is_absolute(name))
diff --git a/src/shared/util.c b/src/shared/util.c
index 7be0df2a2b..007f15d680 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2043,29 +2043,23 @@ char *format_timespan(char *buf, size_t l, usec_t t) {
}
bool fstype_is_network(const char *fstype) {
- static const char * const table[] = {
- "cifs",
- "smbfs",
- "ncpfs",
- "nfs",
- "nfs4",
- "gfs",
- "gfs2"
- };
-
- unsigned i;
+ static const char table[] =
+ "cifs\0"
+ "smbfs\0"
+ "ncpfs\0"
+ "nfs\0"
+ "nfs4\0"
+ "gfs\0"
+ "gfs2";
- for (i = 0; i < ELEMENTSOF(table); i++)
- if (streq(table[i], fstype))
- return true;
-
- return false;
+ return nulstr_contains(table, fstype);
}
int chvt(int vt) {
- int fd, r = 0;
+ _cleanup_close_ int fd;
- if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
+ fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
+ if (fd < 0)
return -errno;
if (vt < 0) {
@@ -2074,20 +2068,16 @@ int chvt(int vt) {
0
};
- if (ioctl(fd, TIOCLINUX, tiocl) < 0) {
- r = -errno;
- goto fail;
- }
+ if (ioctl(fd, TIOCLINUX, tiocl) < 0)
+ return -errno;
vt = tiocl[0] <= 0 ? 1 : tiocl[0];
}
if (ioctl(fd, VT_ACTIVATE, vt) < 0)
- r = -errno;
+ return -errno;
-fail:
- close_nointr_nofail(fd);
- return r;
+ return 0;
}
int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
@@ -2952,36 +2942,30 @@ bool is_device_path(const char *path) {
}
int dir_is_empty(const char *path) {
- DIR *d;
+ _cleanup_closedir_ DIR *d;
int r;
- struct dirent buf, *de;
- if (!(d = opendir(path)))
+ d = opendir(path);
+ if (!d)
return -errno;
for (;;) {
- if ((r = readdir_r(d, &buf, &de)) > 0) {
- r = -r;
- break;
- }
+ struct dirent buf, *de;
- if (!de) {
- r = 1;
- break;
- }
+ r = readdir_r(d, &buf, &de);
+ if (r > 0)
+ return -r;
- if (!ignore_file(de->d_name)) {
- r = 0;
- break;
- }
- }
+ if (!de)
+ return 1;
- closedir(d);
- return r;
+ if (!ignore_file(de->d_name))
+ return 0;
+ }
}
unsigned long long random_ull(void) {
- int fd;
+ _cleanup_close_ int fd;
uint64_t ull;
ssize_t r;
@@ -2990,8 +2974,6 @@ unsigned long long random_ull(void) {
goto fallback;
r = loop_read(fd, &ull, sizeof(ull), true);
- close_nointr_nofail(fd);
-
if (r != sizeof(ull))
goto fallback;
@@ -3065,7 +3047,8 @@ bool hostname_is_set(void) {
static char *lookup_uid(uid_t uid) {
long bufsize;
- char *buf, *name;
+ char *name;
+ _cleanup_free_ char *buf = NULL;
struct passwd pwbuf, *pw = NULL;
/* Shortcut things to avoid NSS lookups */
@@ -3080,13 +3063,8 @@ static char *lookup_uid(uid_t uid) {
if (!buf)
return NULL;
- if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
- name = strdup(pw->pw_name);
- free(buf);
- return name;
- }
-
- free(buf);
+ if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw)
+ return strdup(pw->pw_name);
if (asprintf(&name, "%lu", (unsigned long) uid) < 0)
return NULL;
@@ -3122,12 +3100,14 @@ int getttyname_malloc(int fd, char **r) {
assert(r);
- if ((k = ttyname_r(fd, path, sizeof(path))) != 0)
+ k = ttyname_r(fd, path, sizeof(path));
+ if (k != 0)
return -k;
char_array_0(path);
- if (!(c = strdup(startswith(path, "/dev/") ? path + 5 : path)))
+ c = strdup(startswith(path, "/dev/") ? path + 5 : path);
+ if (!c)
return -ENOMEM;
*r = c;
@@ -3138,7 +3118,8 @@ int getttyname_harder(int fd, char **r) {
int k;
char *s;
- if ((k = getttyname_malloc(fd, &s)) < 0)
+ k = getttyname_malloc(fd, &s);
+ if (k < 0)
return k;
if (streq(s, "tty")) {
@@ -5706,7 +5687,7 @@ bool is_valid_documentation_url(const char *url) {
}
bool in_initrd(void) {
- static int saved = -1;
+ static __thread int saved = -1;
struct statfs s;
if (saved >= 0)
@@ -5877,3 +5858,8 @@ void closep(int *fd) {
if (*fd >= 0)
close_nointr_nofail(*fd);
}
+
+void closedirp(DIR **d) {
+ if (*d)
+ closedir(*d);
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 90234eb27b..a3f825b5bb 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -532,3 +532,4 @@ int get_home_dir(char **ret);
void freep(void *p);
void fclosep(FILE **f);
void closep(int *fd);
+void closedirp(DIR **d);