#include <sys/utsname.h>
#include <pwd.h>
#include <netinet/ip.h>
+#include <linux/kd.h>
+#include <dlfcn.h>
#include "macro.h"
#include "util.h"
#include "missing.h"
#include "log.h"
#include "strv.h"
+#include "label.h"
bool streq_ptr(const char *a, const char *b) {
fclose(f);
+ /* Kernel threads have no argv[] */
+ if (r[0] == 0) {
+ char *t;
+ int h;
+
+ free(r);
+
+ if ((h = get_process_name(pid, &t)) < 0)
+ return h;
+
+ h = asprintf(&r, "[%s]", t);
+ free(t);
+
+ if (h < 0)
+ return -ENOMEM;
+ }
+
*line = r;
return 0;
}
return 0;
}
+int parent_of_path(const char *path, char **_r) {
+ const char *e, *a = NULL, *b = NULL, *p;
+ char *r;
+ bool slash = false;
+
+ assert(path);
+ assert(_r);
+
+ if (!*path)
+ return -EINVAL;
+
+ for (e = path; *e; e++) {
+
+ if (!slash && *e == '/') {
+ a = b;
+ b = e;
+ slash = true;
+ } else if (slash && *e != '/')
+ slash = false;
+ }
+
+ if (*(e-1) == '/')
+ p = a;
+ else
+ p = b;
+
+ if (!p)
+ return -EINVAL;
+
+ if (p == path)
+ r = strdup("/");
+ else
+ r = strndup(path, p-path);
+
+ if (!r)
+ return -ENOMEM;
+
+ *_r = r;
+ return 0;
+}
+
+
char *file_name_from_path(const char *p) {
char *r;
int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
struct stat st;
- if (mkdir(path, mode) >= 0)
+ if (label_mkdir(path, mode) >= 0)
if (chmod_and_chown(path, mode, uid, gid) < 0)
return -errno;
if (!(t = strndup(path, e - path)))
return -ENOMEM;
- r = mkdir(t, mode);
-
+ r = label_mkdir(t, mode);
free(t);
if (r < 0 && errno != EEXIST)
if ((r = mkdir_parents(path, mode)) < 0)
return r;
- if (mkdir(path, mode) < 0)
+ if (label_mkdir(path, mode) < 0 && errno != EEXIST)
return -errno;
return 0;
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++) {
int reset_terminal(int fd) {
struct termios termios;
int r = 0;
+ long arg;
+
+ /* Set terminal to some sane defaults */
assert(fd >= 0);
- /* Set terminal to some sane defaults */
+ /* First, unlock termios */
+ zero(termios);
+ ioctl(fd, TIOCSLCKTRMIOS, &termios);
+
+ /* Disable exclusive mode, just in case */
+ ioctl(fd, TIOCNXCL);
+
+ /* Enable console unicode mode */
+ arg = K_UNICODE;
+ ioctl(fd, KDSKBMODE, &arg);
if (tcgetattr(fd, &termios) < 0) {
r = -errno;
}
if (e.wd != wd || !(e.mask & IN_CLOSE)) {
- r = -errno;
+ r = -EIO;
goto fail;
}
int path_is_mount_point(const char *t) {
struct stat a, b;
- char *copy;
+ char *parent;
+ int r;
if (lstat(t, &a) < 0) {
-
if (errno == ENOENT)
return 0;
return -errno;
}
- if (!(copy = strdup(t)))
- return -ENOMEM;
+ if ((r = parent_of_path(t, &parent)) < 0)
+ return r;
- if (lstat(dirname(copy), &b) < 0) {
- free(copy);
- return -errno;
- }
+ r = lstat(parent, &b);
+ free(parent);
- free(copy);
+ if (r < 0)
+ return -errno;
return a.st_dev != b.st_dev;
}
return strdup(u.sysname);
}
-int getmachineid_malloc(char **b) {
- int r;
-
- assert(b);
-
- if ((r = read_one_line_file("/var/lib/dbus/machine-id", b)) < 0)
- return r;
-
- strstrip(*b);
- return 0;
-}
-
char* getlogname_malloc(void) {
uid_t uid;
long bufsize;
if (!(d = fdopendir(fd))) {
close_nointr_nofail(fd);
- return -errno;
+
+ return errno == ENOENT ? 0 : -errno;
}
for (;;) {
struct stat st;
if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
- if (ret == 0)
+ if (ret == 0 && errno != ENOENT)
ret = -errno;
continue;
}
int subdir_fd;
if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
- if (ret == 0)
+ if (ret == 0 && errno != ENOENT)
ret = -errno;
continue;
}
}
if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
- if (ret == 0)
+ if (ret == 0 && errno != ENOENT)
ret = -errno;
}
} else if (!only_dirs) {
if (unlinkat(fd, de->d_name, 0) < 0) {
- if (ret == 0)
+ if (ret == 0 && errno != ENOENT)
ret = -errno;
}
}
char *replace_env(const char *format, char **env) {
enum {
WORD,
- DOLLAR,
+ CURLY,
VARIABLE
} state = WORD;
case WORD:
if (*e == '$')
- state = DOLLAR;
+ state = CURLY;
break;
- case DOLLAR:
- if (*e == '(') {
+ case CURLY:
+ if (*e == '{') {
if (!(k = strnappend(r, word, e-word-1)))
goto fail;
break;
case VARIABLE:
- if (*e == ')') {
- char *t;
+ if (*e == '}') {
+ 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;
char **replace_env_argv(char **argv, char **env) {
char **r, **i;
- unsigned k;
+ unsigned k = 0, l = 0;
- if (!(r = new(char*, strv_length(argv)+1)))
+ l = strv_length(argv);
+
+ if (!(r = new(char*, l+1)))
return NULL;
STRV_FOREACH(i, argv) {
+
+ /* If $FOO appears as single word, replace it by the split up variable */
+ if ((*i)[0] == '$' && (*i)[1] != '{') {
+ char *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;
+
+ if (!(w = realloc(r, sizeof(char*) * (l+1)))) {
+ r[k] = NULL;
+ strv_free(r);
+ strv_free(m);
+ return NULL;
+ }
+
+ r = w;
+ if (m) {
+ memcpy(r + k, m, q * sizeof(char*));
+ free(m);
+ }
+
+ k += q;
+ continue;
+ }
+
+ /* If ${FOO} appears as part of a word, replace it by the variable as-is */
if (!(r[k++] = replace_env(*i, env))) {
strv_free(r);
return NULL;
return r;
}
+int columns(void) {
+ static __thread int parsed_columns = 0;
+ const char *e;
+
+ if (parsed_columns > 0)
+ return parsed_columns;
+
+ if ((e = getenv("COLUMNS")))
+ parsed_columns = atoi(e);
+
+ if (parsed_columns <= 0) {
+ struct winsize ws;
+ zero(ws);
+
+ if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
+ parsed_columns = ws.ws_col;
+ }
+
+ if (parsed_columns <= 0)
+ parsed_columns = 80;
+
+ return parsed_columns;
+}
+
+int running_in_chroot(void) {
+ struct stat a, b;
+
+ zero(a);
+ zero(b);
+
+ /* Only works as root */
+
+ if (stat("/proc/1/root", &a) < 0)
+ return -errno;
+
+ if (stat("/", &b) < 0)
+ return -errno;
+
+ return
+ a.st_dev != b.st_dev ||
+ a.st_ino != b.st_ino;
+}
+
+char *ellipsize(const char *s, unsigned length, unsigned percent) {
+ size_t l, x;
+ char *r;
+
+ assert(s);
+ assert(percent <= 100);
+ assert(length >= 3);
+
+ l = strlen(s);
+
+ if (l <= 3 || l <= length)
+ return strdup(s);
+
+ if (!(r = new0(char, length+1)))
+ return r;
+
+ x = (length * percent) / 100;
+
+ if (x > length - 3)
+ x = length - 3;
+
+ memcpy(r, s, x);
+ r[x] = '.';
+ r[x+1] = '.';
+ r[x+2] = '.';
+ memcpy(r + x + 3,
+ s + l - (length - x - 3),
+ length - x - 3);
+
+ return r;
+}
+
+void nss_disable_nscd(void) {
+
+ void (*func)(void);
+
+ /* This is an internal glibc function call. We are not
+ * supposed to call this, because we are not nscd. However
+ * sometimes we feel really dangerous and do it
+ * nonetheless. Muahahah! But at least we protect this with a
+ * dlsym() just in case glibc takes this away from us. */
+
+ if ((func = dlsym(RTLD_DEFAULT, "__nss_disable_nscd"))) {
+ log_debug("Disabling nscd.");
+ func();
+ } else
+ log_debug("Cannot disable nscd.");
+}
+
+
static const char *const ioprio_class_table[] = {
[IOPRIO_CLASS_NONE] = "none",
[IOPRIO_CLASS_RT] = "realtime",
};
DEFINE_STRING_TABLE_LOOKUP(ip_tos, int);
+
+static const char *const signal_table[] = {
+ [SIGHUP] = "HUP",
+ [SIGINT] = "INT",
+ [SIGQUIT] = "QUIT",
+ [SIGILL] = "ILL",
+ [SIGTRAP] = "TRAP",
+ [SIGABRT] = "ABRT",
+ [SIGBUS] = "BUS",
+ [SIGFPE] = "FPE",
+ [SIGKILL] = "KILL",
+ [SIGUSR1] = "USR1",
+ [SIGSEGV] = "SEGV",
+ [SIGUSR2] = "USR2",
+ [SIGPIPE] = "PIPE",
+ [SIGALRM] = "ALRM",
+ [SIGTERM] = "TERM",
+ [SIGSTKFLT] = "STKFLT",
+ [SIGCHLD] = "CHLD",
+ [SIGCONT] = "CONT",
+ [SIGSTOP] = "STOP",
+ [SIGTSTP] = "TSTP",
+ [SIGTTIN] = "TTIN",
+ [SIGTTOU] = "TTOU",
+ [SIGURG] = "URG",
+ [SIGXCPU] = "XCPU",
+ [SIGXFSZ] = "XFSZ",
+ [SIGVTALRM] = "VTALRM",
+ [SIGPROF] = "PROF",
+ [SIGWINCH] = "WINCH",
+ [SIGIO] = "IO",
+ [SIGPWR] = "PWR",
+ [SIGSYS] = "SYS"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(signal, int);