X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Futil.c;h=a2def28730c935f0f3bc684ac37b5ebe8410d11e;hb=b95cf3629e8d78a0d28e71b0f5559fa9a8c038b5;hp=39dca0d4587ffdea104964a078be4e133506b017;hpb=5b1fe56a8b5543df614091d3cbcdb6b87e294dc0;p=elogind.git diff --git a/src/util.c b/src/util.c index 39dca0d45..a2def2873 100644 --- a/src/util.c +++ b/src/util.c @@ -62,11 +62,11 @@ static struct selabel_handle *label_hnd = NULL; -static inline int use_selinux(void) { +static inline bool use_selinux(void) { static int use_selinux_ind = -1; - if (use_selinux_ind == -1) - use_selinux_ind = (is_selinux_enabled() == 1); + if (use_selinux_ind < 0) + use_selinux_ind = is_selinux_enabled() > 0; return use_selinux_ind; } @@ -84,6 +84,8 @@ static int label_get_file_label_from_path( r = getfilecon(path, &dir_con); if (r >= 0) { r = -1; + errno = EINVAL; + if ((sclass = string_to_security_class(class)) != 0) r = security_compute_create((security_context_t) label, dir_con, sclass, fcon); } @@ -122,7 +124,7 @@ int label_fix(const char *path) { struct stat st; security_context_t fcon; - if (!use_selinux()) + if (!use_selinux() || !label_hnd) return 0; r = lstat(path, &st); @@ -147,7 +149,7 @@ int label_fix(const char *path) { void label_finish(void) { #ifdef HAVE_SELINUX - if (use_selinux()) + if (use_selinux() && label_hnd) selabel_close(label_hnd); #endif } @@ -270,7 +272,7 @@ static int label_mkdir( int r; security_context_t fcon = NULL; - if (use_selinux()) { + if (use_selinux() && label_hnd) { if (path[0] == '/') { r = selabel_lookup_raw(label_hnd, &fcon, path, mode); } @@ -300,7 +302,7 @@ static int label_mkdir( r = mkdir(path, mode); finish: - if (use_selinux()) { + if (use_selinux() && label_hnd) { setfscreatecon(NULL); freecon(fcon); } @@ -1934,6 +1936,12 @@ char *format_timespan(char *buf, size_t l, usec_t t) { if (t == (usec_t) -1) return NULL; + if (t == 0) { + snprintf(p, l, "0"); + p[l-1] = 0; + return p; + } + /* The result of this function can be parsed with parse_usec */ for (i = 0; i < ELEMENTSOF(table); i++) { @@ -3077,18 +3085,18 @@ char *replace_env(const char *format, char **env) { case VARIABLE: if (*e == '}') { - char *t; + const char *t; - if ((t = strv_env_get_with_length(env, word+2, e-word-2))) { - if (!(k = strappend(r, t))) - goto fail; + if (!(t = strv_env_get_with_length(env, word+2, e-word-2))) + t = ""; - free(r); - r = k; + if (!(k = strappend(r, t))) + goto fail; - word = e+1; - } + free(r); + r = k; + word = e+1; state = WORD; } break; @@ -3118,36 +3126,39 @@ char **replace_env_argv(char **argv, char **env) { STRV_FOREACH(i, argv) { /* If $FOO appears as single word, replace it by the split up variable */ - if ((*i)[0] == '$') { - char *e = strv_env_get(env, *i+1); + if ((*i)[0] == '$' && (*i)[1] != '{') { + char *e; + char **w, **m; + unsigned q; - if (e) { - char **w, **m; - unsigned q; + if ((e = strv_env_get(env, *i+1))) { if (!(m = strv_split_quoted(e))) { r[k] = NULL; strv_free(r); return NULL; } + } else + m = NULL; - q = strv_length(m); - l = l + q - 1; + q = strv_length(m); + l = l + q - 1; - if (!(w = realloc(r, sizeof(char*) * (l+1)))) { - r[k] = NULL; - strv_free(r); - strv_free(m); - return NULL; - } + if (!(w = realloc(r, sizeof(char*) * (l+1)))) { + r[k] = NULL; + strv_free(r); + strv_free(m); + return NULL; + } - r = w; + r = w; + if (m) { memcpy(r + k, m, q * sizeof(char*)); free(m); - - k += q; - continue; } + + k += q; + continue; } /* If ${FOO} appears as part of a word, replace it by the variable as-is */