along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
+#include <libintl.h>
#include <stdio.h>
#include <syslog.h>
#include <sched.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/ioctl.h>
-#include <linux/vt.h>
-#include <linux/tiocl.h>
-#include <termios.h>
#include <stdarg.h>
-#include <sys/inotify.h>
-#include <sys/poll.h>
+#include <poll.h>
#include <ctype.h>
#include <sys/prctl.h>
#include <sys/utsname.h>
#include <pwd.h>
#include <netinet/ip.h>
-#include <linux/kd.h>
-#include <dlfcn.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <glob.h>
#include <langinfo.h>
#include <locale.h>
#include <sys/personality.h>
+#include <sys/xattr.h>
+#include <sys/statvfs.h>
+#include <sys/file.h>
+#include <linux/fs.h>
+
+/* When we include libgen.h because we need dirname() we immediately
+ * undefine basename() since libgen.h defines it as a macro to the XDG
+ * version which is really broken. */
#include <libgen.h>
#undef basename
#include <sys/auxv.h>
#endif
+#include "config.h"
#include "macro.h"
#include "util.h"
#include "ioprio.h"
#include "missing.h"
#include "log.h"
#include "strv.h"
-#include "label.h"
#include "mkdir.h"
#include "path-util.h"
#include "exit-status.h"
#include "gunicode.h"
#include "virt.h"
#include "def.h"
+#include "sparse-endian.h"
+#include "formats-util.h"
+#include "process-util.h"
+#include "random-util.h"
+#include "terminal-util.h"
+
+/* Put this test here for a lack of better place */
+assert_cc(EAGAIN == EWOULDBLOCK);
int saved_argc = 0;
char **saved_argv = NULL;
-static volatile unsigned cached_columns = 0;
-static volatile unsigned cached_lines = 0;
-
size_t page_size(void) {
static thread_local size_t pgsz = 0;
long r;
return (char*) p;
}
-static size_t cescape_char(char c, char *buf) {
+size_t cescape_char(char c, char *buf) {
char * buf_old = buf;
switch (c) {
int r;
assert(s);
- assert(ret_uid);
r = safe_atolu(s, &ul);
if (r < 0)
if (uid == (uid_t) 0xFFFF)
return -ENXIO;
- *ret_uid = uid;
+ if (ret_uid)
+ *ret_uid = uid;
+
return 0;
}
int safe_atod(const char *s, double *ret_d) {
char *x = NULL;
double d = 0;
+ locale_t loc;
assert(s);
assert(ret_d);
- RUN_WITH_LOCALE(LC_NUMERIC_MASK, "C") {
- errno = 0;
- d = strtod(s, &x);
- }
+ loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
+ if (loc == (locale_t) 0)
+ return -errno;
- if (!x || x == s || *x || errno)
+ errno = 0;
+ d = strtod_l(s, &x, loc);
+
+ if (!x || x == s || *x || errno) {
+ freelocale(loc);
return errno ? -errno : -EINVAL;
+ }
+ freelocale(loc);
*ret_d = (double) d;
return 0;
}
static size_t strcspn_escaped(const char *s, const char *reject) {
bool escaped = false;
- size_t n;
+ int n;
for (n=0; s[n]; n++) {
if (escaped)
else if (strchr(reject, s[n]))
break;
}
+
/* if s ends in \, return index of previous char */
return n - escaped;
}
*l = strcspn_escaped(current + 1, quotechars);
if (current[*l + 1] == '\0' ||
(current[*l + 2] && !strchr(separator, current[*l + 2]))) {
- /* right quote missing or garbage at the end*/
+ /* right quote missing or garbage at the end */
*state = current;
return NULL;
}
*state = current++ + *l + 2;
} else if (quoted) {
*l = strcspn_escaped(current, separator);
+ if (current[*l] && !strchr(separator, current[*l])) {
+ /* unfinished escape */
+ *state = current;
+ return NULL;
+ }
*state = current + *l;
} else {
*l = strcspn(current, separator);
return current;
}
-int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
- int r;
- _cleanup_free_ char *line = NULL;
- long unsigned ppid;
- const char *p;
-
- assert(pid >= 0);
- assert(_ppid);
-
- if (pid == 0) {
- *_ppid = getppid();
- return 0;
- }
-
- p = procfs_file_alloca(pid, "stat");
- 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
- * skip over it manually */
-
- p = strrchr(line, ')');
- if (!p)
- return -EIO;
-
- p++;
-
- if (sscanf(p, " "
- "%*c " /* state */
- "%lu ", /* ppid */
- &ppid) != 1)
- return -EIO;
-
- if ((long unsigned) (pid_t) ppid != ppid)
- return -ERANGE;
-
- *_ppid = (pid_t) ppid;
-
- return 0;
-}
-
int fchmod_umask(int fd, mode_t m) {
mode_t u;
int r;
return s;
}
-int get_process_state(pid_t pid) {
- const char *p;
- char state;
- int r;
- _cleanup_free_ char *line = NULL;
-
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "stat");
- r = read_one_line_file(p, &line);
- if (r < 0)
- return r;
-
- p = strrchr(line, ')');
- if (!p)
- return -EIO;
-
- p++;
-
- if (sscanf(p, " %c", &state) != 1)
- return -EIO;
-
- return (unsigned char) state;
-}
-
-int get_process_comm(pid_t pid, char **name) {
- const char *p;
- int r;
-
- assert(name);
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "comm");
-
- r = read_one_line_file(p, name);
- if (r == -ENOENT)
- return -ESRCH;
-
- return r;
-}
-
-int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
- _cleanup_fclose_ FILE *f = NULL;
- char *r = NULL, *k;
- const char *p;
- int c;
-
- assert(line);
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "cmdline");
-
- f = fopen(p, "re");
- if (!f)
- return -errno;
-
- if (max_length == 0) {
- size_t len = 0, allocated = 0;
-
- while ((c = getc(f)) != EOF) {
-
- if (!GREEDY_REALLOC(r, allocated, len+2)) {
- free(r);
- return -ENOMEM;
- }
-
- r[len++] = isprint(c) ? c : ' ';
- }
-
- if (len > 0)
- r[len-1] = 0;
-
- } else {
- bool space = false;
- size_t left;
-
- r = new(char, max_length);
- if (!r)
- return -ENOMEM;
-
- k = r;
- left = max_length;
- while ((c = getc(f)) != EOF) {
-
- if (isprint(c)) {
- if (space) {
- if (left <= 4)
- break;
-
- *(k++) = ' ';
- left--;
- space = false;
- }
-
- if (left <= 4)
- break;
-
- *(k++) = (char) c;
- left--;
- } else
- space = true;
- }
-
- if (left <= 4) {
- size_t n = MIN(left-1, 3U);
- memcpy(k, "...", n);
- k[n] = 0;
- } else
- *k = 0;
- }
-
- /* Kernel threads have no argv[] */
- if (isempty(r)) {
- _cleanup_free_ char *t = NULL;
- int h;
-
- free(r);
-
- if (!comm_fallback)
- return -ENOENT;
-
- h = get_process_comm(pid, &t);
- if (h < 0)
- return h;
-
- r = strjoin("[", t, "]", NULL);
- if (!r)
- return -ENOMEM;
- }
-
- *line = r;
- return 0;
-}
-
-int is_kernel_thread(pid_t pid) {
- const char *p;
- size_t count;
- char c;
- bool eof;
- FILE *f;
-
- if (pid == 0)
- return 0;
-
- assert(pid > 0);
-
- p = procfs_file_alloca(pid, "cmdline");
- f = fopen(p, "re");
- if (!f)
- return -errno;
-
- count = fread(&c, 1, 1, f);
- eof = feof(f);
- fclose(f);
-
- /* Kernel threads have an empty cmdline */
-
- if (count <= 0)
- return eof ? 1 : -errno;
-
- return 0;
-}
-
-int get_process_capeff(pid_t pid, char **capeff) {
- const char *p;
-
- assert(capeff);
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "status");
-
- return get_status_field(p, "\nCapEff:", capeff);
-}
-
-static int get_process_link_contents(const char *proc_file, char **name) {
- int r;
-
- assert(proc_file);
- assert(name);
-
- r = readlink_malloc(proc_file, name);
- if (r < 0)
- return r == -ENOENT ? -ESRCH : r;
-
- return 0;
-}
-
-int get_process_exe(pid_t pid, char **name) {
- const char *p;
- char *d;
- int r;
-
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "exe");
- r = get_process_link_contents(p, name);
- if (r < 0)
- return r;
-
- d = endswith(*name, " (deleted)");
- if (d)
- *d = '\0';
-
- return 0;
-}
-
-static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
- _cleanup_fclose_ FILE *f = NULL;
- char line[LINE_MAX];
- const char *p;
-
- assert(field);
- assert(uid);
-
- if (pid == 0)
- return getuid();
-
- p = procfs_file_alloca(pid, "status");
- f = fopen(p, "re");
- if (!f)
- return -errno;
-
- FOREACH_LINE(line, f, return -errno) {
- char *l;
-
- l = strstrip(line);
-
- if (startswith(l, field)) {
- l += strlen(field);
- l += strspn(l, WHITESPACE);
-
- l[strcspn(l, WHITESPACE)] = 0;
-
- return parse_uid(l, uid);
- }
- }
-
- return -EIO;
-}
-
-int get_process_uid(pid_t pid, uid_t *uid) {
- return get_process_id(pid, "Uid:", uid);
-}
-
-int get_process_gid(pid_t pid, gid_t *gid) {
- assert_cc(sizeof(uid_t) == sizeof(gid_t));
- return get_process_id(pid, "Gid:", gid);
-}
-
-int get_process_cwd(pid_t pid, char **cwd) {
- const char *p;
-
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "cwd");
-
- return get_process_link_contents(p, cwd);
-}
-
-int get_process_root(pid_t pid, char **root) {
- const char *p;
-
- assert(pid >= 0);
-
- p = procfs_file_alloca(pid, "root");
-
- return get_process_link_contents(p, root);
-}
-
-int get_process_environ(pid_t pid, char **env) {
- _cleanup_fclose_ FILE *f = NULL;
- _cleanup_free_ char *outcome = NULL;
- int c;
- const char *p;
- size_t allocated = 0, sz = 0;
-
- assert(pid >= 0);
- assert(env);
-
- p = procfs_file_alloca(pid, "environ");
-
- f = fopen(p, "re");
- if (!f)
- return -errno;
-
- while ((c = fgetc(f)) != EOF) {
- if (!GREEDY_REALLOC(outcome, allocated, sz + 5))
- return -ENOMEM;
-
- if (c == '\0')
- outcome[sz++] = '\n';
- else
- sz += cescape_char(c, outcome + sz);
- }
-
- outcome[sz] = '\0';
- *env = outcome;
- outcome = NULL;
-
- return 0;
-}
-
char *strnappend(const char *s, const char *suffix, size_t b) {
size_t a;
char *r;
}
char *file_in_same_dir(const char *path, const char *filename) {
- char *e, *r;
+ char *e, *ret;
size_t k;
assert(path);
if (path_is_absolute(filename))
return strdup(filename);
- if (!(e = strrchr(path, '/')))
+ e = strrchr(path, '/');
+ if (!e)
return strdup(filename);
k = strlen(filename);
- if (!(r = new(char, e-path+1+k+1)))
+ ret = new(char, (e + 1 - path) + k + 1);
+ if (!ret)
return NULL;
- memcpy(r, path, e-path+1);
- memcpy(r+(e-path)+1, filename, k+1);
-
- return r;
+ memcpy(mempcpy(ret, path, e + 1 - path), filename, k + 1);
+ return ret;
}
int rmdir_parents(const char *path, const char *stop) {
assert(s);
- /* Does C style string escaping. */
+ /* Does C style string escaping. May be reversed with
+ * cunescape(). */
r = new(char, strlen(s)*4 + 1);
if (!r)
return r;
}
-char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix) {
- char *r, *t;
- const char *f;
- size_t pl;
+static int cunescape_one(const char *p, size_t length, char *ret, uint32_t *ret_unicode) {
+ int r = 1;
- assert(s);
+ assert(p);
+ assert(*p);
+ assert(ret);
- /* Undoes C style string escaping, and optionally prefixes it. */
+ /* Unescapes C style. Returns the unescaped character in ret,
+ * unless we encountered a \u sequence in which case the full
+ * unicode character is returned in ret_unicode, instead. */
- pl = prefix ? strlen(prefix) : 0;
+ if (length != (size_t) -1 && length < 1)
+ return -EINVAL;
- r = new(char, pl+length+1);
- if (!r)
- return NULL;
+ switch (p[0]) {
+
+ case 'a':
+ *ret = '\a';
+ break;
+ case 'b':
+ *ret = '\b';
+ break;
+ case 'f':
+ *ret = '\f';
+ break;
+ case 'n':
+ *ret = '\n';
+ break;
+ case 'r':
+ *ret = '\r';
+ break;
+ case 't':
+ *ret = '\t';
+ break;
+ case 'v':
+ *ret = '\v';
+ break;
+ case '\\':
+ *ret = '\\';
+ break;
+ case '"':
+ *ret = '"';
+ break;
+ case '\'':
+ *ret = '\'';
+ break;
+
+ case 's':
+ /* This is an extension of the XDG syntax files */
+ *ret = ' ';
+ break;
+
+ case 'x': {
+ /* hexadecimal encoding */
+ int a, b;
- if (prefix)
- memcpy(r, prefix, pl);
+ if (length != (size_t) -1 && length < 3)
+ return -EINVAL;
- for (f = s, t = r + pl; f < s + length; f++) {
+ a = unhexchar(p[1]);
+ if (a < 0)
+ return -EINVAL;
- if (*f != '\\') {
- *(t++) = *f;
- continue;
- }
+ b = unhexchar(p[2]);
+ if (b < 0)
+ return -EINVAL;
- f++;
+ /* Don't allow NUL bytes */
+ if (a == 0 && b == 0)
+ return -EINVAL;
- switch (*f) {
+ *ret = (char) ((a << 4U) | b);
+ r = 3;
+ break;
+ }
- case 'a':
- *(t++) = '\a';
- break;
- case 'b':
- *(t++) = '\b';
- break;
- case 'f':
- *(t++) = '\f';
- break;
- case 'n':
- *(t++) = '\n';
- break;
- case 'r':
- *(t++) = '\r';
- break;
- case 't':
- *(t++) = '\t';
- break;
- case 'v':
- *(t++) = '\v';
- break;
- case '\\':
- *(t++) = '\\';
- break;
- case '"':
- *(t++) = '"';
- break;
- case '\'':
- *(t++) = '\'';
- break;
+ case 'u': {
+ /* C++11 style 16bit unicode */
- case 's':
- /* This is an extension of the XDG syntax files */
- *(t++) = ' ';
- break;
+ int a[4];
+ unsigned i;
+ uint32_t c;
- case 'x': {
- /* hexadecimal encoding */
- int a, b;
+ if (length != (size_t) -1 && length < 5)
+ return -EINVAL;
- a = unhexchar(f[1]);
- b = unhexchar(f[2]);
+ for (i = 0; i < 4; i++) {
+ a[i] = unhexchar(p[1 + i]);
+ if (a[i] < 0)
+ return a[i];
+ }
- if (a < 0 || b < 0 || (a == 0 && b == 0)) {
- /* Invalid escape code, let's take it literal then */
- *(t++) = '\\';
- *(t++) = 'x';
- } else {
- *(t++) = (char) ((a << 4) | b);
- f += 2;
+ c = ((uint32_t) a[0] << 12U) | ((uint32_t) a[1] << 8U) | ((uint32_t) a[2] << 4U) | (uint32_t) a[3];
+
+ /* Don't allow 0 chars */
+ if (c == 0)
+ return -EINVAL;
+
+ if (c < 128)
+ *ret = c;
+ else {
+ if (!ret_unicode)
+ return -EINVAL;
+
+ *ret = 0;
+ *ret_unicode = c;
+ }
+
+ r = 5;
+ break;
+ }
+
+ case 'U': {
+ /* C++11 style 32bit unicode */
+
+ int a[8];
+ unsigned i;
+ uint32_t c;
+
+ if (length != (size_t) -1 && length < 9)
+ return -EINVAL;
+
+ for (i = 0; i < 8; i++) {
+ a[i] = unhexchar(p[1 + i]);
+ if (a[i] < 0)
+ return a[i];
+ }
+
+ c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) |
+ ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7];
+
+ /* Don't allow 0 chars */
+ if (c == 0)
+ return -EINVAL;
+
+ /* Don't allow invalid code points */
+ if (!unichar_is_valid(c))
+ return -EINVAL;
+
+ if (c < 128)
+ *ret = c;
+ else {
+ if (!ret_unicode)
+ return -EINVAL;
+
+ *ret = 0;
+ *ret_unicode = c;
+ }
+
+ r = 9;
+ break;
+ }
+
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7': {
+ /* octal encoding */
+ int a, b, c;
+ uint32_t m;
+
+ if (length != (size_t) -1 && length < 4)
+ return -EINVAL;
+
+ a = unoctchar(p[0]);
+ if (a < 0)
+ return -EINVAL;
+
+ b = unoctchar(p[1]);
+ if (b < 0)
+ return -EINVAL;
+
+ c = unoctchar(p[2]);
+ if (c < 0)
+ return -EINVAL;
+
+ /* don't allow NUL bytes */
+ if (a == 0 && b == 0 && c == 0)
+ return -EINVAL;
+
+ /* Don't allow bytes above 255 */
+ m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c;
+ if (m > 255)
+ return -EINVAL;
+
+ *ret = m;
+ r = 3;
+ break;
+ }
+
+ default:
+ return -EINVAL;
+ }
+
+ return r;
+}
+
+int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
+ char *r, *t;
+ const char *f;
+ size_t pl;
+
+ assert(s);
+ assert(ret);
+
+ /* Undoes C style string escaping, and optionally prefixes it. */
+
+ pl = prefix ? strlen(prefix) : 0;
+
+ r = new(char, pl+length+1);
+ if (!r)
+ return -ENOMEM;
+
+ if (prefix)
+ memcpy(r, prefix, pl);
+
+ for (f = s, t = r + pl; f < s + length; f++) {
+ size_t remaining;
+ uint32_t u;
+ char c;
+ int k;
+
+ remaining = s + length - f;
+ assert(remaining > 0);
+
+ if (*f != '\\') {
+ /* A literal literal, copy verbatim */
+ *(t++) = *f;
+ continue;
+ }
+
+ if (remaining == 1) {
+ if (flags & UNESCAPE_RELAX) {
+ /* A trailing backslash, copy verbatim */
+ *(t++) = *f;
+ continue;
}
- break;
+ free(r);
+ return -EINVAL;
}
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7': {
- /* octal encoding */
- int a, b, c;
-
- a = unoctchar(f[0]);
- b = unoctchar(f[1]);
- c = unoctchar(f[2]);
-
- if (a < 0 || b < 0 || c < 0 || (a == 0 && b == 0 && c == 0)) {
+ k = cunescape_one(f + 1, remaining - 1, &c, &u);
+ if (k < 0) {
+ if (flags & UNESCAPE_RELAX) {
/* Invalid escape code, let's take it literal then */
*(t++) = '\\';
- *(t++) = f[0];
- } else {
- *(t++) = (char) ((a << 6) | (b << 3) | c);
- f += 2;
+ continue;
}
- break;
+ free(r);
+ return k;
}
- case 0:
- /* premature end of string.*/
- *(t++) = '\\';
- goto finish;
+ if (c != 0)
+ /* Non-Unicode? Let's encode this directly */
+ *(t++) = c;
+ else
+ /* Unicode? Then let's encode this in UTF-8 */
+ t += utf8_encode_unichar(t, u);
- default:
- /* Invalid escape code, let's take it literal then */
- *(t++) = '\\';
- *(t++) = *f;
- break;
- }
+ f += k;
}
-finish:
*t = 0;
- return r;
-}
-char *cunescape_length(const char *s, size_t length) {
- return cunescape_length_with_prefix(s, length, NULL);
+ *ret = r;
+ return t - r;
}
-char *cunescape(const char *s) {
- assert(s);
+int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
+ return cunescape_length_with_prefix(s, length, NULL, flags, ret);
+}
- return cunescape_length(s, strlen(s));
+int cunescape(const char *s, UnescapeFlags flags, char **ret) {
+ return cunescape_length(s, strlen(s), flags, ret);
}
char *xescape(const char *s, const char *bad) {
/* Escapes all chars in bad, in addition to \ and all special
* chars, in \xFF style escaping. May be reversed with
- * cunescape. */
+ * cunescape(). */
r = new(char, strlen(s) * 4 + 1);
if (!r)
return t;
}
-_pure_ static bool ignore_file_allow_backup(const char *filename) {
+_pure_ static bool hidden_file_allow_backup(const char *filename) {
assert(filename);
return
endswith(filename, ".dpkg-old") ||
endswith(filename, ".dpkg-new") ||
endswith(filename, ".dpkg-tmp") ||
+ endswith(filename, ".dpkg-dist") ||
+ endswith(filename, ".dpkg-bak") ||
+ endswith(filename, ".dpkg-backup") ||
+ endswith(filename, ".dpkg-remove") ||
endswith(filename, ".swp");
}
-bool ignore_file(const char *filename) {
+bool hidden_file(const char *filename) {
assert(filename);
if (endswith(filename, "~"))
return true;
- return ignore_file_allow_backup(filename);
+ return hidden_file_allow_backup(filename);
}
int fd_nonblock(int fd, bool nonblock) {
while ((de = readdir(d))) {
int fd = -1;
- if (ignore_file(de->d_name))
+ if (hidden_file(de->d_name))
continue;
if (safe_atoi(de->d_name, &fd) < 0)
bool fstype_is_network(const char *fstype) {
static const char table[] =
+ "afs\0"
"cifs\0"
"smbfs\0"
"sshfs\0"
return nulstr_contains(table, fstype);
}
-int chvt(int vt) {
- _cleanup_close_ int fd;
+int flush_fd(int fd) {
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLIN,
+ };
- fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
+ for (;;) {
+ char buf[LINE_MAX];
+ ssize_t l;
+ int r;
- if (vt < 0) {
- int tiocl[2] = {
- TIOCL_GETKMSGREDIRECT,
- 0
- };
+ r = poll(&pollfd, 1, 0);
+ if (r < 0) {
+ if (errno == EINTR)
+ continue;
- if (ioctl(fd, TIOCLINUX, tiocl) < 0)
return -errno;
- vt = tiocl[0] <= 0 ? 1 : tiocl[0];
- }
-
- if (ioctl(fd, VT_ACTIVATE, vt) < 0)
- return -errno;
+ } else if (r == 0)
+ return 0;
- return 0;
-}
+ l = read(fd, buf, sizeof(buf));
+ if (l < 0) {
-int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
- struct termios old_termios, new_termios;
- char c, line[LINE_MAX];
+ if (errno == EINTR)
+ continue;
- assert(f);
- assert(ret);
+ if (errno == EAGAIN)
+ return 0;
- if (tcgetattr(fileno(f), &old_termios) >= 0) {
- new_termios = old_termios;
+ return -errno;
+ } else if (l == 0)
+ return 0;
+ }
+}
- new_termios.c_lflag &= ~ICANON;
- new_termios.c_cc[VMIN] = 1;
- new_termios.c_cc[VTIME] = 0;
+int sigaction_many(const struct sigaction *sa, ...) {
+ va_list ap;
+ int r = 0, sig;
- if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
- size_t k;
+ va_start(ap, sa);
+ while ((sig = va_arg(ap, int)) > 0)
+ if (sigaction(sig, sa, NULL) < 0)
+ r = -errno;
+ va_end(ap);
- if (t != USEC_INFINITY) {
- if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) {
- tcsetattr(fileno(f), TCSADRAIN, &old_termios);
- return -ETIMEDOUT;
- }
- }
+ return r;
+}
- k = fread(&c, 1, 1, f);
+int ignore_signals(int sig, ...) {
+ struct sigaction sa = {
+ .sa_handler = SIG_IGN,
+ .sa_flags = SA_RESTART,
+ };
+ va_list ap;
+ int r = 0;
- tcsetattr(fileno(f), TCSADRAIN, &old_termios);
+ if (sigaction(sig, &sa, NULL) < 0)
+ r = -errno;
- if (k <= 0)
- return -EIO;
+ va_start(ap, sig);
+ while ((sig = va_arg(ap, int)) > 0)
+ if (sigaction(sig, &sa, NULL) < 0)
+ r = -errno;
+ va_end(ap);
- if (need_nl)
- *need_nl = c != '\n';
+ return r;
+}
- *ret = c;
- return 0;
- }
- }
+int default_signals(int sig, ...) {
+ struct sigaction sa = {
+ .sa_handler = SIG_DFL,
+ .sa_flags = SA_RESTART,
+ };
+ va_list ap;
+ int r = 0;
- if (t != USEC_INFINITY) {
- if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0)
- return -ETIMEDOUT;
- }
+ if (sigaction(sig, &sa, NULL) < 0)
+ r = -errno;
- errno = 0;
- if (!fgets(line, sizeof(line), f))
- return errno ? -errno : -EIO;
+ va_start(ap, sig);
+ while ((sig = va_arg(ap, int)) > 0)
+ if (sigaction(sig, &sa, NULL) < 0)
+ r = -errno;
+ va_end(ap);
- truncate_nl(line);
+ return r;
+}
- if (strlen(line) != 1)
- return -EBADMSG;
+void safe_close_pair(int p[]) {
+ assert(p);
- if (need_nl)
- *need_nl = false;
+ if (p[0] == p[1]) {
+ /* Special case pairs which use the same fd in both
+ * directions... */
+ p[0] = p[1] = safe_close(p[0]);
+ return;
+ }
- *ret = line[0];
- return 0;
+ p[0] = safe_close(p[0]);
+ p[1] = safe_close(p[1]);
}
-int ask_char(char *ret, const char *replies, const char *text, ...) {
- int r;
-
- assert(ret);
- assert(replies);
- assert(text);
-
- for (;;) {
- va_list ap;
- char c;
- bool need_nl = true;
+ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
+ uint8_t *p = buf;
+ ssize_t n = 0;
- if (on_tty())
- fputs(ANSI_HIGHLIGHT_ON, stdout);
+ assert(fd >= 0);
+ assert(buf);
- va_start(ap, text);
- vprintf(text, ap);
- va_end(ap);
+ while (nbytes > 0) {
+ ssize_t k;
- if (on_tty())
- fputs(ANSI_HIGHLIGHT_OFF, stdout);
+ k = read(fd, p, nbytes);
+ if (k < 0) {
+ if (errno == EINTR)
+ continue;
- fflush(stdout);
+ if (errno == EAGAIN && do_poll) {
- r = read_one_char(stdin, &c, USEC_INFINITY, &need_nl);
- if (r < 0) {
+ /* We knowingly ignore any return value here,
+ * and expect that any error/EOF is reported
+ * via read() */
- if (r == -EBADMSG) {
- puts("Bad input, please try again.");
+ fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
continue;
}
- putchar('\n');
- return r;
+ return n > 0 ? n : -errno;
}
- if (need_nl)
- putchar('\n');
-
- if (strchr(replies, c)) {
- *ret = c;
- return 0;
- }
+ if (k == 0)
+ return n;
- puts("Read unexpected character, please try again.");
+ p += k;
+ nbytes -= k;
+ n += k;
}
+
+ return n;
}
-int ask_string(char **ret, const char *text, ...) {
- assert(ret);
- assert(text);
+int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
+ ssize_t n;
- for (;;) {
- char line[LINE_MAX];
- va_list ap;
+ n = loop_read(fd, buf, nbytes, do_poll);
+ if (n < 0)
+ return n;
+ if ((size_t) n != nbytes)
+ return -EIO;
+ return 0;
+}
- if (on_tty())
- fputs(ANSI_HIGHLIGHT_ON, stdout);
+int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
+ const uint8_t *p = buf;
- va_start(ap, text);
- vprintf(text, ap);
- va_end(ap);
+ assert(fd >= 0);
+ assert(buf);
- if (on_tty())
- fputs(ANSI_HIGHLIGHT_OFF, stdout);
+ errno = 0;
- fflush(stdout);
+ while (nbytes > 0) {
+ ssize_t k;
- errno = 0;
- if (!fgets(line, sizeof(line), stdin))
- return errno ? -errno : -EIO;
+ k = write(fd, p, nbytes);
+ if (k < 0) {
+ if (errno == EINTR)
+ continue;
- if (!endswith(line, "\n"))
- putchar('\n');
- else {
- char *s;
+ if (errno == EAGAIN && do_poll) {
+ /* We knowingly ignore any return value here,
+ * and expect that any error/EOF is reported
+ * via write() */
- if (isempty(line))
+ fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
continue;
+ }
- truncate_nl(line);
- s = strdup(line);
- if (!s)
- return -ENOMEM;
-
- *ret = s;
- return 0;
+ return -errno;
}
- }
-}
-int reset_terminal_fd(int fd, bool switch_to_text) {
- struct termios termios;
- int r = 0;
-
- /* Set terminal to some sane defaults */
-
- assert(fd >= 0);
-
- /* We leave locked terminal attributes untouched, so that
- * Plymouth may set whatever it wants to set, and we don't
- * interfere with that. */
-
- /* Disable exclusive mode, just in case */
- ioctl(fd, TIOCNXCL);
-
- /* Switch to text mode */
- if (switch_to_text)
- ioctl(fd, KDSETMODE, KD_TEXT);
-
- /* Enable console unicode mode */
- ioctl(fd, KDSKBMODE, K_UNICODE);
+ if (k == 0) /* Can't really happen */
+ return -EIO;
- if (tcgetattr(fd, &termios) < 0) {
- r = -errno;
- goto finish;
+ p += k;
+ nbytes -= k;
}
- /* We only reset the stuff that matters to the software. How
- * hardware is set up we don't touch assuming that somebody
- * else will do that for us */
-
- termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC);
- termios.c_iflag |= ICRNL | IMAXBEL | IUTF8;
- termios.c_oflag |= ONLCR;
- termios.c_cflag |= CREAD;
- termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE;
-
- termios.c_cc[VINTR] = 03; /* ^C */
- termios.c_cc[VQUIT] = 034; /* ^\ */
- termios.c_cc[VERASE] = 0177;
- termios.c_cc[VKILL] = 025; /* ^X */
- termios.c_cc[VEOF] = 04; /* ^D */
- termios.c_cc[VSTART] = 021; /* ^Q */
- termios.c_cc[VSTOP] = 023; /* ^S */
- termios.c_cc[VSUSP] = 032; /* ^Z */
- termios.c_cc[VLNEXT] = 026; /* ^V */
- termios.c_cc[VWERASE] = 027; /* ^W */
- termios.c_cc[VREPRINT] = 022; /* ^R */
- termios.c_cc[VEOL] = 0;
- termios.c_cc[VEOL2] = 0;
-
- termios.c_cc[VTIME] = 0;
- termios.c_cc[VMIN] = 1;
-
- if (tcsetattr(fd, TCSANOW, &termios) < 0)
- r = -errno;
-
-finish:
- /* Just in case, flush all crap out */
- tcflush(fd, TCIOFLUSH);
-
- return r;
-}
-
-int reset_terminal(const char *name) {
- _cleanup_close_ int fd = -1;
-
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
-
- return reset_terminal_fd(fd, true);
+ return 0;
}
-int open_terminal(const char *name, int mode) {
- int fd, r;
- unsigned c = 0;
+int parse_size(const char *t, off_t base, off_t *size) {
- /*
- * If a TTY is in the process of being closed opening it might
- * cause EIO. This is horribly awful, but unlikely to be
- * changed in the kernel. Hence we work around this problem by
- * retrying a couple of times.
+ /* Soo, sometimes we want to parse IEC binary suffxies, and
+ * sometimes SI decimal suffixes. This function can parse
+ * both. Which one is the right way depends on the
+ * context. Wikipedia suggests that SI is customary for
+ * hardware metrics and network speeds, while IEC is
+ * customary for most data sizes used by software and volatile
+ * (RAM) memory. Hence be careful which one you pick!
*
- * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
- */
-
- assert(!(mode & O_CREAT));
+ * In either case we use just K, M, G as suffix, and not Ki,
+ * Mi, Gi or so (as IEC would suggest). That's because that's
+ * frickin' ugly. But this means you really need to make sure
+ * to document which base you are parsing when you use this
+ * call. */
- for (;;) {
- fd = open(name, mode, 0);
- if (fd >= 0)
- break;
+ struct table {
+ const char *suffix;
+ unsigned long long factor;
+ };
- if (errno != EIO)
- return -errno;
+ static const struct table iec[] = {
+ { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
+ { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
+ { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
+ { "G", 1024ULL*1024ULL*1024ULL },
+ { "M", 1024ULL*1024ULL },
+ { "K", 1024ULL },
+ { "B", 1 },
+ { "", 1 },
+ };
- /* Max 1s in total */
- if (c >= 20)
- return -errno;
+ static const struct table si[] = {
+ { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
+ { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
+ { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
+ { "G", 1000ULL*1000ULL*1000ULL },
+ { "M", 1000ULL*1000ULL },
+ { "K", 1000ULL },
+ { "B", 1 },
+ { "", 1 },
+ };
- usleep(50 * USEC_PER_MSEC);
- c++;
- }
+ const struct table *table;
+ const char *p;
+ unsigned long long r = 0;
+ unsigned n_entries, start_pos = 0;
- r = isatty(fd);
- if (r < 0) {
- safe_close(fd);
- return -errno;
- }
+ assert(t);
+ assert(base == 1000 || base == 1024);
+ assert(size);
- if (!r) {
- safe_close(fd);
- return -ENOTTY;
+ if (base == 1000) {
+ table = si;
+ n_entries = ELEMENTSOF(si);
+ } else {
+ table = iec;
+ n_entries = ELEMENTSOF(iec);
}
- return fd;
-}
-
-int flush_fd(int fd) {
- struct pollfd pollfd = {
- .fd = fd,
- .events = POLLIN,
- };
-
- for (;;) {
- char buf[LINE_MAX];
- ssize_t l;
- int r;
+ p = t;
+ do {
+ long long l;
+ unsigned long long l2;
+ double frac = 0;
+ char *e;
+ unsigned i;
- r = poll(&pollfd, 1, 0);
- if (r < 0) {
- if (errno == EINTR)
- continue;
+ errno = 0;
+ l = strtoll(p, &e, 10);
+ if (errno > 0)
return -errno;
- } else if (r == 0)
- return 0;
+ if (l < 0)
+ return -ERANGE;
- l = read(fd, buf, sizeof(buf));
- if (l < 0) {
+ if (e == p)
+ return -EINVAL;
- if (errno == EINTR)
- continue;
+ if (*e == '.') {
+ e++;
+ if (*e >= '0' && *e <= '9') {
+ char *e2;
- if (errno == EAGAIN)
- return 0;
+ /* strotoull itself would accept space/+/- */
+ l2 = strtoull(e, &e2, 10);
- return -errno;
- } else if (l == 0)
- return 0;
- }
-}
+ if (errno == ERANGE)
+ return -errno;
-int acquire_terminal(
- const char *name,
- bool fail,
- bool force,
- bool ignore_tiocstty_eperm,
- usec_t timeout) {
+ /* Ignore failure. E.g. 10.M is valid */
+ frac = l2;
+ for (; e < e2; e++)
+ frac /= 10;
+ }
+ }
- int fd = -1, notify = -1, r = 0, wd = -1;
- usec_t ts = 0;
+ e += strspn(e, WHITESPACE);
- assert(name);
+ for (i = start_pos; i < n_entries; i++)
+ if (startswith(e, table[i].suffix)) {
+ unsigned long long tmp;
+ if ((unsigned long long) l + (frac > 0) > ULLONG_MAX / table[i].factor)
+ return -ERANGE;
+ tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
+ if (tmp > ULLONG_MAX - r)
+ return -ERANGE;
- /* We use inotify to be notified when the tty is closed. We
- * create the watch before checking if we can actually acquire
- * it, so that we don't lose any event.
- *
- * Note: strictly speaking this actually watches for the
- * device being closed, it does *not* really watch whether a
- * tty loses its controlling process. However, unless some
- * rogue process uses TIOCNOTTY on /dev/tty *after* closing
- * its tty otherwise this will not become a problem. As long
- * as the administrator makes sure not configure any service
- * on the same tty as an untrusted user this should not be a
- * problem. (Which he probably should not do anyway.) */
+ r += tmp;
+ if ((unsigned long long) (off_t) r != r)
+ return -ERANGE;
- if (timeout != USEC_INFINITY)
- ts = now(CLOCK_MONOTONIC);
+ p = e + strlen(table[i].suffix);
- if (!fail && !force) {
- notify = inotify_init1(IN_CLOEXEC | (timeout != USEC_INFINITY ? IN_NONBLOCK : 0));
- if (notify < 0) {
- r = -errno;
- goto fail;
- }
+ start_pos = i + 1;
+ break;
+ }
- wd = inotify_add_watch(notify, name, IN_CLOSE);
- if (wd < 0) {
- r = -errno;
- goto fail;
- }
- }
+ if (i >= n_entries)
+ return -EINVAL;
- for (;;) {
- struct sigaction sa_old, sa_new = {
- .sa_handler = SIG_IGN,
- .sa_flags = SA_RESTART,
- };
+ } while (*p);
- if (notify >= 0) {
- r = flush_fd(notify);
- if (r < 0)
- goto fail;
- }
+ *size = r;
- /* We pass here O_NOCTTY only so that we can check the return
- * value TIOCSCTTY and have a reliable way to figure out if we
- * successfully became the controlling process of the tty */
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+ return 0;
+}
- /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
- * if we already own the tty. */
- assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
+bool is_device_path(const char *path) {
- /* First, try to get the tty */
- if (ioctl(fd, TIOCSCTTY, force) < 0)
- r = -errno;
+ /* Returns true on paths that refer to a device, either in
+ * sysfs or in /dev */
- assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
+ return
+ path_startswith(path, "/dev/") ||
+ path_startswith(path, "/sys/");
+}
- /* Sometimes it makes sense to ignore TIOCSCTTY
- * returning EPERM, i.e. when very likely we already
- * are have this controlling terminal. */
- if (r < 0 && r == -EPERM && ignore_tiocstty_eperm)
- r = 0;
+int dir_is_empty(const char *path) {
+ _cleanup_closedir_ DIR *d;
- if (r < 0 && (force || fail || r != -EPERM)) {
- goto fail;
- }
+ d = opendir(path);
+ if (!d)
+ return -errno;
- if (r >= 0)
- break;
+ for (;;) {
+ struct dirent *de;
- assert(!fail);
- assert(!force);
- assert(notify >= 0);
+ errno = 0;
+ de = readdir(d);
+ if (!de && errno != 0)
+ return -errno;
- for (;;) {
- uint8_t inotify_buffer[sizeof(struct inotify_event) + FILENAME_MAX];
- ssize_t l;
- struct inotify_event *e;
+ if (!de)
+ return 1;
- if (timeout != USEC_INFINITY) {
- usec_t n;
+ if (!hidden_file(de->d_name))
+ return 0;
+ }
+}
- n = now(CLOCK_MONOTONIC);
- if (ts + timeout < n) {
- r = -ETIMEDOUT;
- goto fail;
- }
+char* dirname_malloc(const char *path) {
+ char *d, *dir, *dir2;
- r = fd_wait_for_event(fd, POLLIN, ts + timeout - n);
- if (r < 0)
- goto fail;
+ d = strdup(path);
+ if (!d)
+ return NULL;
+ dir = dirname(d);
+ assert(dir);
- if (r == 0) {
- r = -ETIMEDOUT;
- goto fail;
- }
- }
+ if (dir != d) {
+ dir2 = strdup(dir);
+ free(d);
+ return dir2;
+ }
- l = read(notify, inotify_buffer, sizeof(inotify_buffer));
- if (l < 0) {
+ return dir;
+}
- if (errno == EINTR || errno == EAGAIN)
- continue;
+void rename_process(const char name[8]) {
+ assert(name);
- r = -errno;
- goto fail;
- }
+ /* This is a like a poor man's setproctitle(). It changes the
+ * comm field, argv[0], and also the glibc's internally used
+ * name of the process. For the first one a limit of 16 chars
+ * applies, to the second one usually one of 10 (i.e. length
+ * of "/sbin/init"), to the third one one of 7 (i.e. length of
+ * "systemd"). If you pass a longer string it will be
+ * truncated */
- e = (struct inotify_event*) inotify_buffer;
+ prctl(PR_SET_NAME, name);
- while (l > 0) {
- size_t step;
+ if (program_invocation_name)
+ strncpy(program_invocation_name, name, strlen(program_invocation_name));
- if (e->wd != wd || !(e->mask & IN_CLOSE)) {
- r = -EIO;
- goto fail;
- }
+ if (saved_argc > 0) {
+ int i;
- step = sizeof(struct inotify_event) + e->len;
- assert(step <= (size_t) l);
+ if (saved_argv[0])
+ strncpy(saved_argv[0], name, strlen(saved_argv[0]));
- e = (struct inotify_event*) ((uint8_t*) e + step);
- l -= step;
- }
+ for (i = 1; i < saved_argc; i++) {
+ if (!saved_argv[i])
+ break;
- break;
+ memzero(saved_argv[i], strlen(saved_argv[i]));
}
-
- /* We close the tty fd here since if the old session
- * ended our handle will be dead. It's important that
- * we do this after sleeping, so that we don't enter
- * an endless loop. */
- fd = safe_close(fd);
}
+}
- safe_close(notify);
+void sigset_add_many(sigset_t *ss, ...) {
+ va_list ap;
+ int sig;
- r = reset_terminal_fd(fd, true);
- if (r < 0)
- log_warning_errno(r, "Failed to reset terminal: %m");
-
- return fd;
-
-fail:
- safe_close(fd);
- safe_close(notify);
+ assert(ss);
- return r;
+ va_start(ap, ss);
+ while ((sig = va_arg(ap, int)) > 0)
+ assert_se(sigaddset(ss, sig) == 0);
+ va_end(ap);
}
-int release_terminal(void) {
- static const struct sigaction sa_new = {
- .sa_handler = SIG_IGN,
- .sa_flags = SA_RESTART,
- };
+int sigprocmask_many(int how, ...) {
+ va_list ap;
+ sigset_t ss;
+ int sig;
- _cleanup_close_ int fd = -1;
- struct sigaction sa_old;
- int r = 0;
+ assert_se(sigemptyset(&ss) == 0);
- fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC);
- if (fd < 0)
+ va_start(ap, how);
+ while ((sig = va_arg(ap, int)) > 0)
+ assert_se(sigaddset(&ss, sig) == 0);
+ va_end(ap);
+
+ if (sigprocmask(how, &ss, NULL) < 0)
return -errno;
- /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
- * by our own TIOCNOTTY */
- assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
+ return 0;
+}
+
+char* gethostname_malloc(void) {
+ struct utsname u;
- if (ioctl(fd, TIOCNOTTY) < 0)
- r = -errno;
+ assert_se(uname(&u) >= 0);
- assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
+ if (!isempty(u.nodename) && !streq(u.nodename, "(none)"))
+ return strdup(u.nodename);
- return r;
+ return strdup(u.sysname);
}
-int sigaction_many(const struct sigaction *sa, ...) {
- va_list ap;
- int r = 0, sig;
+bool hostname_is_set(void) {
+ struct utsname u;
- va_start(ap, sa);
- while ((sig = va_arg(ap, int)) > 0)
- if (sigaction(sig, sa, NULL) < 0)
- r = -errno;
- va_end(ap);
+ assert_se(uname(&u) >= 0);
- return r;
+ return !isempty(u.nodename) && !streq(u.nodename, "(none)");
}
-int ignore_signals(int sig, ...) {
- struct sigaction sa = {
- .sa_handler = SIG_IGN,
- .sa_flags = SA_RESTART,
- };
- va_list ap;
- int r = 0;
+char *lookup_uid(uid_t uid) {
+ long bufsize;
+ char *name;
+ _cleanup_free_ char *buf = NULL;
+ struct passwd pwbuf, *pw = NULL;
- if (sigaction(sig, &sa, NULL) < 0)
- r = -errno;
+ /* Shortcut things to avoid NSS lookups */
+ if (uid == 0)
+ return strdup("root");
- va_start(ap, sig);
- while ((sig = va_arg(ap, int)) > 0)
- if (sigaction(sig, &sa, NULL) < 0)
- r = -errno;
- va_end(ap);
+ bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (bufsize <= 0)
+ bufsize = 4096;
- return r;
-}
+ buf = malloc(bufsize);
+ if (!buf)
+ return NULL;
-int default_signals(int sig, ...) {
- struct sigaction sa = {
- .sa_handler = SIG_DFL,
- .sa_flags = SA_RESTART,
- };
- va_list ap;
- int r = 0;
+ if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw)
+ return strdup(pw->pw_name);
- if (sigaction(sig, &sa, NULL) < 0)
- r = -errno;
+ if (asprintf(&name, UID_FMT, uid) < 0)
+ return NULL;
- va_start(ap, sig);
- while ((sig = va_arg(ap, int)) > 0)
- if (sigaction(sig, &sa, NULL) < 0)
- r = -errno;
- va_end(ap);
+ return name;
+}
- return r;
+char* getlogname_malloc(void) {
+ uid_t uid;
+ struct stat st;
+
+ if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
+ uid = st.st_uid;
+ else
+ uid = getuid();
+
+ return lookup_uid(uid);
}
-void safe_close_pair(int p[]) {
- assert(p);
+char *getusername_malloc(void) {
+ const char *e;
- if (p[0] == p[1]) {
- /* Special case pairs which use the same fd in both
- * directions... */
- p[0] = p[1] = safe_close(p[0]);
- return;
- }
+ e = getenv("USER");
+ if (e)
+ return strdup(e);
- p[0] = safe_close(p[0]);
- p[1] = safe_close(p[1]);
+ return lookup_uid(getuid());
}
-ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
- uint8_t *p = buf;
- ssize_t n = 0;
+bool is_temporary_fs(const struct statfs *s) {
+ assert(s);
- assert(fd >= 0);
- assert(buf);
+ return F_TYPE_EQUAL(s->f_type, TMPFS_MAGIC) ||
+ F_TYPE_EQUAL(s->f_type, RAMFS_MAGIC);
+}
- while (nbytes > 0) {
- ssize_t k;
+int fd_is_temporary_fs(int fd) {
+ struct statfs s;
- k = read(fd, p, nbytes);
- if (k < 0 && errno == EINTR)
- continue;
+ if (fstatfs(fd, &s) < 0)
+ return -errno;
- if (k < 0 && errno == EAGAIN && do_poll) {
+ return is_temporary_fs(&s);
+}
- /* We knowingly ignore any return value here,
- * and expect that any error/EOF is reported
- * via read() */
+int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
+ assert(path);
- fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
- continue;
- }
+ /* Under the assumption that we are running privileged we
+ * first change the access mode and only then hand out
+ * ownership to avoid a window where access is too open. */
- if (k <= 0)
- return n > 0 ? n : (k < 0 ? -errno : 0);
+ if (mode != MODE_INVALID)
+ if (chmod(path, mode) < 0)
+ return -errno;
- p += k;
- nbytes -= k;
- n += k;
- }
+ if (uid != UID_INVALID || gid != GID_INVALID)
+ if (chown(path, uid, gid) < 0)
+ return -errno;
- return n;
+ return 0;
}
-ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
- const uint8_t *p = buf;
- ssize_t n = 0;
-
+int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) {
assert(fd >= 0);
- assert(buf);
- while (nbytes > 0) {
- ssize_t k;
+ /* Under the assumption that we are running privileged we
+ * first change the access mode and only then hand out
+ * ownership to avoid a window where access is too open. */
- k = write(fd, p, nbytes);
- if (k < 0 && errno == EINTR)
- continue;
+ if (mode != MODE_INVALID)
+ if (fchmod(fd, mode) < 0)
+ return -errno;
- if (k < 0 && errno == EAGAIN && do_poll) {
+ if (uid != UID_INVALID || gid != GID_INVALID)
+ if (fchown(fd, uid, gid) < 0)
+ return -errno;
- /* We knowingly ignore any return value here,
- * and expect that any error/EOF is reported
- * via write() */
+ return 0;
+}
- fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
- continue;
- }
+cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
+ cpu_set_t *r;
+ unsigned n = 1024;
- if (k <= 0)
- return n > 0 ? n : (k < 0 ? -errno : 0);
+ /* Allocates the cpuset in the right size */
- p += k;
- nbytes -= k;
- n += k;
- }
+ for (;;) {
+ if (!(r = CPU_ALLOC(n)))
+ return NULL;
- return n;
-}
+ if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
+ CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
-int parse_size(const char *t, off_t base, off_t *size) {
+ if (ncpus)
+ *ncpus = n;
- /* Soo, sometimes we want to parse IEC binary suffxies, and
- * sometimes SI decimal suffixes. This function can parse
- * both. Which one is the right way depends on the
- * context. Wikipedia suggests that SI is customary for
- * hardrware metrics and network speeds, while IEC is
- * customary for most data sizes used by software and volatile
- * (RAM) memory. Hence be careful which one you pick!
- *
- * In either case we use just K, M, G as suffix, and not Ki,
- * Mi, Gi or so (as IEC would suggest). That's because that's
- * frickin' ugly. But this means you really need to make sure
- * to document which base you are parsing when you use this
- * call. */
+ return r;
+ }
- struct table {
- const char *suffix;
- unsigned long long factor;
- };
+ CPU_FREE(r);
- static const struct table iec[] = {
- { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
- { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
- { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
- { "G", 1024ULL*1024ULL*1024ULL },
- { "M", 1024ULL*1024ULL },
- { "K", 1024ULL },
- { "B", 1 },
- { "", 1 },
- };
+ if (errno != EINVAL)
+ return NULL;
- static const struct table si[] = {
- { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
- { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
- { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
- { "G", 1000ULL*1000ULL*1000ULL },
- { "M", 1000ULL*1000ULL },
- { "K", 1000ULL },
- { "B", 1 },
- { "", 1 },
- };
+ n *= 2;
+ }
+}
- const struct table *table;
- const char *p;
- unsigned long long r = 0;
- unsigned n_entries, start_pos = 0;
+int files_same(const char *filea, const char *fileb) {
+ struct stat a, b;
- assert(t);
- assert(base == 1000 || base == 1024);
- assert(size);
-
- if (base == 1000) {
- table = si;
- n_entries = ELEMENTSOF(si);
- } else {
- table = iec;
- n_entries = ELEMENTSOF(iec);
- }
+ if (stat(filea, &a) < 0)
+ return -errno;
- p = t;
- do {
- long long l;
- unsigned long long l2;
- double frac = 0;
- char *e;
- unsigned i;
+ if (stat(fileb, &b) < 0)
+ return -errno;
- errno = 0;
- l = strtoll(p, &e, 10);
+ return a.st_dev == b.st_dev &&
+ a.st_ino == b.st_ino;
+}
- if (errno > 0)
- return -errno;
+int running_in_chroot(void) {
+ int ret;
- if (l < 0)
- return -ERANGE;
+ ret = files_same("/proc/1/root", "/");
+ if (ret < 0)
+ return ret;
- if (e == p)
- return -EINVAL;
+ return ret == 0;
+}
- if (*e == '.') {
- e++;
- if (*e >= '0' && *e <= '9') {
- char *e2;
+static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
+ size_t x;
+ char *r;
- /* strotoull itself would accept space/+/- */
- l2 = strtoull(e, &e2, 10);
+ assert(s);
+ assert(percent <= 100);
+ assert(new_length >= 3);
- if (errno == ERANGE)
- return -errno;
+ if (old_length <= 3 || old_length <= new_length)
+ return strndup(s, old_length);
- /* Ignore failure. E.g. 10.M is valid */
- frac = l2;
- for (; e < e2; e++)
- frac /= 10;
- }
- }
+ r = new0(char, new_length+1);
+ if (!r)
+ return NULL;
- e += strspn(e, WHITESPACE);
+ x = (new_length * percent) / 100;
- for (i = start_pos; i < n_entries; i++)
- if (startswith(e, table[i].suffix)) {
- unsigned long long tmp;
- if ((unsigned long long) l + (frac > 0) > ULLONG_MAX / table[i].factor)
- return -ERANGE;
- tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
- if (tmp > ULLONG_MAX - r)
- return -ERANGE;
+ if (x > new_length - 3)
+ x = new_length - 3;
- r += tmp;
- if ((unsigned long long) (off_t) r != r)
- return -ERANGE;
+ memcpy(r, s, x);
+ r[x] = '.';
+ r[x+1] = '.';
+ r[x+2] = '.';
+ memcpy(r + x + 3,
+ s + old_length - (new_length - x - 3),
+ new_length - x - 3);
- p = e + strlen(table[i].suffix);
+ return r;
+}
- start_pos = i + 1;
- break;
- }
+char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
+ size_t x;
+ char *e;
+ const char *i, *j;
+ unsigned k, len, len2;
- if (i >= n_entries)
- return -EINVAL;
+ assert(s);
+ assert(percent <= 100);
+ assert(new_length >= 3);
- } while (*p);
+ /* if no multibyte characters use ascii_ellipsize_mem for speed */
+ if (ascii_is_valid(s))
+ return ascii_ellipsize_mem(s, old_length, new_length, percent);
- *size = r;
+ if (old_length <= 3 || old_length <= new_length)
+ return strndup(s, old_length);
- return 0;
-}
+ x = (new_length * percent) / 100;
-int make_stdio(int fd) {
- int r, s, t;
+ if (x > new_length - 3)
+ x = new_length - 3;
- assert(fd >= 0);
+ k = 0;
+ for (i = s; k < x && i < s + old_length; i = utf8_next_char(i)) {
+ int c;
- r = dup3(fd, STDIN_FILENO, 0);
- s = dup3(fd, STDOUT_FILENO, 0);
- t = dup3(fd, STDERR_FILENO, 0);
+ c = utf8_encoded_to_unichar(i);
+ if (c < 0)
+ return NULL;
+ k += unichar_iswide(c) ? 2 : 1;
+ }
- if (fd >= 3)
- safe_close(fd);
+ if (k > x) /* last character was wide and went over quota */
+ x ++;
- if (r < 0 || s < 0 || t < 0)
- return -errno;
+ for (j = s + old_length; k < new_length && j > i; ) {
+ int c;
- /* We rely here that the new fd has O_CLOEXEC not set */
+ j = utf8_prev_char(j);
+ c = utf8_encoded_to_unichar(j);
+ if (c < 0)
+ return NULL;
+ k += unichar_iswide(c) ? 2 : 1;
+ }
+ assert(i <= j);
- return 0;
-}
+ /* we don't actually need to ellipsize */
+ if (i == j)
+ return memdup(s, old_length + 1);
-int make_null_stdio(void) {
- int null_fd;
+ /* make space for ellipsis */
+ j = utf8_next_char(j);
- null_fd = open("/dev/null", O_RDWR|O_NOCTTY);
- if (null_fd < 0)
- return -errno;
+ len = i - s;
+ len2 = s + old_length - j;
+ e = new(char, len + 3 + len2 + 1);
+ if (!e)
+ return NULL;
- return make_stdio(null_fd);
-}
+ /*
+ printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
+ old_length, new_length, x, len, len2, k);
+ */
-bool is_device_path(const char *path) {
+ memcpy(e, s, len);
+ e[len] = 0xe2; /* tri-dot ellipsis: … */
+ e[len + 1] = 0x80;
+ e[len + 2] = 0xa6;
- /* Returns true on paths that refer to a device, either in
- * sysfs or in /dev */
+ memcpy(e + len + 3, j, len2 + 1);
- return
- path_startswith(path, "/dev/") ||
- path_startswith(path, "/sys/");
+ return e;
}
-int dir_is_empty(const char *path) {
- _cleanup_closedir_ DIR *d;
+char *ellipsize(const char *s, size_t length, unsigned percent) {
+ return ellipsize_mem(s, strlen(s), length, percent);
+}
- d = opendir(path);
- if (!d)
- return -errno;
+int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) {
+ _cleanup_close_ int fd;
+ int r;
- for (;;) {
- struct dirent *de;
+ assert(path);
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0)
- return -errno;
+ if (parents)
+ mkdir_parents(path, 0755);
- if (!de)
- return 1;
+ fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode > 0 ? mode : 0644);
+ if (fd < 0)
+ return -errno;
- if (!ignore_file(de->d_name))
- return 0;
+ if (mode > 0) {
+ r = fchmod(fd, mode);
+ if (r < 0)
+ return -errno;
}
-}
-char* dirname_malloc(const char *path) {
- char *d, *dir, *dir2;
+ if (uid != UID_INVALID || gid != GID_INVALID) {
+ r = fchown(fd, uid, gid);
+ if (r < 0)
+ return -errno;
+ }
- d = strdup(path);
- if (!d)
- return NULL;
- dir = dirname(d);
- assert(dir);
+ if (stamp != USEC_INFINITY) {
+ struct timespec ts[2];
- if (dir != d) {
- dir2 = strdup(dir);
- free(d);
- return dir2;
- }
+ timespec_store(&ts[0], stamp);
+ ts[1] = ts[0];
+ r = futimens(fd, ts);
+ } else
+ r = futimens(fd, NULL);
+ if (r < 0)
+ return -errno;
- return dir;
+ return 0;
}
-int dev_urandom(void *p, size_t n) {
- static int have_syscall = -1;
- int r, fd;
- ssize_t k;
-
- /* Gathers some randomness from the kernel. This call will
- * never block, and will always return some data from the
- * kernel, regardless if the random pool is fully initialized
- * or not. It thus makes no guarantee for the quality of the
- * returned entropy, but is good enough for or usual usecases
- * of seeding the hash functions for hashtable */
-
- /* Use the getrandom() syscall unless we know we don't have
- * it, or when the requested size is too large for it. */
- if (have_syscall != 0 || (size_t) (int) n != n) {
- r = getrandom(p, n, GRND_NONBLOCK);
- if (r == (int) n) {
- have_syscall = true;
- return 0;
- }
+int touch(const char *path) {
+ return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, 0);
+}
- if (r < 0) {
- if (errno == ENOSYS)
- /* we lack the syscall, continue with
- * reading from /dev/urandom */
- have_syscall = false;
- else if (errno == EAGAIN)
- /* not enough entropy for now. Let's
- * remember to use the syscall the
- * next time, again, but also read
- * from /dev/urandom for now, which
- * doesn't care about the current
- * amount of entropy. */
- have_syscall = true;
- else
- return -errno;
- } else
- /* too short read? */
- return -EIO;
- }
+static char *unquote(const char *s, const char* quotes) {
+ size_t l;
+ assert(s);
- fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0)
- return errno == ENOENT ? -ENOSYS : -errno;
+ /* This is rather stupid, simply removes the heading and
+ * trailing quotes if there is one. Doesn't care about
+ * escaping or anything.
+ *
+ * DON'T USE THIS FOR NEW CODE ANYMORE!*/
- k = loop_read(fd, p, n, true);
- safe_close(fd);
+ l = strlen(s);
+ if (l < 2)
+ return strdup(s);
- if (k < 0)
- return (int) k;
- if ((size_t) k != n)
- return -EIO;
+ if (strchr(quotes, s[0]) && s[l-1] == s[0])
+ return strndup(s+1, l-2);
- return 0;
+ return strdup(s);
}
-void initialize_srand(void) {
- static bool srand_called = false;
- unsigned x;
-#ifdef HAVE_SYS_AUXV_H
- void *auxv;
-#endif
+noreturn void freeze(void) {
- if (srand_called)
- return;
+ /* Make sure nobody waits for us on a socket anymore */
+ close_all_fds(NULL, 0);
- x = 0;
+ sync();
-#ifdef HAVE_SYS_AUXV_H
- /* The kernel provides us with a bit of entropy in auxv, so
- * let's try to make use of that to seed the pseudo-random
- * generator. It's better than nothing... */
+ for (;;)
+ pause();
+}
- auxv = (void*) getauxval(AT_RANDOM);
- if (auxv)
- x ^= *(unsigned*) auxv;
-#endif
+bool null_or_empty(struct stat *st) {
+ assert(st);
- x ^= (unsigned) now(CLOCK_REALTIME);
- x ^= (unsigned) gettid();
+ if (S_ISREG(st->st_mode) && st->st_size <= 0)
+ return true;
- srand(x);
- srand_called = true;
-}
+ if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
+ return true;
-void random_bytes(void *p, size_t n) {
- uint8_t *q;
- int r;
+ return false;
+}
- r = dev_urandom(p, n);
- if (r >= 0)
- return;
+int null_or_empty_path(const char *fn) {
+ struct stat st;
- /* If some idiot made /dev/urandom unavailable to us, he'll
- * get a PRNG instead. */
+ assert(fn);
- initialize_srand();
+ if (stat(fn, &st) < 0)
+ return -errno;
- for (q = p; q < (uint8_t*) p + n; q ++)
- *q = rand();
+ return null_or_empty(&st);
}
-void rename_process(const char name[8]) {
- assert(name);
+int null_or_empty_fd(int fd) {
+ struct stat st;
- /* This is a like a poor man's setproctitle(). It changes the
- * comm field, argv[0], and also the glibc's internally used
- * name of the process. For the first one a limit of 16 chars
- * applies, to the second one usually one of 10 (i.e. length
- * of "/sbin/init"), to the third one one of 7 (i.e. length of
- * "systemd"). If you pass a longer string it will be
- * truncated */
+ assert(fd >= 0);
- prctl(PR_SET_NAME, name);
+ if (fstat(fd, &st) < 0)
+ return -errno;
- if (program_invocation_name)
- strncpy(program_invocation_name, name, strlen(program_invocation_name));
+ return null_or_empty(&st);
+}
- if (saved_argc > 0) {
- int i;
+DIR *xopendirat(int fd, const char *name, int flags) {
+ int nfd;
+ DIR *d;
- if (saved_argv[0])
- strncpy(saved_argv[0], name, strlen(saved_argv[0]));
+ assert(!(flags & O_CREAT));
- for (i = 1; i < saved_argc; i++) {
- if (!saved_argv[i])
- break;
+ nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
+ if (nfd < 0)
+ return NULL;
- memzero(saved_argv[i], strlen(saved_argv[i]));
- }
+ d = fdopendir(nfd);
+ if (!d) {
+ safe_close(nfd);
+ return NULL;
}
+
+ return d;
}
-void sigset_add_many(sigset_t *ss, ...) {
- va_list ap;
- int sig;
+int signal_from_string_try_harder(const char *s) {
+ int signo;
+ assert(s);
- assert(ss);
+ signo = signal_from_string(s);
+ if (signo <= 0)
+ if (startswith(s, "SIG"))
+ return signal_from_string(s+3);
- va_start(ap, ss);
- while ((sig = va_arg(ap, int)) > 0)
- assert_se(sigaddset(ss, sig) == 0);
- va_end(ap);
+ return signo;
}
-int sigprocmask_many(int how, ...) {
- va_list ap;
- sigset_t ss;
- int sig;
+static char *tag_to_udev_node(const char *tagvalue, const char *by) {
+ _cleanup_free_ char *t = NULL, *u = NULL;
+ size_t enc_len;
- assert_se(sigemptyset(&ss) == 0);
+ u = unquote(tagvalue, QUOTES);
+ if (!u)
+ return NULL;
- va_start(ap, how);
- while ((sig = va_arg(ap, int)) > 0)
- assert_se(sigaddset(&ss, sig) == 0);
- va_end(ap);
+ enc_len = strlen(u) * 4 + 1;
+ t = new(char, enc_len);
+ if (!t)
+ return NULL;
- if (sigprocmask(how, &ss, NULL) < 0)
- return -errno;
+ if (encode_devnode_name(u, t, enc_len) < 0)
+ return NULL;
- return 0;
+ return strjoin("/dev/disk/by-", by, "/", t, NULL);
}
-char* gethostname_malloc(void) {
- struct utsname u;
+char *fstab_node_to_udev_node(const char *p) {
+ assert(p);
- assert_se(uname(&u) >= 0);
+ if (startswith(p, "LABEL="))
+ return tag_to_udev_node(p+6, "label");
- if (!isempty(u.nodename) && !streq(u.nodename, "(none)"))
- return strdup(u.nodename);
+ if (startswith(p, "UUID="))
+ return tag_to_udev_node(p+5, "uuid");
- return strdup(u.sysname);
+ if (startswith(p, "PARTUUID="))
+ return tag_to_udev_node(p+9, "partuuid");
+
+ if (startswith(p, "PARTLABEL="))
+ return tag_to_udev_node(p+10, "partlabel");
+
+ return strdup(p);
}
-bool hostname_is_set(void) {
- struct utsname u;
+bool dirent_is_file(const struct dirent *de) {
+ assert(de);
- assert_se(uname(&u) >= 0);
+ if (hidden_file(de->d_name))
+ return false;
- return !isempty(u.nodename) && !streq(u.nodename, "(none)");
+ if (de->d_type != DT_REG &&
+ de->d_type != DT_LNK &&
+ de->d_type != DT_UNKNOWN)
+ return false;
+
+ return true;
}
-char *lookup_uid(uid_t uid) {
- long bufsize;
- char *name;
- _cleanup_free_ char *buf = NULL;
- struct passwd pwbuf, *pw = NULL;
+bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
+ assert(de);
- /* Shortcut things to avoid NSS lookups */
- if (uid == 0)
- return strdup("root");
+ if (de->d_type != DT_REG &&
+ de->d_type != DT_LNK &&
+ de->d_type != DT_UNKNOWN)
+ return false;
- bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
- if (bufsize <= 0)
- bufsize = 4096;
+ if (hidden_file_allow_backup(de->d_name))
+ return false;
- buf = malloc(bufsize);
- if (!buf)
- return NULL;
+ return endswith(de->d_name, suffix);
+}
- if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw)
- return strdup(pw->pw_name);
+static int do_execute(char **directories, usec_t timeout, char *argv[]) {
+ _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
+ _cleanup_set_free_free_ Set *seen = NULL;
+ char **directory;
- if (asprintf(&name, UID_FMT, uid) < 0)
- return NULL;
+ /* We fork this all off from a child process so that we can
+ * somewhat cleanly make use of SIGALRM to set a time limit */
- return name;
-}
+ reset_all_signal_handlers();
+ reset_signal_mask();
-char* getlogname_malloc(void) {
- uid_t uid;
- struct stat st;
+ assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
- if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
- uid = st.st_uid;
- else
- uid = getuid();
+ pids = hashmap_new(NULL);
+ if (!pids)
+ return log_oom();
- return lookup_uid(uid);
-}
+ seen = set_new(&string_hash_ops);
+ if (!seen)
+ return log_oom();
-char *getusername_malloc(void) {
- const char *e;
+ STRV_FOREACH(directory, directories) {
+ _cleanup_closedir_ DIR *d;
+ struct dirent *de;
- e = getenv("USER");
- if (e)
- return strdup(e);
+ d = opendir(*directory);
+ if (!d) {
+ if (errno == ENOENT)
+ continue;
- return lookup_uid(getuid());
-}
+ return log_error_errno(errno, "Failed to open directory %s: %m", *directory);
+ }
-int getttyname_malloc(int fd, char **r) {
- char path[PATH_MAX], *c;
- int k;
+ FOREACH_DIRENT(de, d, break) {
+ _cleanup_free_ char *path = NULL;
+ pid_t pid;
+ int r;
- assert(r);
+ if (!dirent_is_file(de))
+ continue;
- k = ttyname_r(fd, path, sizeof(path));
- if (k > 0)
- return -k;
+ if (set_contains(seen, de->d_name)) {
+ log_debug("%1$s/%2$s skipped (%2$s was already seen).", *directory, de->d_name);
+ continue;
+ }
- char_array_0(path);
+ r = set_put_strdup(seen, de->d_name);
+ if (r < 0)
+ return log_oom();
- c = strdup(startswith(path, "/dev/") ? path + 5 : path);
- if (!c)
- return -ENOMEM;
+ path = strjoin(*directory, "/", de->d_name, NULL);
+ if (!path)
+ return log_oom();
- *r = c;
- return 0;
-}
+ if (null_or_empty_path(path)) {
+ log_debug("%s is empty (a mask).", path);
+ continue;
+ }
-int getttyname_harder(int fd, char **r) {
- int k;
- char *s;
+ pid = fork();
+ if (pid < 0) {
+ log_error_errno(errno, "Failed to fork: %m");
+ continue;
+ } else if (pid == 0) {
+ char *_argv[2];
- k = getttyname_malloc(fd, &s);
- if (k < 0)
- return k;
+ assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
- if (streq(s, "tty")) {
- free(s);
- return get_ctty(0, NULL, r);
- }
+ if (!argv) {
+ _argv[0] = path;
+ _argv[1] = NULL;
+ argv = _argv;
+ } else
+ argv[0] = path;
- *r = s;
- return 0;
-}
+ execv(path, argv);
+ return log_error_errno(errno, "Failed to execute %s: %m", path);
+ }
-int get_ctty_devnr(pid_t pid, dev_t *d) {
- int r;
- _cleanup_free_ char *line = NULL;
- const char *p;
- unsigned long ttynr;
+ log_debug("Spawned %s as " PID_FMT ".", path, pid);
- assert(pid >= 0);
+ r = hashmap_put(pids, UINT_TO_PTR(pid), path);
+ if (r < 0)
+ return log_oom();
+ path = NULL;
+ }
+ }
- p = procfs_file_alloca(pid, "stat");
- r = read_one_line_file(p, &line);
- if (r < 0)
- return r;
+ /* Abort execution of this process after the timout. We simply
+ * rely on SIGALRM as default action terminating the process,
+ * and turn on alarm(). */
- p = strrchr(line, ')');
- if (!p)
- return -EIO;
+ if (timeout != USEC_INFINITY)
+ alarm((timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
- p++;
+ while (!hashmap_isempty(pids)) {
+ _cleanup_free_ char *path = NULL;
+ pid_t pid;
- if (sscanf(p, " "
- "%*c " /* state */
- "%*d " /* ppid */
- "%*d " /* pgrp */
- "%*d " /* session */
- "%lu ", /* ttynr */
- &ttynr) != 1)
- return -EIO;
+ pid = PTR_TO_UINT(hashmap_first_key(pids));
+ assert(pid > 0);
- if (major(ttynr) == 0 && minor(ttynr) == 0)
- return -ENOENT;
+ path = hashmap_remove(pids, UINT_TO_PTR(pid));
+ assert(path);
- if (d)
- *d = (dev_t) ttynr;
+ wait_for_terminate_and_warn(path, pid, true);
+ }
return 0;
}
-int get_ctty(pid_t pid, dev_t *_devnr, char **r) {
- char fn[sizeof("/dev/char/")-1 + 2*DECIMAL_STR_MAX(unsigned) + 1 + 1], *b = NULL;
- _cleanup_free_ char *s = NULL;
- const char *p;
- dev_t devnr;
- int k;
+void execute_directories(const char* const* directories, usec_t timeout, char *argv[]) {
+ pid_t executor_pid;
+ int r;
+ char *name;
+ char **dirs = (char**) directories;
- assert(r);
+ assert(!strv_isempty(dirs));
- k = get_ctty_devnr(pid, &devnr);
- if (k < 0)
- return k;
+ name = basename(dirs[0]);
+ assert(!isempty(name));
- sprintf(fn, "/dev/char/%u:%u", major(devnr), minor(devnr));
+ /* Executes all binaries in the directories in parallel and waits
+ * for them to finish. Optionally a timeout is applied. If a file
+ * with the same name exists in more than one directory, the
+ * earliest one wins. */
- k = readlink_malloc(fn, &s);
- if (k < 0) {
+ executor_pid = fork();
+ if (executor_pid < 0) {
+ log_error_errno(errno, "Failed to fork: %m");
+ return;
- if (k != -ENOENT)
- return k;
+ } else if (executor_pid == 0) {
+ r = do_execute(dirs, timeout, argv);
+ _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
+ }
- /* This is an ugly hack */
- if (major(devnr) == 136) {
- asprintf(&b, "pts/%u", minor(devnr));
- goto finish;
- }
+ wait_for_terminate_and_warn(name, executor_pid, true);
+}
+
+bool nulstr_contains(const char*nulstr, const char *needle) {
+ const char *i;
- /* Probably something like the ptys which have no
- * symlink in /dev/char. Let's return something
- * vaguely useful. */
+ if (!nulstr)
+ return false;
- b = strdup(fn + 5);
- goto finish;
- }
+ NULSTR_FOREACH(i, nulstr)
+ if (streq(i, needle))
+ return true;
- if (startswith(s, "/dev/"))
- p = s + 5;
- else if (startswith(s, "../"))
- p = s + 3;
- else
- p = s;
+ return false;
+}
- b = strdup(p);
+bool plymouth_running(void) {
+ return access("/run/plymouth/pid", F_OK) >= 0;
+}
-finish:
- if (!b)
- return -ENOMEM;
+char* strshorten(char *s, size_t l) {
+ assert(s);
- *r = b;
- if (_devnr)
- *_devnr = devnr;
+ if (l < strlen(s))
+ s[l] = 0;
- return 0;
+ return s;
}
-int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) {
- _cleanup_closedir_ DIR *d = NULL;
- int ret = 0;
+static bool hostname_valid_char(char c) {
+ return
+ (c >= 'a' && c <= 'z') ||
+ (c >= 'A' && c <= 'Z') ||
+ (c >= '0' && c <= '9') ||
+ c == '-' ||
+ c == '_' ||
+ c == '.';
+}
- assert(fd >= 0);
+bool hostname_is_valid(const char *s) {
+ const char *p;
+ bool dot;
- /* This returns the first error we run into, but nevertheless
- * tries to go on. This closes the passed fd. */
+ if (isempty(s))
+ return false;
- d = fdopendir(fd);
- if (!d) {
- safe_close(fd);
+ /* Doesn't accept empty hostnames, hostnames with trailing or
+ * leading dots, and hostnames with multiple dots in a
+ * sequence. Also ensures that the length stays below
+ * HOST_NAME_MAX. */
- return errno == ENOENT ? 0 : -errno;
- }
+ for (p = s, dot = true; *p; p++) {
+ if (*p == '.') {
+ if (dot)
+ return false;
- for (;;) {
- struct dirent *de;
- bool is_dir, keep_around;
- struct stat st;
- int r;
+ dot = true;
+ } else {
+ if (!hostname_valid_char(*p))
+ return false;
- errno = 0;
- de = readdir(d);
- if (!de) {
- if (errno != 0 && ret == 0)
- ret = -errno;
- return ret;
+ dot = false;
}
+ }
- if (streq(de->d_name, ".") || streq(de->d_name, ".."))
- continue;
+ if (dot)
+ return false;
- if (de->d_type == DT_UNKNOWN ||
- honour_sticky ||
- (de->d_type == DT_DIR && root_dev)) {
- if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
- if (ret == 0 && errno != ENOENT)
- ret = -errno;
- continue;
- }
+ if (p-s > HOST_NAME_MAX)
+ return false;
- is_dir = S_ISDIR(st.st_mode);
- keep_around =
- honour_sticky &&
- (st.st_uid == 0 || st.st_uid == getuid()) &&
- (st.st_mode & S_ISVTX);
- } else {
- is_dir = de->d_type == DT_DIR;
- keep_around = false;
- }
+ return true;
+}
- if (is_dir) {
- int subdir_fd;
+char* hostname_cleanup(char *s, bool lowercase) {
+ char *p, *d;
+ bool dot;
- /* if root_dev is set, remove subdirectories only, if device is same as dir */
- if (root_dev && st.st_dev != root_dev->st_dev)
+ for (p = s, d = s, dot = true; *p; p++) {
+ if (*p == '.') {
+ if (dot)
continue;
- subdir_fd = openat(fd, de->d_name,
- O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
- if (subdir_fd < 0) {
- if (ret == 0 && errno != ENOENT)
- ret = -errno;
- continue;
- }
+ *(d++) = '.';
+ dot = true;
+ } else if (hostname_valid_char(*p)) {
+ *(d++) = lowercase ? tolower(*p) : *p;
+ dot = false;
+ }
- r = rm_rf_children_dangerous(subdir_fd, only_dirs, honour_sticky, root_dev);
- if (r < 0 && ret == 0)
- ret = r;
+ }
- if (!keep_around)
- if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
- if (ret == 0 && errno != ENOENT)
- ret = -errno;
- }
+ if (dot && d > s)
+ d[-1] = 0;
+ else
+ *d = 0;
- } else if (!only_dirs && !keep_around) {
+ strshorten(s, HOST_NAME_MAX);
- if (unlinkat(fd, de->d_name, 0) < 0) {
- if (ret == 0 && errno != ENOENT)
- ret = -errno;
- }
- }
- }
+ return s;
}
-_pure_ static int is_temporary_fs(struct statfs *s) {
- assert(s);
+bool machine_name_is_valid(const char *s) {
- return F_TYPE_EQUAL(s->f_type, TMPFS_MAGIC) ||
- F_TYPE_EQUAL(s->f_type, RAMFS_MAGIC);
-}
+ if (!hostname_is_valid(s))
+ return false;
-int is_fd_on_temporary_fs(int fd) {
- struct statfs s;
+ /* Machine names should be useful hostnames, but also be
+ * useful in unit names, hence we enforce a stricter length
+ * limitation. */
- if (fstatfs(fd, &s) < 0)
- return -errno;
+ if (strlen(s) > 64)
+ return false;
- return is_temporary_fs(&s);
+ return true;
}
-int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) {
- struct statfs s;
+int pipe_eof(int fd) {
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLIN|POLLHUP,
+ };
- assert(fd >= 0);
+ int r;
- if (fstatfs(fd, &s) < 0) {
- safe_close(fd);
+ r = poll(&pollfd, 1, 0);
+ if (r < 0)
return -errno;
- }
- /* We refuse to clean disk file systems with this call. This
- * is extra paranoia just to be sure we never ever remove
- * non-state data */
- if (!is_temporary_fs(&s)) {
- log_error("Attempted to remove disk file system, and we can't allow that.");
- safe_close(fd);
- return -EPERM;
- }
+ if (r == 0)
+ return 0;
- return rm_rf_children_dangerous(fd, only_dirs, honour_sticky, root_dev);
+ return pollfd.revents & POLLHUP;
}
-static int file_is_priv_sticky(const char *p) {
- struct stat st;
+int fd_wait_for_event(int fd, int event, usec_t t) {
- assert(p);
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = event,
+ };
- if (lstat(p, &st) < 0)
+ struct timespec ts;
+ int r;
+
+ r = ppoll(&pollfd, 1, t == USEC_INFINITY ? NULL : timespec_store(&ts, t), NULL);
+ if (r < 0)
return -errno;
- return
- (st.st_uid == 0 || st.st_uid == getuid()) &&
- (st.st_mode & S_ISVTX);
+ if (r == 0)
+ return 0;
+
+ return pollfd.revents;
}
-static int rm_rf_internal(const char *path, bool only_dirs, bool delete_root, bool honour_sticky, bool dangerous) {
- int fd, r;
- struct statfs s;
+int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
+ FILE *f;
+ char *t;
+ int r, fd;
assert(path);
+ assert(_f);
+ assert(_temp_path);
- /* We refuse to clean the root file system with this
- * call. This is extra paranoia to never cause a really
- * seriously broken system. */
- if (path_equal(path, "/")) {
- log_error("Attempted to remove entire root file system, and we can't allow that.");
- return -EPERM;
- }
+ r = tempfn_xxxxxx(path, &t);
+ if (r < 0)
+ return r;
- fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
+ fd = mkostemp_safe(t, O_WRONLY|O_CLOEXEC);
if (fd < 0) {
+ free(t);
+ return -errno;
+ }
- if (errno != ENOTDIR)
- return -errno;
+ f = fdopen(fd, "we");
+ if (!f) {
+ unlink(t);
+ free(t);
+ return -errno;
+ }
- if (!dangerous) {
- if (statfs(path, &s) < 0)
- return -errno;
+ *_f = f;
+ *_temp_path = t;
- if (!is_temporary_fs(&s)) {
- log_error("Attempted to remove disk file system, and we can't allow that.");
- return -EPERM;
- }
- }
+ return 0;
+}
- if (delete_root && !only_dirs)
- if (unlink(path) < 0 && errno != ENOENT)
- return -errno;
+int symlink_atomic(const char *from, const char *to) {
+ _cleanup_free_ char *t = NULL;
+ int r;
- return 0;
- }
+ assert(from);
+ assert(to);
- if (!dangerous) {
- if (fstatfs(fd, &s) < 0) {
- safe_close(fd);
- return -errno;
- }
+ r = tempfn_random(to, &t);
+ if (r < 0)
+ return r;
- if (!is_temporary_fs(&s)) {
- log_error("Attempted to remove disk file system, and we can't allow that.");
- safe_close(fd);
- return -EPERM;
- }
+ if (symlink(from, t) < 0)
+ return -errno;
+
+ if (rename(t, to) < 0) {
+ unlink_noerrno(t);
+ return -errno;
}
- r = rm_rf_children_dangerous(fd, only_dirs, honour_sticky, NULL);
- if (delete_root) {
+ return 0;
+}
+
+int symlink_idempotent(const char *from, const char *to) {
+ _cleanup_free_ char *p = NULL;
+ int r;
+
+ assert(from);
+ assert(to);
+
+ if (symlink(from, to) < 0) {
+ if (errno != EEXIST)
+ return -errno;
- if (honour_sticky && file_is_priv_sticky(path) > 0)
+ r = readlink_malloc(to, &p);
+ if (r < 0)
return r;
- if (rmdir(path) < 0 && errno != ENOENT) {
- if (r == 0)
- r = -errno;
- }
+ if (!streq(p, from))
+ return -EINVAL;
}
- return r;
-}
-
-int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) {
- return rm_rf_internal(path, only_dirs, delete_root, honour_sticky, false);
+ return 0;
}
-int rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) {
- return rm_rf_internal(path, only_dirs, delete_root, honour_sticky, true);
-}
+int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
+ _cleanup_free_ char *t = NULL;
+ int r;
-int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
assert(path);
- /* Under the assumption that we are running privileged we
- * first change the access mode and only then hand out
- * ownership to avoid a window where access is too open. */
+ r = tempfn_random(path, &t);
+ if (r < 0)
+ return r;
- if (mode != MODE_INVALID)
- if (chmod(path, mode) < 0)
- return -errno;
+ if (mknod(t, mode, dev) < 0)
+ return -errno;
- if (uid != UID_INVALID || gid != GID_INVALID)
- if (chown(path, uid, gid) < 0)
- return -errno;
+ if (rename(t, path) < 0) {
+ unlink_noerrno(t);
+ return -errno;
+ }
return 0;
}
-int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) {
- assert(fd >= 0);
+int mkfifo_atomic(const char *path, mode_t mode) {
+ _cleanup_free_ char *t = NULL;
+ int r;
- /* Under the assumption that we are running privileged we
- * first change the access mode and only then hand out
- * ownership to avoid a window where access is too open. */
+ assert(path);
- if (mode != MODE_INVALID)
- if (fchmod(fd, mode) < 0)
- return -errno;
+ r = tempfn_random(path, &t);
+ if (r < 0)
+ return r;
- if (uid != UID_INVALID || gid != GID_INVALID)
- if (fchown(fd, uid, gid) < 0)
- return -errno;
+ if (mkfifo(t, mode) < 0)
+ return -errno;
+
+ if (rename(t, path) < 0) {
+ unlink_noerrno(t);
+ return -errno;
+ }
return 0;
}
-cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
- cpu_set_t *r;
- unsigned n = 1024;
+bool display_is_local(const char *display) {
+ assert(display);
- /* Allocates the cpuset in the right size */
+ return
+ display[0] == ':' &&
+ display[1] >= '0' &&
+ display[1] <= '9';
+}
- for (;;) {
- if (!(r = CPU_ALLOC(n)))
- return NULL;
+int socket_from_display(const char *display, char **path) {
+ size_t k;
+ char *f, *c;
- if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), r) >= 0) {
- CPU_ZERO_S(CPU_ALLOC_SIZE(n), r);
+ assert(display);
+ assert(path);
- if (ncpus)
- *ncpus = n;
+ if (!display_is_local(display))
+ return -EINVAL;
- return r;
- }
+ k = strspn(display+1, "0123456789");
- CPU_FREE(r);
+ f = new(char, strlen("/tmp/.X11-unix/X") + k + 1);
+ if (!f)
+ return -ENOMEM;
- if (errno != EINVAL)
- return NULL;
+ c = stpcpy(f, "/tmp/.X11-unix/X");
+ memcpy(c, display+1, k);
+ c[k] = 0;
- n *= 2;
- }
+ *path = f;
+
+ return 0;
}
-int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) {
- static const char status_indent[] = " "; /* "[" STATUS "] " */
- _cleanup_free_ char *s = NULL;
- _cleanup_close_ int fd = -1;
- struct iovec iovec[6] = {};
- int n = 0;
- static bool prev_ephemeral;
+int get_user_creds(
+ const char **username,
+ uid_t *uid, gid_t *gid,
+ const char **home,
+ const char **shell) {
- assert(format);
+ struct passwd *p;
+ uid_t u;
- /* This is independent of logging, as status messages are
- * optional and go exclusively to the console. */
+ assert(username);
+ assert(*username);
- if (vasprintf(&s, format, ap) < 0)
- return log_oom();
+ /* We enforce some special rules for uid=0: in order to avoid
+ * NSS lookups for root we hardcode its data. */
- fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+ if (streq(*username, "root") || streq(*username, "0")) {
+ *username = "root";
- if (ellipse) {
- char *e;
- size_t emax, sl;
- int c;
+ if (uid)
+ *uid = 0;
- c = fd_columns(fd);
- if (c <= 0)
- c = 80;
+ if (gid)
+ *gid = 0;
- sl = status ? sizeof(status_indent)-1 : 0;
+ if (home)
+ *home = "/root";
- emax = c - sl - 1;
- if (emax < 3)
- emax = 3;
+ if (shell)
+ *shell = "/bin/sh";
- e = ellipsize(s, emax, 50);
- if (e) {
- free(s);
- s = e;
- }
+ return 0;
}
- if (prev_ephemeral)
- IOVEC_SET_STRING(iovec[n++], "\r" ANSI_ERASE_TO_END_OF_LINE);
- prev_ephemeral = ephemeral;
+ if (parse_uid(*username, &u) >= 0) {
+ errno = 0;
+ p = getpwuid(u);
- if (status) {
- if (!isempty(status)) {
- IOVEC_SET_STRING(iovec[n++], "[");
- IOVEC_SET_STRING(iovec[n++], status);
- IOVEC_SET_STRING(iovec[n++], "] ");
- } else
- IOVEC_SET_STRING(iovec[n++], status_indent);
+ /* If there are multiple users with the same id, make
+ * sure to leave $USER to the configured value instead
+ * of the first occurrence in the database. However if
+ * the uid was configured by a numeric uid, then let's
+ * pick the real username from /etc/passwd. */
+ if (p)
+ *username = p->pw_name;
+ } else {
+ errno = 0;
+ p = getpwnam(*username);
}
- IOVEC_SET_STRING(iovec[n++], s);
- if (!ephemeral)
- IOVEC_SET_STRING(iovec[n++], "\n");
-
- if (writev(fd, iovec, n) < 0)
- return -errno;
+ if (!p)
+ return errno > 0 ? -errno : -ESRCH;
- return 0;
-}
+ if (uid)
+ *uid = p->pw_uid;
-int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) {
- va_list ap;
- int r;
+ if (gid)
+ *gid = p->pw_gid;
- assert(format);
+ if (home)
+ *home = p->pw_dir;
- va_start(ap, format);
- r = status_vprintf(status, ellipse, ephemeral, format, ap);
- va_end(ap);
+ if (shell)
+ *shell = p->pw_shell;
- return r;
+ return 0;
}
-char *replace_env(const char *format, char **env) {
- enum {
- WORD,
- CURLY,
- VARIABLE
- } state = WORD;
-
- const char *e, *word = format;
- char *r = NULL, *k;
+char* uid_to_name(uid_t uid) {
+ struct passwd *p;
+ char *r;
- assert(format);
+ if (uid == 0)
+ return strdup("root");
- for (e = format; *e; e ++) {
+ p = getpwuid(uid);
+ if (p)
+ return strdup(p->pw_name);
- switch (state) {
+ if (asprintf(&r, UID_FMT, uid) < 0)
+ return NULL;
- case WORD:
- if (*e == '$')
- state = CURLY;
- break;
+ return r;
+}
- case CURLY:
- if (*e == '{') {
- k = strnappend(r, word, e-word-1);
- if (!k)
- goto fail;
+char* gid_to_name(gid_t gid) {
+ struct group *p;
+ char *r;
- free(r);
- r = k;
+ if (gid == 0)
+ return strdup("root");
- word = e-1;
- state = VARIABLE;
+ p = getgrgid(gid);
+ if (p)
+ return strdup(p->gr_name);
- } else if (*e == '$') {
- k = strnappend(r, word, e-word);
- if (!k)
- goto fail;
+ if (asprintf(&r, GID_FMT, gid) < 0)
+ return NULL;
- free(r);
- r = k;
+ return r;
+}
- word = e+1;
- state = WORD;
- } else
- state = WORD;
- break;
+int get_group_creds(const char **groupname, gid_t *gid) {
+ struct group *g;
+ gid_t id;
- case VARIABLE:
- if (*e == '}') {
- const char *t;
+ assert(groupname);
- t = strempty(strv_env_get_n(env, word+2, e-word-2));
+ /* We enforce some special rules for gid=0: in order to avoid
+ * NSS lookups for root we hardcode its data. */
- k = strappend(r, t);
- if (!k)
- goto fail;
+ if (streq(*groupname, "root") || streq(*groupname, "0")) {
+ *groupname = "root";
- free(r);
- r = k;
+ if (gid)
+ *gid = 0;
- word = e+1;
- state = WORD;
- }
- break;
- }
+ return 0;
}
- k = strnappend(r, word, e-word);
- if (!k)
- goto fail;
+ if (parse_gid(*groupname, &id) >= 0) {
+ errno = 0;
+ g = getgrgid(id);
- free(r);
- return k;
+ if (g)
+ *groupname = g->gr_name;
+ } else {
+ errno = 0;
+ g = getgrnam(*groupname);
+ }
-fail:
- free(r);
- return NULL;
-}
+ if (!g)
+ return errno > 0 ? -errno : -ESRCH;
-char **replace_env_argv(char **argv, char **env) {
- char **ret, **i;
- unsigned k = 0, l = 0;
+ if (gid)
+ *gid = g->gr_gid;
- l = strv_length(argv);
+ return 0;
+}
- ret = new(char*, l+1);
- if (!ret)
- return NULL;
+int in_gid(gid_t gid) {
+ gid_t *gids;
+ int ngroups_max, r, i;
- STRV_FOREACH(i, argv) {
+ if (getgid() == gid)
+ return 1;
- /* 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 (getegid() == gid)
+ return 1;
- e = strv_env_get(env, *i+1);
- if (e) {
- int r;
+ ngroups_max = sysconf(_SC_NGROUPS_MAX);
+ assert(ngroups_max > 0);
- r = strv_split_quoted(&m, e, true);
- if (r < 0) {
- ret[k] = NULL;
- strv_free(ret);
- return NULL;
- }
- } else
- m = NULL;
+ gids = alloca(sizeof(gid_t) * ngroups_max);
- q = strv_length(m);
- l = l + q - 1;
+ r = getgroups(ngroups_max, gids);
+ if (r < 0)
+ return -errno;
- w = realloc(ret, sizeof(char*) * (l+1));
- if (!w) {
- ret[k] = NULL;
- strv_free(ret);
- strv_free(m);
- return NULL;
- }
+ for (i = 0; i < r; i++)
+ if (gids[i] == gid)
+ return 1;
- ret = w;
- if (m) {
- memcpy(ret + k, m, q * sizeof(char*));
- free(m);
- }
+ return 0;
+}
- k += q;
- continue;
- }
+int in_group(const char *name) {
+ int r;
+ gid_t gid;
- /* If ${FOO} appears as part of a word, replace it by the variable as-is */
- ret[k] = replace_env(*i, env);
- if (!ret[k]) {
- strv_free(ret);
- return NULL;
- }
- k++;
- }
+ r = get_group_creds(&name, &gid);
+ if (r < 0)
+ return r;
- ret[k] = NULL;
- return ret;
+ return in_gid(gid);
}
-int fd_columns(int fd) {
- struct winsize ws = {};
+int glob_exists(const char *path) {
+ _cleanup_globfree_ glob_t g = {};
+ int k;
- if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
- return -errno;
+ assert(path);
- if (ws.ws_col <= 0)
- return -EIO;
+ errno = 0;
+ k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
- return ws.ws_col;
+ if (k == GLOB_NOMATCH)
+ return 0;
+ else if (k == GLOB_NOSPACE)
+ return -ENOMEM;
+ else if (k == 0)
+ return !strv_isempty(g.gl_pathv);
+ else
+ return errno ? -errno : -EIO;
}
-unsigned columns(void) {
- const char *e;
- int c;
-
- if (_likely_(cached_columns > 0))
- return cached_columns;
+int glob_extend(char ***strv, const char *path) {
+ _cleanup_globfree_ glob_t g = {};
+ int k;
+ char **p;
- c = 0;
- e = getenv("COLUMNS");
- if (e)
- (void) safe_atoi(e, &c);
+ errno = 0;
+ k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
- if (c <= 0)
- c = fd_columns(STDOUT_FILENO);
+ if (k == GLOB_NOMATCH)
+ return -ENOENT;
+ else if (k == GLOB_NOSPACE)
+ return -ENOMEM;
+ else if (k != 0 || strv_isempty(g.gl_pathv))
+ return errno ? -errno : -EIO;
- if (c <= 0)
- c = 80;
+ STRV_FOREACH(p, g.gl_pathv) {
+ k = strv_extend(strv, *p);
+ if (k < 0)
+ break;
+ }
- cached_columns = c;
- return c;
+ return k;
}
-int fd_lines(int fd) {
- struct winsize ws = {};
+int dirent_ensure_type(DIR *d, struct dirent *de) {
+ struct stat st;
- if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
- return -errno;
+ assert(d);
+ assert(de);
- if (ws.ws_row <= 0)
- return -EIO;
+ if (de->d_type != DT_UNKNOWN)
+ return 0;
- return ws.ws_row;
-}
+ if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
+ return -errno;
-unsigned lines(void) {
- const char *e;
- unsigned l;
+ de->d_type =
+ S_ISREG(st.st_mode) ? DT_REG :
+ S_ISDIR(st.st_mode) ? DT_DIR :
+ S_ISLNK(st.st_mode) ? DT_LNK :
+ S_ISFIFO(st.st_mode) ? DT_FIFO :
+ S_ISSOCK(st.st_mode) ? DT_SOCK :
+ S_ISCHR(st.st_mode) ? DT_CHR :
+ S_ISBLK(st.st_mode) ? DT_BLK :
+ DT_UNKNOWN;
- if (_likely_(cached_lines > 0))
- return cached_lines;
+ return 0;
+}
- l = 0;
- e = getenv("LINES");
- if (e)
- (void) safe_atou(e, &l);
+int get_files_in_directory(const char *path, char ***list) {
+ _cleanup_closedir_ DIR *d = NULL;
+ size_t bufsize = 0, n = 0;
+ _cleanup_strv_free_ char **l = NULL;
- if (l <= 0)
- l = fd_lines(STDOUT_FILENO);
+ assert(path);
- if (l <= 0)
- l = 24;
+ /* Returns all files in a directory in *list, and the number
+ * of files as return value. If list is NULL returns only the
+ * number. */
- cached_lines = l;
- return cached_lines;
-}
+ d = opendir(path);
+ if (!d)
+ return -errno;
-/* intended to be used as a SIGWINCH sighandler */
-void columns_lines_cache_reset(int signum) {
- cached_columns = 0;
- cached_lines = 0;
-}
+ for (;;) {
+ struct dirent *de;
-bool on_tty(void) {
- static int cached_on_tty = -1;
+ errno = 0;
+ de = readdir(d);
+ if (!de && errno != 0)
+ return -errno;
+ if (!de)
+ break;
- if (_unlikely_(cached_on_tty < 0))
- cached_on_tty = isatty(STDOUT_FILENO) > 0;
+ dirent_ensure_type(d, de);
- return cached_on_tty;
-}
+ if (!dirent_is_file(de))
+ continue;
-int files_same(const char *filea, const char *fileb) {
- struct stat a, b;
+ if (list) {
+ /* one extra slot is needed for the terminating NULL */
+ if (!GREEDY_REALLOC(l, bufsize, n + 2))
+ return -ENOMEM;
- if (stat(filea, &a) < 0)
- return -errno;
+ l[n] = strdup(de->d_name);
+ if (!l[n])
+ return -ENOMEM;
- if (stat(fileb, &b) < 0)
- return -errno;
+ l[++n] = NULL;
+ } else
+ n++;
+ }
- return a.st_dev == b.st_dev &&
- a.st_ino == b.st_ino;
+ if (list) {
+ *list = l;
+ l = NULL; /* avoid freeing */
+ }
+
+ return n;
}
-int running_in_chroot(void) {
- int ret;
+char *strjoin(const char *x, ...) {
+ va_list ap;
+ size_t l;
+ char *r, *p;
- ret = files_same("/proc/1/root", "/");
- if (ret < 0)
- return ret;
+ va_start(ap, x);
- return ret == 0;
-}
+ if (x) {
+ l = strlen(x);
-static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
- size_t x;
- char *r;
+ for (;;) {
+ const char *t;
+ size_t n;
- assert(s);
- assert(percent <= 100);
- assert(new_length >= 3);
+ t = va_arg(ap, const char *);
+ if (!t)
+ break;
- if (old_length <= 3 || old_length <= new_length)
- return strndup(s, old_length);
+ n = strlen(t);
+ if (n > ((size_t) -1) - l) {
+ va_end(ap);
+ return NULL;
+ }
- r = new0(char, new_length+1);
+ l += n;
+ }
+ } else
+ l = 0;
+
+ va_end(ap);
+
+ r = new(char, l+1);
if (!r)
return NULL;
- x = (new_length * percent) / 100;
+ if (x) {
+ p = stpcpy(r, x);
- if (x > new_length - 3)
- x = new_length - 3;
+ va_start(ap, x);
- memcpy(r, s, x);
- r[x] = '.';
- r[x+1] = '.';
- r[x+2] = '.';
- memcpy(r + x + 3,
- s + old_length - (new_length - x - 3),
- new_length - x - 3);
+ for (;;) {
+ const char *t;
+
+ t = va_arg(ap, const char *);
+ if (!t)
+ break;
+
+ p = stpcpy(p, t);
+ }
+
+ va_end(ap);
+ } else
+ r[0] = 0;
return r;
}
-char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
- size_t x;
- char *e;
- const char *i, *j;
- unsigned k, len, len2;
+bool is_main_thread(void) {
+ static thread_local int cached = 0;
- assert(s);
- assert(percent <= 100);
- assert(new_length >= 3);
+ if (_unlikely_(cached == 0))
+ cached = getpid() == gettid() ? 1 : -1;
- /* if no multibyte characters use ascii_ellipsize_mem for speed */
- if (ascii_is_valid(s))
- return ascii_ellipsize_mem(s, old_length, new_length, percent);
+ return cached > 0;
+}
- if (old_length <= 3 || old_length <= new_length)
- return strndup(s, old_length);
+int block_get_whole_disk(dev_t d, dev_t *ret) {
+ char *p, *s;
+ int r;
+ unsigned n, m;
- x = (new_length * percent) / 100;
+ assert(ret);
- if (x > new_length - 3)
- x = new_length - 3;
+ /* If it has a queue this is good enough for us */
+ if (asprintf(&p, "/sys/dev/block/%u:%u/queue", major(d), minor(d)) < 0)
+ return -ENOMEM;
- k = 0;
- for (i = s; k < x && i < s + old_length; i = utf8_next_char(i)) {
- int c;
+ r = access(p, F_OK);
+ free(p);
- c = utf8_encoded_to_unichar(i);
- if (c < 0)
- return NULL;
- k += unichar_iswide(c) ? 2 : 1;
+ if (r >= 0) {
+ *ret = d;
+ return 0;
}
- if (k > x) /* last character was wide and went over quota */
- x ++;
+ /* If it is a partition find the originating device */
+ if (asprintf(&p, "/sys/dev/block/%u:%u/partition", major(d), minor(d)) < 0)
+ return -ENOMEM;
- for (j = s + old_length; k < new_length && j > i; ) {
- int c;
+ r = access(p, F_OK);
+ free(p);
- j = utf8_prev_char(j);
- c = utf8_encoded_to_unichar(j);
- if (c < 0)
- return NULL;
- k += unichar_iswide(c) ? 2 : 1;
- }
- assert(i <= j);
+ if (r < 0)
+ return -ENOENT;
- /* we don't actually need to ellipsize */
- if (i == j)
- return memdup(s, old_length + 1);
+ /* Get parent dev_t */
+ if (asprintf(&p, "/sys/dev/block/%u:%u/../dev", major(d), minor(d)) < 0)
+ return -ENOMEM;
- /* make space for ellipsis */
- j = utf8_next_char(j);
+ r = read_one_line_file(p, &s);
+ free(p);
- len = i - s;
- len2 = s + old_length - j;
- e = new(char, len + 3 + len2 + 1);
- if (!e)
- return NULL;
+ if (r < 0)
+ return r;
- /*
- printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
- old_length, new_length, x, len, len2, k);
- */
+ r = sscanf(s, "%u:%u", &m, &n);
+ free(s);
- memcpy(e, s, len);
- e[len] = 0xe2; /* tri-dot ellipsis: … */
- e[len + 1] = 0x80;
- e[len + 2] = 0xa6;
+ if (r != 2)
+ return -EINVAL;
- memcpy(e + len + 3, j, len2 + 1);
+ /* Only return this if it is really good enough for us. */
+ if (asprintf(&p, "/sys/dev/block/%u:%u/queue", m, n) < 0)
+ return -ENOMEM;
- return e;
-}
+ r = access(p, F_OK);
+ free(p);
-char *ellipsize(const char *s, size_t length, unsigned percent) {
- return ellipsize_mem(s, strlen(s), length, percent);
+ if (r >= 0) {
+ *ret = makedev(m, n);
+ return 0;
+ }
+
+ return -ENOENT;
}
-int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) {
- _cleanup_close_ int fd;
- int r;
+static const char *const ioprio_class_table[] = {
+ [IOPRIO_CLASS_NONE] = "none",
+ [IOPRIO_CLASS_RT] = "realtime",
+ [IOPRIO_CLASS_BE] = "best-effort",
+ [IOPRIO_CLASS_IDLE] = "idle"
+};
- assert(path);
+DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, INT_MAX);
- if (parents)
- mkdir_parents(path, 0755);
+static const char *const sigchld_code_table[] = {
+ [CLD_EXITED] = "exited",
+ [CLD_KILLED] = "killed",
+ [CLD_DUMPED] = "dumped",
+ [CLD_TRAPPED] = "trapped",
+ [CLD_STOPPED] = "stopped",
+ [CLD_CONTINUED] = "continued",
+};
- fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode > 0 ? mode : 0644);
- if (fd < 0)
- return -errno;
+DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
- if (mode > 0) {
- r = fchmod(fd, mode);
- if (r < 0)
- return -errno;
- }
+static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
+ [LOG_FAC(LOG_KERN)] = "kern",
+ [LOG_FAC(LOG_USER)] = "user",
+ [LOG_FAC(LOG_MAIL)] = "mail",
+ [LOG_FAC(LOG_DAEMON)] = "daemon",
+ [LOG_FAC(LOG_AUTH)] = "auth",
+ [LOG_FAC(LOG_SYSLOG)] = "syslog",
+ [LOG_FAC(LOG_LPR)] = "lpr",
+ [LOG_FAC(LOG_NEWS)] = "news",
+ [LOG_FAC(LOG_UUCP)] = "uucp",
+ [LOG_FAC(LOG_CRON)] = "cron",
+ [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
+ [LOG_FAC(LOG_FTP)] = "ftp",
+ [LOG_FAC(LOG_LOCAL0)] = "local0",
+ [LOG_FAC(LOG_LOCAL1)] = "local1",
+ [LOG_FAC(LOG_LOCAL2)] = "local2",
+ [LOG_FAC(LOG_LOCAL3)] = "local3",
+ [LOG_FAC(LOG_LOCAL4)] = "local4",
+ [LOG_FAC(LOG_LOCAL5)] = "local5",
+ [LOG_FAC(LOG_LOCAL6)] = "local6",
+ [LOG_FAC(LOG_LOCAL7)] = "local7"
+};
- if (uid != UID_INVALID || gid != GID_INVALID) {
- r = fchown(fd, uid, gid);
- if (r < 0)
- return -errno;
- }
+DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
- if (stamp != USEC_INFINITY) {
- struct timespec ts[2];
+static const char *const log_level_table[] = {
+ [LOG_EMERG] = "emerg",
+ [LOG_ALERT] = "alert",
+ [LOG_CRIT] = "crit",
+ [LOG_ERR] = "err",
+ [LOG_WARNING] = "warning",
+ [LOG_NOTICE] = "notice",
+ [LOG_INFO] = "info",
+ [LOG_DEBUG] = "debug"
+};
- timespec_store(&ts[0], stamp);
- ts[1] = ts[0];
- r = futimens(fd, ts);
- } else
- r = futimens(fd, NULL);
- if (r < 0)
- return -errno;
+DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
- return 0;
-}
+static const char* const sched_policy_table[] = {
+ [SCHED_OTHER] = "other",
+ [SCHED_BATCH] = "batch",
+ [SCHED_IDLE] = "idle",
+ [SCHED_FIFO] = "fifo",
+ [SCHED_RR] = "rr"
+};
-int touch(const char *path) {
- return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, 0);
-}
+DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX);
-char *unquote(const char *s, const char* quotes) {
- size_t l;
- assert(s);
+static const char* const rlimit_table[_RLIMIT_MAX] = {
+ [RLIMIT_CPU] = "LimitCPU",
+ [RLIMIT_FSIZE] = "LimitFSIZE",
+ [RLIMIT_DATA] = "LimitDATA",
+ [RLIMIT_STACK] = "LimitSTACK",
+ [RLIMIT_CORE] = "LimitCORE",
+ [RLIMIT_RSS] = "LimitRSS",
+ [RLIMIT_NOFILE] = "LimitNOFILE",
+ [RLIMIT_AS] = "LimitAS",
+ [RLIMIT_NPROC] = "LimitNPROC",
+ [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
+ [RLIMIT_LOCKS] = "LimitLOCKS",
+ [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
+ [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
+ [RLIMIT_NICE] = "LimitNICE",
+ [RLIMIT_RTPRIO] = "LimitRTPRIO",
+ [RLIMIT_RTTIME] = "LimitRTTIME"
+};
- /* This is rather stupid, simply removes the heading and
- * trailing quotes if there is one. Doesn't care about
- * escaping or anything. We should make this smarter one
- * day...*/
+DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
- l = strlen(s);
- if (l < 2)
- return strdup(s);
+static const char* const ip_tos_table[] = {
+ [IPTOS_LOWDELAY] = "low-delay",
+ [IPTOS_THROUGHPUT] = "throughput",
+ [IPTOS_RELIABILITY] = "reliability",
+ [IPTOS_LOWCOST] = "low-cost",
+};
- if (strchr(quotes, s[0]) && s[l-1] == s[0])
- return strndup(s+1, l-2);
+DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
- return strdup(s);
-}
+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",
+#ifdef SIGSTKFLT
+ [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */
+#endif
+ [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"
+};
-char *normalize_env_assignment(const char *s) {
- _cleanup_free_ char *value = NULL;
- const char *eq;
- char *p, *name;
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
- eq = strchr(s, '=');
- if (!eq) {
- char *r, *t;
+const char *signal_to_string(int signo) {
+ static thread_local char buf[sizeof("RTMIN+")-1 + DECIMAL_STR_MAX(int) + 1];
+ const char *name;
- r = strdup(s);
- if (!r)
- return NULL;
+ name = __signal_to_string(signo);
+ if (name)
+ return name;
- t = strstrip(r);
- if (t != r)
- memmove(r, t, strlen(t) + 1);
+ if (signo >= SIGRTMIN && signo <= SIGRTMAX)
+ snprintf(buf, sizeof(buf), "RTMIN+%d", signo - SIGRTMIN);
+ else
+ snprintf(buf, sizeof(buf), "%d", signo);
- return r;
- }
+ return buf;
+}
- name = strndupa(s, eq - s);
- p = strdupa(eq + 1);
+int signal_from_string(const char *s) {
+ int signo;
+ int offset = 0;
+ unsigned u;
- value = unquote(strstrip(p), QUOTES);
- if (!value)
- return NULL;
+ signo = __signal_from_string(s);
+ if (signo > 0)
+ return signo;
- return strjoin(strstrip(name), "=", value, NULL);
+ if (startswith(s, "RTMIN+")) {
+ s += 6;
+ offset = SIGRTMIN;
+ }
+ if (safe_atou(s, &u) >= 0) {
+ signo = (int) u + offset;
+ if (signo > 0 && signo < _NSIG)
+ return signo;
+ }
+ return -EINVAL;
}
-int wait_for_terminate(pid_t pid, siginfo_t *status) {
- siginfo_t dummy;
+bool kexec_loaded(void) {
+ bool loaded = false;
+ char *s;
- assert(pid >= 1);
+ if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) {
+ if (s[0] == '1')
+ loaded = true;
+ free(s);
+ }
+ return loaded;
+}
- if (!status)
- status = &dummy;
+int prot_from_flags(int flags) {
- for (;;) {
- zero(*status);
+ switch (flags & O_ACCMODE) {
- if (waitid(P_PID, pid, status, WEXITED) < 0) {
+ case O_RDONLY:
+ return PROT_READ;
- if (errno == EINTR)
- continue;
+ case O_WRONLY:
+ return PROT_WRITE;
- return -errno;
- }
+ case O_RDWR:
+ return PROT_READ|PROT_WRITE;
- return 0;
+ default:
+ return -EINVAL;
}
}
-/*
- * Return values:
- * < 0 : wait_for_terminate() failed to get the state of the
- * process, the process was terminated by a signal, or
- * failed for an unknown reason.
- * >=0 : The process terminated normally, and its exit code is
- * returned.
- *
- * That is, success is indicated by a return value of zero, and an
- * error is indicated by a non-zero value.
- *
- * A warning is emitted if the process terminates abnormally,
- * and also if it returns non-zero unless check_exit_code is true.
- */
-int wait_for_terminate_and_warn(const char *name, pid_t pid, bool check_exit_code) {
- int r;
- siginfo_t status;
+char *format_bytes(char *buf, size_t l, off_t t) {
+ unsigned i;
- assert(name);
- assert(pid > 1);
+ static const struct {
+ const char *suffix;
+ off_t factor;
+ } table[] = {
+ { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
+ { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
+ { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
+ { "G", 1024ULL*1024ULL*1024ULL },
+ { "M", 1024ULL*1024ULL },
+ { "K", 1024ULL },
+ };
- r = wait_for_terminate(pid, &status);
- if (r < 0)
- return log_warning_errno(r, "Failed to wait for %s: %m", name);
+ if (t == (off_t) -1)
+ return NULL;
- if (status.si_code == CLD_EXITED) {
- if (status.si_status != 0)
- log_full(check_exit_code ? LOG_WARNING : LOG_DEBUG,
- "%s failed with error code %i.", name, status.si_status);
- else
- log_debug("%s succeeded.", name);
+ for (i = 0; i < ELEMENTSOF(table); i++) {
- return status.si_status;
- } else if (status.si_code == CLD_KILLED ||
- status.si_code == CLD_DUMPED) {
+ if (t >= table[i].factor) {
+ snprintf(buf, l,
+ "%llu.%llu%s",
+ (unsigned long long) (t / table[i].factor),
+ (unsigned long long) (((t*10ULL) / table[i].factor) % 10ULL),
+ table[i].suffix);
- log_warning("%s terminated by signal %s.", name, signal_to_string(status.si_status));
- return -EPROTO;
+ goto finish;
+ }
}
- log_warning("%s failed due to unknown reason.", name);
- return -EPROTO;
-}
+ snprintf(buf, l, "%lluB", (unsigned long long) t);
-noreturn void freeze(void) {
+finish:
+ buf[l-1] = 0;
+ return buf;
- /* Make sure nobody waits for us on a socket anymore */
- close_all_fds(NULL, 0);
+}
- sync();
+void* memdup(const void *p, size_t l) {
+ void *r;
- for (;;)
- pause();
-}
+ assert(p);
-bool null_or_empty(struct stat *st) {
- assert(st);
-
- if (S_ISREG(st->st_mode) && st->st_size <= 0)
- return true;
-
- if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
- return true;
+ r = malloc(l);
+ if (!r)
+ return NULL;
- return false;
+ memcpy(r, p, l);
+ return r;
}
-int null_or_empty_path(const char *fn) {
- struct stat st;
-
- assert(fn);
-
- if (stat(fn, &st) < 0)
- return -errno;
-
- return null_or_empty(&st);
-}
+int fd_inc_sndbuf(int fd, size_t n) {
+ int r, value;
+ socklen_t l = sizeof(value);
-int null_or_empty_fd(int fd) {
- struct stat st;
+ r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
+ if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
+ return 0;
- assert(fd >= 0);
+ /* If we have the privileges we will ignore the kernel limit. */
- if (fstat(fd, &st) < 0)
- return -errno;
+ value = (int) n;
+ if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0)
+ if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0)
+ return -errno;
- return null_or_empty(&st);
+ return 1;
}
-DIR *xopendirat(int fd, const char *name, int flags) {
- int nfd;
- DIR *d;
-
- assert(!(flags & O_CREAT));
-
- nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
- if (nfd < 0)
- return NULL;
-
- d = fdopendir(nfd);
- if (!d) {
- safe_close(nfd);
- return NULL;
- }
-
- return d;
-}
+int fd_inc_rcvbuf(int fd, size_t n) {
+ int r, value;
+ socklen_t l = sizeof(value);
-int signal_from_string_try_harder(const char *s) {
- int signo;
- assert(s);
+ r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
+ if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
+ return 0;
- signo = signal_from_string(s);
- if (signo <= 0)
- if (startswith(s, "SIG"))
- return signal_from_string(s+3);
+ /* If we have the privileges we will ignore the kernel limit. */
- return signo;
+ value = (int) n;
+ if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0)
+ if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0)
+ return -errno;
+ return 1;
}
-static char *tag_to_udev_node(const char *tagvalue, const char *by) {
- _cleanup_free_ char *t = NULL, *u = NULL;
- size_t enc_len;
-
- u = unquote(tagvalue, "\"\'");
- if (!u)
- return NULL;
-
- enc_len = strlen(u) * 4 + 1;
- t = new(char, enc_len);
- if (!t)
- return NULL;
-
- if (encode_devnode_name(u, t, enc_len) < 0)
- return NULL;
+int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) {
+ bool stdout_is_tty, stderr_is_tty;
+ pid_t parent_pid, agent_pid;
+ sigset_t ss, saved_ss;
+ unsigned n, i;
+ va_list ap;
+ char **l;
- return strjoin("/dev/disk/by-", by, "/", t, NULL);
-}
+ assert(pid);
+ assert(path);
-char *fstab_node_to_udev_node(const char *p) {
- assert(p);
+ /* Spawns a temporary TTY agent, making sure it goes away when
+ * we go away */
- if (startswith(p, "LABEL="))
- return tag_to_udev_node(p+6, "label");
+ parent_pid = getpid();
- if (startswith(p, "UUID="))
- return tag_to_udev_node(p+5, "uuid");
+ /* First we temporarily block all signals, so that the new
+ * child has them blocked initially. This way, we can be sure
+ * that SIGTERMs are not lost we might send to the agent. */
+ assert_se(sigfillset(&ss) >= 0);
+ assert_se(sigprocmask(SIG_SETMASK, &ss, &saved_ss) >= 0);
- if (startswith(p, "PARTUUID="))
- return tag_to_udev_node(p+9, "partuuid");
+ agent_pid = fork();
+ if (agent_pid < 0) {
+ assert_se(sigprocmask(SIG_SETMASK, &saved_ss, NULL) >= 0);
+ return -errno;
+ }
- if (startswith(p, "PARTLABEL="))
- return tag_to_udev_node(p+10, "partlabel");
+ if (agent_pid != 0) {
+ assert_se(sigprocmask(SIG_SETMASK, &saved_ss, NULL) >= 0);
+ *pid = agent_pid;
+ return 0;
+ }
- return strdup(p);
-}
+ /* In the child:
+ *
+ * Make sure the agent goes away when the parent dies */
+ if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
+ _exit(EXIT_FAILURE);
-bool tty_is_vc(const char *tty) {
- assert(tty);
+ /* Make sure we actually can kill the agent, if we need to, in
+ * case somebody invoked us from a shell script that trapped
+ * SIGTERM or so... */
+ reset_all_signal_handlers();
+ reset_signal_mask();
- return vtnr_from_tty(tty) >= 0;
-}
+ /* Check whether our parent died before we were able
+ * to set the death signal and unblock the signals */
+ if (getppid() != parent_pid)
+ _exit(EXIT_SUCCESS);
-bool tty_is_console(const char *tty) {
- assert(tty);
+ /* Don't leak fds to the agent */
+ close_all_fds(except, n_except);
- if (startswith(tty, "/dev/"))
- tty += 5;
+ stdout_is_tty = isatty(STDOUT_FILENO);
+ stderr_is_tty = isatty(STDERR_FILENO);
- return streq(tty, "console");
-}
+ if (!stdout_is_tty || !stderr_is_tty) {
+ int fd;
-int vtnr_from_tty(const char *tty) {
- int i, r;
+ /* Detach from stdout/stderr. and reopen
+ * /dev/tty for them. This is important to
+ * ensure that when systemctl is started via
+ * popen() or a similar call that expects to
+ * read EOF we actually do generate EOF and
+ * not delay this indefinitely by because we
+ * keep an unused copy of stdin around. */
+ fd = open("/dev/tty", O_WRONLY);
+ if (fd < 0) {
+ log_error_errno(errno, "Failed to open /dev/tty: %m");
+ _exit(EXIT_FAILURE);
+ }
- assert(tty);
+ if (!stdout_is_tty)
+ dup2(fd, STDOUT_FILENO);
- if (startswith(tty, "/dev/"))
- tty += 5;
+ if (!stderr_is_tty)
+ dup2(fd, STDERR_FILENO);
- if (!startswith(tty, "tty") )
- return -EINVAL;
+ if (fd > 2)
+ close(fd);
+ }
- if (tty[3] < '0' || tty[3] > '9')
- return -EINVAL;
+ /* Count arguments */
+ va_start(ap, path);
+ for (n = 0; va_arg(ap, char*); n++)
+ ;
+ va_end(ap);
- r = safe_atoi(tty+3, &i);
- if (r < 0)
- return r;
+ /* Allocate strv */
+ l = alloca(sizeof(char *) * (n + 1));
- if (i < 0 || i > 63)
- return -EINVAL;
+ /* Fill in arguments */
+ va_start(ap, path);
+ for (i = 0; i <= n; i++)
+ l[i] = va_arg(ap, char*);
+ va_end(ap);
- return i;
+ execv(path, l);
+ _exit(EXIT_FAILURE);
}
-char *resolve_dev_console(char **active) {
- char *tty;
+int setrlimit_closest(int resource, const struct rlimit *rlim) {
+ struct rlimit highest, fixed;
- /* Resolve where /dev/console is pointing to, if /sys is actually ours
- * (i.e. not read-only-mounted which is a sign for container setups) */
+ assert(rlim);
- if (path_is_read_only_fs("/sys") > 0)
- return NULL;
+ if (setrlimit(resource, rlim) >= 0)
+ return 0;
- if (read_one_line_file("/sys/class/tty/console/active", active) < 0)
- return NULL;
+ if (errno != EPERM)
+ return -errno;
- /* If multiple log outputs are configured the last one is what
- * /dev/console points to */
- tty = strrchr(*active, ' ');
- if (tty)
- tty++;
- else
- tty = *active;
+ /* So we failed to set the desired setrlimit, then let's try
+ * to get as close as we can */
+ assert_se(getrlimit(resource, &highest) == 0);
- if (streq(tty, "tty0")) {
- char *tmp;
+ fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
+ fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
- /* Get the active VC (e.g. tty1) */
- if (read_one_line_file("/sys/class/tty/tty0/active", &tmp) >= 0) {
- free(*active);
- tty = *active = tmp;
- }
- }
+ if (setrlimit(resource, &fixed) < 0)
+ return -errno;
- return tty;
+ return 0;
}
-bool tty_is_vc_resolve(const char *tty) {
- _cleanup_free_ char *active = NULL;
-
- assert(tty);
+bool http_etag_is_valid(const char *etag) {
+ if (isempty(etag))
+ return false;
- if (startswith(tty, "/dev/"))
- tty += 5;
+ if (!endswith(etag, "\""))
+ return false;
- if (streq(tty, "console")) {
- tty = resolve_dev_console(&active);
- if (!tty)
- return false;
- }
+ if (!startswith(etag, "\"") && !startswith(etag, "W/\""))
+ return false;
- return tty_is_vc(tty);
+ return true;
}
-const char *default_term_for_tty(const char *tty) {
- assert(tty);
-
- return tty_is_vc_resolve(tty) ? "TERM=linux" : "TERM=vt102";
-}
+bool http_url_is_valid(const char *url) {
+ const char *p;
-bool dirent_is_file(const struct dirent *de) {
- assert(de);
+ if (isempty(url))
+ return false;
- if (ignore_file(de->d_name))
+ p = startswith(url, "http://");
+ if (!p)
+ p = startswith(url, "https://");
+ if (!p)
return false;
- if (de->d_type != DT_REG &&
- de->d_type != DT_LNK &&
- de->d_type != DT_UNKNOWN)
+ if (isempty(p))
return false;
- return true;
+ return ascii_is_valid(p);
}
-bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
- assert(de);
+bool documentation_url_is_valid(const char *url) {
+ const char *p;
- if (de->d_type != DT_REG &&
- de->d_type != DT_LNK &&
- de->d_type != DT_UNKNOWN)
+ if (isempty(url))
return false;
- if (ignore_file_allow_backup(de->d_name))
+ if (http_url_is_valid(url))
+ return true;
+
+ p = startswith(url, "file:/");
+ if (!p)
+ p = startswith(url, "info:");
+ if (!p)
+ p = startswith(url, "man:");
+
+ if (isempty(p))
return false;
- return endswith(de->d_name, suffix);
+ return ascii_is_valid(p);
}
-void execute_directory(const char *directory, DIR *d, usec_t timeout, char *argv[]) {
- pid_t executor_pid;
- int r;
-
- assert(directory);
+bool in_initrd(void) {
+ static int saved = -1;
+ struct statfs s;
- /* Executes all binaries in a directory in parallel and waits
- * for them to finish. Optionally a timeout is applied. */
+ if (saved >= 0)
+ return saved;
- executor_pid = fork();
- if (executor_pid < 0) {
- log_error_errno(errno, "Failed to fork: %m");
- return;
-
- } else if (executor_pid == 0) {
- _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
- _cleanup_closedir_ DIR *_d = NULL;
- struct dirent *de;
-
- /* We fork this all off from a child process so that
- * we can somewhat cleanly make use of SIGALRM to set
- * a time limit */
-
- reset_all_signal_handlers();
- reset_signal_mask();
-
- assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
-
- if (!d) {
- d = _d = opendir(directory);
- if (!d) {
- if (errno == ENOENT)
- _exit(EXIT_SUCCESS);
-
- log_error_errno(errno, "Failed to enumerate directory %s: %m", directory);
- _exit(EXIT_FAILURE);
- }
- }
+ /* We make two checks here:
+ *
+ * 1. the flag file /etc/initrd-release must exist
+ * 2. the root file system must be a memory file system
+ *
+ * The second check is extra paranoia, since misdetecting an
+ * initrd can have bad bad consequences due the initrd
+ * emptying when transititioning to the main systemd.
+ */
- pids = hashmap_new(NULL);
- if (!pids) {
- log_oom();
- _exit(EXIT_FAILURE);
- }
+ saved = access("/etc/initrd-release", F_OK) >= 0 &&
+ statfs("/", &s) >= 0 &&
+ is_temporary_fs(&s);
- FOREACH_DIRENT(de, d, break) {
- _cleanup_free_ char *path = NULL;
- pid_t pid;
+ return saved;
+}
- if (!dirent_is_file(de))
- continue;
+int get_home_dir(char **_h) {
+ struct passwd *p;
+ const char *e;
+ char *h;
+ uid_t u;
- path = strjoin(directory, "/", de->d_name, NULL);
- if (!path) {
- log_oom();
- _exit(EXIT_FAILURE);
- }
+ assert(_h);
- pid = fork();
- if (pid < 0) {
- log_error_errno(errno, "Failed to fork: %m");
- continue;
- } else if (pid == 0) {
- char *_argv[2];
+ /* Take the user specified one */
+ e = secure_getenv("HOME");
+ if (e && path_is_absolute(e)) {
+ h = strdup(e);
+ if (!h)
+ return -ENOMEM;
- assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
+ *_h = h;
+ return 0;
+ }
- if (!argv) {
- _argv[0] = path;
- _argv[1] = NULL;
- argv = _argv;
- } else
- argv[0] = path;
+ /* Hardcode home directory for root to avoid NSS */
+ u = getuid();
+ if (u == 0) {
+ h = strdup("/root");
+ if (!h)
+ return -ENOMEM;
- execv(path, argv);
- log_error_errno(errno, "Failed to execute %s: %m", path);
- _exit(EXIT_FAILURE);
- }
+ *_h = h;
+ return 0;
+ }
- log_debug("Spawned %s as " PID_FMT ".", path, pid);
+ /* Check the database... */
+ errno = 0;
+ p = getpwuid(u);
+ if (!p)
+ return errno > 0 ? -errno : -ESRCH;
- r = hashmap_put(pids, UINT_TO_PTR(pid), path);
- if (r < 0) {
- log_oom();
- _exit(EXIT_FAILURE);
- }
+ if (!path_is_absolute(p->pw_dir))
+ return -EINVAL;
- path = NULL;
- }
+ h = strdup(p->pw_dir);
+ if (!h)
+ return -ENOMEM;
- /* Abort execution of this process after the
- * timout. We simply rely on SIGALRM as default action
- * terminating the process, and turn on alarm(). */
+ *_h = h;
+ return 0;
+}
- if (timeout != USEC_INFINITY)
- alarm((timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
+int get_shell(char **_s) {
+ struct passwd *p;
+ const char *e;
+ char *s;
+ uid_t u;
- while (!hashmap_isempty(pids)) {
- _cleanup_free_ char *path = NULL;
- pid_t pid;
+ assert(_s);
- pid = PTR_TO_UINT(hashmap_first_key(pids));
- assert(pid > 0);
+ /* Take the user specified one */
+ e = getenv("SHELL");
+ if (e) {
+ s = strdup(e);
+ if (!s)
+ return -ENOMEM;
- path = hashmap_remove(pids, UINT_TO_PTR(pid));
- assert(path);
+ *_s = s;
+ return 0;
+ }
- wait_for_terminate_and_warn(path, pid, true);
- }
+ /* Hardcode home directory for root to avoid NSS */
+ u = getuid();
+ if (u == 0) {
+ s = strdup("/bin/sh");
+ if (!s)
+ return -ENOMEM;
- _exit(EXIT_SUCCESS);
+ *_s = s;
+ return 0;
}
- wait_for_terminate_and_warn(directory, executor_pid, true);
-}
-
-int kill_and_sigcont(pid_t pid, int sig) {
- int r;
+ /* Check the database... */
+ errno = 0;
+ p = getpwuid(u);
+ if (!p)
+ return errno > 0 ? -errno : -ESRCH;
- r = kill(pid, sig) < 0 ? -errno : 0;
+ if (!path_is_absolute(p->pw_shell))
+ return -EINVAL;
- if (r >= 0)
- kill(pid, SIGCONT);
+ s = strdup(p->pw_shell);
+ if (!s)
+ return -ENOMEM;
- return r;
+ *_s = s;
+ return 0;
}
-bool nulstr_contains(const char*nulstr, const char *needle) {
- const char *i;
+bool filename_is_valid(const char *p) {
- if (!nulstr)
+ if (isempty(p))
return false;
- NULSTR_FOREACH(i, nulstr)
- if (streq(i, needle))
- return true;
-
- return false;
-}
-
-bool plymouth_running(void) {
- return access("/run/plymouth/pid", F_OK) >= 0;
-}
+ if (strchr(p, '/'))
+ return false;
-char* strshorten(char *s, size_t l) {
- assert(s);
+ if (streq(p, "."))
+ return false;
- if (l < strlen(s))
- s[l] = 0;
+ if (streq(p, ".."))
+ return false;
- return s;
-}
+ if (strlen(p) > FILENAME_MAX)
+ return false;
-static bool hostname_valid_char(char c) {
- return
- (c >= 'a' && c <= 'z') ||
- (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') ||
- c == '-' ||
- c == '_' ||
- c == '.';
+ return true;
}
-bool hostname_is_valid(const char *s) {
- const char *p;
- bool dot;
+bool string_is_safe(const char *p) {
+ const char *t;
- if (isempty(s))
+ if (!p)
return false;
- for (p = s, dot = true; *p; p++) {
- if (*p == '.') {
- if (dot)
- return false;
-
- dot = true;
- } else {
- if (!hostname_valid_char(*p))
- return false;
+ for (t = p; *t; t++) {
+ if (*t > 0 && *t < ' ')
+ return false;
- dot = false;
- }
+ if (strchr("\\\"\'\0x7f", *t))
+ return false;
}
- if (dot)
- return false;
-
- if (p-s > HOST_NAME_MAX)
- return false;
-
return true;
}
-char* hostname_cleanup(char *s, bool lowercase) {
- char *p, *d;
- bool dot;
+/**
+ * Check if a string contains control characters. If 'ok' is non-NULL
+ * it may be a string containing additional CCs to be considered OK.
+ */
+bool string_has_cc(const char *p, const char *ok) {
+ const char *t;
- for (p = s, d = s, dot = true; *p; p++) {
- if (*p == '.') {
- if (dot)
- continue;
+ assert(p);
- *(d++) = '.';
- dot = true;
- } else if (hostname_valid_char(*p)) {
- *(d++) = lowercase ? tolower(*p) : *p;
- dot = false;
- }
+ for (t = p; *t; t++) {
+ if (ok && strchr(ok, *t))
+ continue;
+
+ if (*t > 0 && *t < ' ')
+ return true;
+ if (*t == 127)
+ return true;
}
- if (dot && d > s)
- d[-1] = 0;
- else
- *d = 0;
+ return false;
+}
- strshorten(s, HOST_NAME_MAX);
+bool path_is_safe(const char *p) {
- return s;
-}
+ if (isempty(p))
+ return false;
-bool machine_name_is_valid(const char *s) {
+ if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
+ return false;
- if (!hostname_is_valid(s))
+ if (strlen(p)+1 > PATH_MAX)
return false;
- /* Machine names should be useful hostnames, but also be
- * useful in unit names, hence we enforce a stricter length
- * limitation. */
+ /* The following two checks are not really dangerous, but hey, they still are confusing */
+ if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
+ return false;
- if (strlen(s) > 64)
+ if (strstr(p, "//"))
return false;
return true;
}
-int pipe_eof(int fd) {
- struct pollfd pollfd = {
- .fd = fd,
- .events = POLLIN|POLLHUP,
- };
-
- int r;
-
- r = poll(&pollfd, 1, 0);
- if (r < 0)
- return -errno;
-
- if (r == 0)
- return 0;
+/* hey glibc, APIs with callbacks without a user pointer are so useless */
+void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
+ int (*compar) (const void *, const void *, void *), void *arg) {
+ size_t l, u, idx;
+ const void *p;
+ int comparison;
- return pollfd.revents & POLLHUP;
+ l = 0;
+ u = nmemb;
+ while (l < u) {
+ idx = (l + u) / 2;
+ p = (void *)(((const char *) base) + (idx * size));
+ comparison = compar(key, p, arg);
+ if (comparison < 0)
+ u = idx;
+ else if (comparison > 0)
+ l = idx + 1;
+ else
+ return (void *)p;
+ }
+ return NULL;
}
-int fd_wait_for_event(int fd, int event, usec_t t) {
-
- struct pollfd pollfd = {
- .fd = fd,
- .events = event,
- };
-
- struct timespec ts;
- int r;
-
- r = ppoll(&pollfd, 1, t == USEC_INFINITY ? NULL : timespec_store(&ts, t), NULL);
- if (r < 0)
- return -errno;
-
- if (r == 0)
- return 0;
-
- return pollfd.revents;
+void init_gettext(void) {
+ setlocale(LC_ALL, "");
+ textdomain(GETTEXT_PACKAGE);
}
-int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
- FILE *f;
- char *t;
- int fd;
+bool is_locale_utf8(void) {
+ const char *set;
+ static int cached_answer = -1;
- assert(path);
- assert(_f);
- assert(_temp_path);
+ if (cached_answer >= 0)
+ goto out;
- t = tempfn_xxxxxx(path);
- if (!t)
- return -ENOMEM;
+ if (!setlocale(LC_ALL, "")) {
+ cached_answer = true;
+ goto out;
+ }
- fd = mkostemp_safe(t, O_WRONLY|O_CLOEXEC);
- if (fd < 0) {
- free(t);
- return -errno;
+ set = nl_langinfo(CODESET);
+ if (!set) {
+ cached_answer = true;
+ goto out;
}
- f = fdopen(fd, "we");
- if (!f) {
- unlink(t);
- free(t);
- return -errno;
+ if (streq(set, "UTF-8")) {
+ cached_answer = true;
+ goto out;
}
- *_f = f;
- *_temp_path = t;
+ /* For LC_CTYPE=="C" return true, because CTYPE is effectly
+ * unset and everything can do to UTF-8 nowadays. */
+ set = setlocale(LC_CTYPE, NULL);
+ if (!set) {
+ cached_answer = true;
+ goto out;
+ }
- return 0;
+ /* Check result, but ignore the result if C was set
+ * explicitly. */
+ cached_answer =
+ streq(set, "C") &&
+ !getenv("LC_ALL") &&
+ !getenv("LC_CTYPE") &&
+ !getenv("LANG");
+
+out:
+ return (bool) cached_answer;
}
-int terminal_vhangup_fd(int fd) {
- assert(fd >= 0);
+const char *draw_special_char(DrawSpecialChar ch) {
+ static const char *draw_table[2][_DRAW_SPECIAL_CHAR_MAX] = {
- if (ioctl(fd, TIOCVHANGUP) < 0)
- return -errno;
+ /* UTF-8 */ {
+ [DRAW_TREE_VERTICAL] = "\342\224\202 ", /* │ */
+ [DRAW_TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */
+ [DRAW_TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */
+ [DRAW_TREE_SPACE] = " ", /* */
+ [DRAW_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */
+ [DRAW_BLACK_CIRCLE] = "\342\227\217", /* ● */
+ [DRAW_ARROW] = "\342\206\222", /* → */
+ [DRAW_DASH] = "\342\200\223", /* – */
+ },
- return 0;
-}
+ /* ASCII fallback */ {
+ [DRAW_TREE_VERTICAL] = "| ",
+ [DRAW_TREE_BRANCH] = "|-",
+ [DRAW_TREE_RIGHT] = "`-",
+ [DRAW_TREE_SPACE] = " ",
+ [DRAW_TRIANGULAR_BULLET] = ">",
+ [DRAW_BLACK_CIRCLE] = "*",
+ [DRAW_ARROW] = "->",
+ [DRAW_DASH] = "-",
+ }
+ };
-int terminal_vhangup(const char *name) {
- _cleanup_close_ int fd;
+ return draw_table[!is_locale_utf8()][ch];
+}
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+char *strreplace(const char *text, const char *old_string, const char *new_string) {
+ const char *f;
+ char *t, *r;
+ size_t l, old_len, new_len;
- return terminal_vhangup_fd(fd);
-}
+ assert(text);
+ assert(old_string);
+ assert(new_string);
-int vt_disallocate(const char *name) {
- int fd, r;
- unsigned u;
+ old_len = strlen(old_string);
+ new_len = strlen(new_string);
- /* Deallocate the VT if possible. If not possible
- * (i.e. because it is the active one), at least clear it
- * entirely (including the scrollback buffer) */
+ l = strlen(text);
+ r = new(char, l+1);
+ if (!r)
+ return NULL;
- if (!startswith(name, "/dev/"))
- return -EINVAL;
+ f = text;
+ t = r;
+ while (*f) {
+ char *a;
+ size_t d, nl;
- if (!tty_is_vc(name)) {
- /* So this is not a VT. I guess we cannot deallocate
- * it then. But let's at least clear the screen */
+ if (!startswith(f, old_string)) {
+ *(t++) = *(f++);
+ continue;
+ }
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+ d = t - r;
+ nl = l - old_len + new_len;
+ a = realloc(r, nl + 1);
+ if (!a)
+ goto oom;
- loop_write(fd,
- "\033[r" /* clear scrolling region */
- "\033[H" /* move home */
- "\033[2J", /* clear screen */
- 10, false);
- safe_close(fd);
+ l = nl;
+ r = a;
+ t = r + d;
- return 0;
+ t = stpcpy(t, new_string);
+ f += old_len;
}
- if (!startswith(name, "/dev/tty"))
- return -EINVAL;
+ *t = 0;
+ return r;
- r = safe_atou(name+8, &u);
- if (r < 0)
- return r;
+oom:
+ free(r);
+ return NULL;
+}
- if (u <= 0)
- return -EINVAL;
+char *strip_tab_ansi(char **ibuf, size_t *_isz) {
+ const char *i, *begin = NULL;
+ enum {
+ STATE_OTHER,
+ STATE_ESCAPE,
+ STATE_BRACKET
+ } state = STATE_OTHER;
+ char *obuf = NULL;
+ size_t osz = 0, isz;
+ FILE *f;
- /* Try to deallocate */
- fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+ assert(ibuf);
+ assert(*ibuf);
- r = ioctl(fd, VT_DISALLOCATE, u);
- safe_close(fd);
+ /* Strips ANSI color and replaces TABs by 8 spaces */
- if (r >= 0)
- return 0;
+ isz = _isz ? *_isz : strlen(*ibuf);
- if (errno != EBUSY)
- return -errno;
+ f = open_memstream(&obuf, &osz);
+ if (!f)
+ return NULL;
- /* Couldn't deallocate, so let's clear it fully with
- * scrollback */
- fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
- if (fd < 0)
- return fd;
+ for (i = *ibuf; i < *ibuf + isz + 1; i++) {
- loop_write(fd,
- "\033[r" /* clear scrolling region */
- "\033[H" /* move home */
- "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
- 10, false);
- safe_close(fd);
+ switch (state) {
- return 0;
-}
+ case STATE_OTHER:
+ if (i >= *ibuf + isz) /* EOT */
+ break;
+ else if (*i == '\x1B')
+ state = STATE_ESCAPE;
+ else if (*i == '\t')
+ fputs(" ", f);
+ else
+ fputc(*i, f);
+ break;
-int symlink_atomic(const char *from, const char *to) {
- _cleanup_free_ char *t = NULL;
+ case STATE_ESCAPE:
+ if (i >= *ibuf + isz) { /* EOT */
+ fputc('\x1B', f);
+ break;
+ } else if (*i == '[') {
+ state = STATE_BRACKET;
+ begin = i + 1;
+ } else {
+ fputc('\x1B', f);
+ fputc(*i, f);
+ state = STATE_OTHER;
+ }
- assert(from);
- assert(to);
+ break;
- t = tempfn_random(to);
- if (!t)
- return -ENOMEM;
+ case STATE_BRACKET:
- if (symlink(from, t) < 0)
- return -errno;
+ if (i >= *ibuf + isz || /* EOT */
+ (!(*i >= '0' && *i <= '9') && *i != ';' && *i != 'm')) {
+ fputc('\x1B', f);
+ fputc('[', f);
+ state = STATE_OTHER;
+ i = begin-1;
+ } else if (*i == 'm')
+ state = STATE_OTHER;
+ break;
+ }
+ }
- if (rename(t, to) < 0) {
- unlink_noerrno(t);
- return -errno;
+ if (ferror(f)) {
+ fclose(f);
+ free(obuf);
+ return NULL;
}
- return 0;
-}
-
-int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
- _cleanup_free_ char *t = NULL;
-
- assert(path);
-
- t = tempfn_random(path);
- if (!t)
- return -ENOMEM;
+ fclose(f);
- if (mknod(t, mode, dev) < 0)
- return -errno;
+ free(*ibuf);
+ *ibuf = obuf;
- if (rename(t, path) < 0) {
- unlink_noerrno(t);
- return -errno;
- }
+ if (_isz)
+ *_isz = osz;
- return 0;
+ return obuf;
}
-int mkfifo_atomic(const char *path, mode_t mode) {
- _cleanup_free_ char *t = NULL;
-
- assert(path);
+int on_ac_power(void) {
+ bool found_offline = false, found_online = false;
+ _cleanup_closedir_ DIR *d = NULL;
- t = tempfn_random(path);
- if (!t)
- return -ENOMEM;
+ d = opendir("/sys/class/power_supply");
+ if (!d)
+ return errno == ENOENT ? true : -errno;
- if (mkfifo(t, mode) < 0)
- return -errno;
+ for (;;) {
+ struct dirent *de;
+ _cleanup_close_ int fd = -1, device = -1;
+ char contents[6];
+ ssize_t n;
- if (rename(t, path) < 0) {
- unlink_noerrno(t);
- return -errno;
- }
+ errno = 0;
+ de = readdir(d);
+ if (!de && errno != 0)
+ return -errno;
- return 0;
-}
+ if (!de)
+ break;
-bool display_is_local(const char *display) {
- assert(display);
+ if (hidden_file(de->d_name))
+ continue;
- return
- display[0] == ':' &&
- display[1] >= '0' &&
- display[1] <= '9';
-}
+ device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
+ if (device < 0) {
+ if (errno == ENOENT || errno == ENOTDIR)
+ continue;
-int socket_from_display(const char *display, char **path) {
- size_t k;
- char *f, *c;
+ return -errno;
+ }
- assert(display);
- assert(path);
+ fd = openat(device, "type", O_RDONLY|O_CLOEXEC|O_NOCTTY);
+ if (fd < 0) {
+ if (errno == ENOENT)
+ continue;
- if (!display_is_local(display))
- return -EINVAL;
+ return -errno;
+ }
- k = strspn(display+1, "0123456789");
+ n = read(fd, contents, sizeof(contents));
+ if (n < 0)
+ return -errno;
- f = new(char, strlen("/tmp/.X11-unix/X") + k + 1);
- if (!f)
- return -ENOMEM;
+ if (n != 6 || memcmp(contents, "Mains\n", 6))
+ continue;
- c = stpcpy(f, "/tmp/.X11-unix/X");
- memcpy(c, display+1, k);
- c[k] = 0;
+ safe_close(fd);
+ fd = openat(device, "online", O_RDONLY|O_CLOEXEC|O_NOCTTY);
+ if (fd < 0) {
+ if (errno == ENOENT)
+ continue;
- *path = f;
+ return -errno;
+ }
- return 0;
-}
+ n = read(fd, contents, sizeof(contents));
+ if (n < 0)
+ return -errno;
-int get_user_creds(
- const char **username,
- uid_t *uid, gid_t *gid,
- const char **home,
- const char **shell) {
+ if (n != 2 || contents[1] != '\n')
+ return -EIO;
- struct passwd *p;
- uid_t u;
+ if (contents[0] == '1') {
+ found_online = true;
+ break;
+ } else if (contents[0] == '0')
+ found_offline = true;
+ else
+ return -EIO;
+ }
- assert(username);
- assert(*username);
+ return found_online || !found_offline;
+}
- /* We enforce some special rules for uid=0: in order to avoid
- * NSS lookups for root we hardcode its data. */
+static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
+ char **i;
- if (streq(*username, "root") || streq(*username, "0")) {
- *username = "root";
+ assert(path);
+ assert(mode);
+ assert(_f);
- if (uid)
- *uid = 0;
+ if (!path_strv_resolve_uniq(search, root))
+ return -ENOMEM;
- if (gid)
- *gid = 0;
+ STRV_FOREACH(i, search) {
+ _cleanup_free_ char *p = NULL;
+ FILE *f;
- if (home)
- *home = "/root";
+ if (root)
+ p = strjoin(root, *i, "/", path, NULL);
+ else
+ p = strjoin(*i, "/", path, NULL);
+ if (!p)
+ return -ENOMEM;
- if (shell)
- *shell = "/bin/sh";
+ f = fopen(p, mode);
+ if (f) {
+ *_f = f;
+ return 0;
+ }
- return 0;
+ if (errno != ENOENT)
+ return -errno;
}
- if (parse_uid(*username, &u) >= 0) {
- errno = 0;
- p = getpwuid(u);
+ return -ENOENT;
+}
- /* If there are multiple users with the same id, make
- * sure to leave $USER to the configured value instead
- * of the first occurrence in the database. However if
- * the uid was configured by a numeric uid, then let's
- * pick the real username from /etc/passwd. */
- if (p)
- *username = p->pw_name;
- } else {
- errno = 0;
- p = getpwnam(*username);
- }
+int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
+ _cleanup_strv_free_ char **copy = NULL;
- if (!p)
- return errno > 0 ? -errno : -ESRCH;
+ assert(path);
+ assert(mode);
+ assert(_f);
- if (uid)
- *uid = p->pw_uid;
+ if (path_is_absolute(path)) {
+ FILE *f;
- if (gid)
- *gid = p->pw_gid;
+ f = fopen(path, mode);
+ if (f) {
+ *_f = f;
+ return 0;
+ }
- if (home)
- *home = p->pw_dir;
+ return -errno;
+ }
- if (shell)
- *shell = p->pw_shell;
+ copy = strv_copy((char**) search);
+ if (!copy)
+ return -ENOMEM;
- return 0;
+ return search_and_fopen_internal(path, mode, root, copy, _f);
}
-char* uid_to_name(uid_t uid) {
- struct passwd *p;
- char *r;
+int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
+ _cleanup_strv_free_ char **s = NULL;
- if (uid == 0)
- return strdup("root");
+ if (path_is_absolute(path)) {
+ FILE *f;
- p = getpwuid(uid);
- if (p)
- return strdup(p->pw_name);
+ f = fopen(path, mode);
+ if (f) {
+ *_f = f;
+ return 0;
+ }
- if (asprintf(&r, UID_FMT, uid) < 0)
- return NULL;
+ return -errno;
+ }
- return r;
+ s = strv_split_nulstr(search);
+ if (!s)
+ return -ENOMEM;
+
+ return search_and_fopen_internal(path, mode, root, s, _f);
}
-char* gid_to_name(gid_t gid) {
- struct group *p;
- char *r;
+char *strextend(char **x, ...) {
+ va_list ap;
+ size_t f, l;
+ char *r, *p;
- if (gid == 0)
- return strdup("root");
+ assert(x);
- p = getgrgid(gid);
- if (p)
- return strdup(p->gr_name);
+ l = f = *x ? strlen(*x) : 0;
- if (asprintf(&r, GID_FMT, gid) < 0)
- return NULL;
-
- return r;
-}
+ va_start(ap, x);
+ for (;;) {
+ const char *t;
+ size_t n;
-int get_group_creds(const char **groupname, gid_t *gid) {
- struct group *g;
- gid_t id;
+ t = va_arg(ap, const char *);
+ if (!t)
+ break;
- assert(groupname);
+ n = strlen(t);
+ if (n > ((size_t) -1) - l) {
+ va_end(ap);
+ return NULL;
+ }
- /* We enforce some special rules for gid=0: in order to avoid
- * NSS lookups for root we hardcode its data. */
+ l += n;
+ }
+ va_end(ap);
- if (streq(*groupname, "root") || streq(*groupname, "0")) {
- *groupname = "root";
+ r = realloc(*x, l+1);
+ if (!r)
+ return NULL;
- if (gid)
- *gid = 0;
+ p = r + f;
- return 0;
- }
+ va_start(ap, x);
+ for (;;) {
+ const char *t;
- if (parse_gid(*groupname, &id) >= 0) {
- errno = 0;
- g = getgrgid(id);
+ t = va_arg(ap, const char *);
+ if (!t)
+ break;
- if (g)
- *groupname = g->gr_name;
- } else {
- errno = 0;
- g = getgrnam(*groupname);
+ p = stpcpy(p, t);
}
+ va_end(ap);
- if (!g)
- return errno > 0 ? -errno : -ESRCH;
-
- if (gid)
- *gid = g->gr_gid;
+ *p = 0;
+ *x = r;
- return 0;
+ return r + l;
}
-int in_gid(gid_t gid) {
- gid_t *gids;
- int ngroups_max, r, i;
-
- if (getgid() == gid)
- return 1;
-
- if (getegid() == gid)
- return 1;
-
- ngroups_max = sysconf(_SC_NGROUPS_MAX);
- assert(ngroups_max > 0);
+char *strrep(const char *s, unsigned n) {
+ size_t l;
+ char *r, *p;
+ unsigned i;
- gids = alloca(sizeof(gid_t) * ngroups_max);
+ assert(s);
- r = getgroups(ngroups_max, gids);
- if (r < 0)
- return -errno;
+ l = strlen(s);
+ p = r = malloc(l * n + 1);
+ if (!r)
+ return NULL;
- for (i = 0; i < r; i++)
- if (gids[i] == gid)
- return 1;
+ for (i = 0; i < n; i++)
+ p = stpcpy(p, s);
- return 0;
+ *p = 0;
+ return r;
}
-int in_group(const char *name) {
- int r;
- gid_t gid;
+void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
+ size_t a, newalloc;
+ void *q;
- r = get_group_creds(&name, &gid);
- if (r < 0)
- return r;
+ assert(p);
+ assert(allocated);
- return in_gid(gid);
-}
+ if (*allocated >= need)
+ return *p;
-int glob_exists(const char *path) {
- _cleanup_globfree_ glob_t g = {};
- int k;
+ newalloc = MAX(need * 2, 64u / size);
+ a = newalloc * size;
- assert(path);
+ /* check for overflows */
+ if (a < size * need)
+ return NULL;
- errno = 0;
- k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
+ q = realloc(*p, a);
+ if (!q)
+ return NULL;
- if (k == GLOB_NOMATCH)
- return 0;
- else if (k == GLOB_NOSPACE)
- return -ENOMEM;
- else if (k == 0)
- return !strv_isempty(g.gl_pathv);
- else
- return errno ? -errno : -EIO;
+ *p = q;
+ *allocated = newalloc;
+ return q;
}
-int glob_extend(char ***strv, const char *path) {
- _cleanup_globfree_ glob_t g = {};
- int k;
- char **p;
+void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) {
+ size_t prev;
+ uint8_t *q;
- errno = 0;
- k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
+ assert(p);
+ assert(allocated);
- if (k == GLOB_NOMATCH)
- return -ENOENT;
- else if (k == GLOB_NOSPACE)
- return -ENOMEM;
- else if (k != 0 || strv_isempty(g.gl_pathv))
- return errno ? -errno : -EIO;
+ prev = *allocated;
- STRV_FOREACH(p, g.gl_pathv) {
- k = strv_extend(strv, *p);
- if (k < 0)
- break;
- }
+ q = greedy_realloc(p, allocated, need, size);
+ if (!q)
+ return NULL;
- return k;
+ if (*allocated > prev)
+ memzero(q + prev * size, (*allocated - prev) * size);
+
+ return q;
}
-int dirent_ensure_type(DIR *d, struct dirent *de) {
- struct stat st;
+bool id128_is_valid(const char *s) {
+ size_t i, l;
- assert(d);
- assert(de);
+ l = strlen(s);
+ if (l == 32) {
- if (de->d_type != DT_UNKNOWN)
- return 0;
+ /* Simple formatted 128bit hex string */
- if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
- return -errno;
+ for (i = 0; i < l; i++) {
+ char c = s[i];
- de->d_type =
- S_ISREG(st.st_mode) ? DT_REG :
- S_ISDIR(st.st_mode) ? DT_DIR :
- S_ISLNK(st.st_mode) ? DT_LNK :
- S_ISFIFO(st.st_mode) ? DT_FIFO :
- S_ISSOCK(st.st_mode) ? DT_SOCK :
- S_ISCHR(st.st_mode) ? DT_CHR :
- S_ISBLK(st.st_mode) ? DT_BLK :
- DT_UNKNOWN;
+ if (!(c >= '0' && c <= '9') &&
+ !(c >= 'a' && c <= 'z') &&
+ !(c >= 'A' && c <= 'Z'))
+ return false;
+ }
- return 0;
-}
+ } else if (l == 36) {
-int get_files_in_directory(const char *path, char ***list) {
- _cleanup_closedir_ DIR *d = NULL;
- size_t bufsize = 0, n = 0;
- _cleanup_strv_free_ char **l = NULL;
+ /* Formatted UUID */
- assert(path);
+ for (i = 0; i < l; i++) {
+ char c = s[i];
- /* Returns all files in a directory in *list, and the number
- * of files as return value. If list is NULL returns only the
- * number. */
+ if ((i == 8 || i == 13 || i == 18 || i == 23)) {
+ if (c != '-')
+ return false;
+ } else {
+ if (!(c >= '0' && c <= '9') &&
+ !(c >= 'a' && c <= 'z') &&
+ !(c >= 'A' && c <= 'Z'))
+ return false;
+ }
+ }
- d = opendir(path);
- if (!d)
- return -errno;
+ } else
+ return false;
- for (;;) {
- struct dirent *de;
+ return true;
+}
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0)
- return -errno;
- if (!de)
- break;
+int split_pair(const char *s, const char *sep, char **l, char **r) {
+ char *x, *a, *b;
- dirent_ensure_type(d, de);
+ assert(s);
+ assert(sep);
+ assert(l);
+ assert(r);
- if (!dirent_is_file(de))
- continue;
+ if (isempty(sep))
+ return -EINVAL;
- if (list) {
- /* one extra slot is needed for the terminating NULL */
- if (!GREEDY_REALLOC(l, bufsize, n + 2))
- return -ENOMEM;
+ x = strstr(s, sep);
+ if (!x)
+ return -EINVAL;
- l[n] = strdup(de->d_name);
- if (!l[n])
- return -ENOMEM;
+ a = strndup(s, x - s);
+ if (!a)
+ return -ENOMEM;
- l[++n] = NULL;
- } else
- n++;
+ b = strdup(x + strlen(sep));
+ if (!b) {
+ free(a);
+ return -ENOMEM;
}
- if (list) {
- *list = l;
- l = NULL; /* avoid freeing */
- }
+ *l = a;
+ *r = b;
- return n;
+ return 0;
}
-char *strjoin(const char *x, ...) {
- va_list ap;
- size_t l;
- char *r, *p;
+int shall_restore_state(void) {
+ _cleanup_free_ char *value = NULL;
+ int r;
- va_start(ap, x);
+ r = get_proc_cmdline_key("systemd.restore_state=", &value);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return true;
- if (x) {
- l = strlen(x);
+ return parse_boolean(value) != 0;
+}
- for (;;) {
- const char *t;
- size_t n;
+int proc_cmdline(char **ret) {
+ assert(ret);
- t = va_arg(ap, const char *);
- if (!t)
- break;
+ if (detect_container(NULL) > 0)
+ return get_process_cmdline(1, 0, false, ret);
+ else
+ return read_one_line_file("/proc/cmdline", ret);
+}
- n = strlen(t);
- if (n > ((size_t) -1) - l) {
- va_end(ap);
- return NULL;
- }
+int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value)) {
+ _cleanup_free_ char *line = NULL;
+ const char *p;
+ int r;
- l += n;
- }
- } else
- l = 0;
+ assert(parse_item);
- va_end(ap);
+ r = proc_cmdline(&line);
+ if (r < 0)
+ return r;
- r = new(char, l+1);
- if (!r)
- return NULL;
+ p = line;
+ for (;;) {
+ _cleanup_free_ char *word = NULL;
+ char *value = NULL;
- if (x) {
- p = stpcpy(r, x);
+ r = unquote_first_word(&p, &word, UNQUOTE_RELAX);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
- va_start(ap, x);
+ /* Filter out arguments that are intended only for the
+ * initrd */
+ if (!in_initrd() && startswith(word, "rd."))
+ continue;
- for (;;) {
- const char *t;
+ value = strchr(word, '=');
+ if (value)
+ *(value++) = 0;
- t = va_arg(ap, const char *);
- if (!t)
- break;
+ r = parse_item(word, value);
+ if (r < 0)
+ return r;
+ }
- p = stpcpy(p, t);
- }
+ return 0;
+}
- va_end(ap);
- } else
- r[0] = 0;
+int get_proc_cmdline_key(const char *key, char **value) {
+ _cleanup_free_ char *line = NULL, *ret = NULL;
+ bool found = false;
+ const char *p;
+ int r;
- return r;
-}
+ assert(key);
-bool is_main_thread(void) {
- static thread_local int cached = 0;
+ r = proc_cmdline(&line);
+ if (r < 0)
+ return r;
- if (_unlikely_(cached == 0))
- cached = getpid() == gettid() ? 1 : -1;
+ p = line;
+ for (;;) {
+ _cleanup_free_ char *word = NULL;
+ const char *e;
- return cached > 0;
-}
+ r = unquote_first_word(&p, &word, UNQUOTE_RELAX);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
-int block_get_whole_disk(dev_t d, dev_t *ret) {
- char *p, *s;
- int r;
- unsigned n, m;
+ /* Filter out arguments that are intended only for the
+ * initrd */
+ if (!in_initrd() && startswith(word, "rd."))
+ continue;
- assert(ret);
+ if (value) {
+ e = startswith(word, key);
+ if (!e)
+ continue;
- /* If it has a queue this is good enough for us */
- if (asprintf(&p, "/sys/dev/block/%u:%u/queue", major(d), minor(d)) < 0)
- return -ENOMEM;
+ r = free_and_strdup(&ret, e);
+ if (r < 0)
+ return r;
- r = access(p, F_OK);
- free(p);
+ found = true;
+ } else {
+ if (streq(word, key))
+ found = true;
+ }
+ }
- if (r >= 0) {
- *ret = d;
- return 0;
+ if (value) {
+ *value = ret;
+ ret = NULL;
}
- /* If it is a partition find the originating device */
- if (asprintf(&p, "/sys/dev/block/%u:%u/partition", major(d), minor(d)) < 0)
- return -ENOMEM;
+ return found;
- r = access(p, F_OK);
- free(p);
+}
- if (r < 0)
- return -ENOENT;
+int container_get_leader(const char *machine, pid_t *pid) {
+ _cleanup_free_ char *s = NULL, *class = NULL;
+ const char *p;
+ pid_t leader;
+ int r;
- /* Get parent dev_t */
- if (asprintf(&p, "/sys/dev/block/%u:%u/../dev", major(d), minor(d)) < 0)
- return -ENOMEM;
+ assert(machine);
+ assert(pid);
- r = read_one_line_file(p, &s);
- free(p);
+ p = strjoina("/run/systemd/machines/", machine);
+ r = parse_env_file(p, NEWLINE, "LEADER", &s, "CLASS", &class, NULL);
+ if (r == -ENOENT)
+ return -EHOSTDOWN;
+ if (r < 0)
+ return r;
+ if (!s)
+ return -EIO;
+
+ if (!streq_ptr(class, "container"))
+ return -EIO;
+ r = parse_pid(s, &leader);
if (r < 0)
return r;
+ if (leader <= 1)
+ return -EIO;
- r = sscanf(s, "%u:%u", &m, &n);
- free(s);
+ *pid = leader;
+ return 0;
+}
- if (r != 2)
- return -EINVAL;
+int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd) {
+ _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, netnsfd = -1;
+ int rfd = -1;
- /* Only return this if it is really good enough for us. */
- if (asprintf(&p, "/sys/dev/block/%u:%u/queue", m, n) < 0)
- return -ENOMEM;
+ assert(pid >= 0);
- r = access(p, F_OK);
- free(p);
+ if (mntns_fd) {
+ const char *mntns;
- if (r >= 0) {
- *ret = makedev(m, n);
- return 0;
+ mntns = procfs_file_alloca(pid, "ns/mnt");
+ mntnsfd = open(mntns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+ if (mntnsfd < 0)
+ return -errno;
}
- return -ENOENT;
-}
+ if (pidns_fd) {
+ const char *pidns;
-static const char *const ioprio_class_table[] = {
- [IOPRIO_CLASS_NONE] = "none",
- [IOPRIO_CLASS_RT] = "realtime",
- [IOPRIO_CLASS_BE] = "best-effort",
- [IOPRIO_CLASS_IDLE] = "idle"
-};
+ pidns = procfs_file_alloca(pid, "ns/pid");
+ pidnsfd = open(pidns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+ if (pidnsfd < 0)
+ return -errno;
+ }
-DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, INT_MAX);
+ if (netns_fd) {
+ const char *netns;
-static const char *const sigchld_code_table[] = {
- [CLD_EXITED] = "exited",
- [CLD_KILLED] = "killed",
- [CLD_DUMPED] = "dumped",
- [CLD_TRAPPED] = "trapped",
- [CLD_STOPPED] = "stopped",
- [CLD_CONTINUED] = "continued",
-};
+ netns = procfs_file_alloca(pid, "ns/net");
+ netnsfd = open(netns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+ if (netnsfd < 0)
+ return -errno;
+ }
-DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
+ if (root_fd) {
+ const char *root;
-static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
- [LOG_FAC(LOG_KERN)] = "kern",
- [LOG_FAC(LOG_USER)] = "user",
- [LOG_FAC(LOG_MAIL)] = "mail",
- [LOG_FAC(LOG_DAEMON)] = "daemon",
- [LOG_FAC(LOG_AUTH)] = "auth",
- [LOG_FAC(LOG_SYSLOG)] = "syslog",
- [LOG_FAC(LOG_LPR)] = "lpr",
- [LOG_FAC(LOG_NEWS)] = "news",
- [LOG_FAC(LOG_UUCP)] = "uucp",
- [LOG_FAC(LOG_CRON)] = "cron",
- [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
- [LOG_FAC(LOG_FTP)] = "ftp",
- [LOG_FAC(LOG_LOCAL0)] = "local0",
- [LOG_FAC(LOG_LOCAL1)] = "local1",
- [LOG_FAC(LOG_LOCAL2)] = "local2",
- [LOG_FAC(LOG_LOCAL3)] = "local3",
- [LOG_FAC(LOG_LOCAL4)] = "local4",
- [LOG_FAC(LOG_LOCAL5)] = "local5",
- [LOG_FAC(LOG_LOCAL6)] = "local6",
- [LOG_FAC(LOG_LOCAL7)] = "local7"
-};
-
-DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
-
-static const char *const log_level_table[] = {
- [LOG_EMERG] = "emerg",
- [LOG_ALERT] = "alert",
- [LOG_CRIT] = "crit",
- [LOG_ERR] = "err",
- [LOG_WARNING] = "warning",
- [LOG_NOTICE] = "notice",
- [LOG_INFO] = "info",
- [LOG_DEBUG] = "debug"
-};
-
-DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
-
-static const char* const sched_policy_table[] = {
- [SCHED_OTHER] = "other",
- [SCHED_BATCH] = "batch",
- [SCHED_IDLE] = "idle",
- [SCHED_FIFO] = "fifo",
- [SCHED_RR] = "rr"
-};
-
-DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX);
-
-static const char* const rlimit_table[_RLIMIT_MAX] = {
- [RLIMIT_CPU] = "LimitCPU",
- [RLIMIT_FSIZE] = "LimitFSIZE",
- [RLIMIT_DATA] = "LimitDATA",
- [RLIMIT_STACK] = "LimitSTACK",
- [RLIMIT_CORE] = "LimitCORE",
- [RLIMIT_RSS] = "LimitRSS",
- [RLIMIT_NOFILE] = "LimitNOFILE",
- [RLIMIT_AS] = "LimitAS",
- [RLIMIT_NPROC] = "LimitNPROC",
- [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
- [RLIMIT_LOCKS] = "LimitLOCKS",
- [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
- [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
- [RLIMIT_NICE] = "LimitNICE",
- [RLIMIT_RTPRIO] = "LimitRTPRIO",
- [RLIMIT_RTTIME] = "LimitRTTIME"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
-
-static const char* const ip_tos_table[] = {
- [IPTOS_LOWDELAY] = "low-delay",
- [IPTOS_THROUGHPUT] = "throughput",
- [IPTOS_RELIABILITY] = "reliability",
- [IPTOS_LOWCOST] = "low-cost",
-};
-
-DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
-
-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",
-#ifdef SIGSTKFLT
- [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */
-#endif
- [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_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
-
-const char *signal_to_string(int signo) {
- static thread_local char buf[sizeof("RTMIN+")-1 + DECIMAL_STR_MAX(int) + 1];
- const char *name;
-
- name = __signal_to_string(signo);
- if (name)
- return name;
-
- if (signo >= SIGRTMIN && signo <= SIGRTMAX)
- snprintf(buf, sizeof(buf), "RTMIN+%d", signo - SIGRTMIN);
- else
- snprintf(buf, sizeof(buf), "%d", signo);
-
- return buf;
-}
-
-int signal_from_string(const char *s) {
- int signo;
- int offset = 0;
- unsigned u;
-
- signo = __signal_from_string(s);
- if (signo > 0)
- return signo;
-
- if (startswith(s, "RTMIN+")) {
- s += 6;
- offset = SIGRTMIN;
- }
- if (safe_atou(s, &u) >= 0) {
- signo = (int) u + offset;
- if (signo > 0 && signo < _NSIG)
- return signo;
- }
- return -EINVAL;
-}
-
-bool kexec_loaded(void) {
- bool loaded = false;
- char *s;
-
- if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) {
- if (s[0] == '1')
- loaded = true;
- free(s);
- }
- return loaded;
-}
-
-int prot_from_flags(int flags) {
-
- switch (flags & O_ACCMODE) {
-
- case O_RDONLY:
- return PROT_READ;
-
- case O_WRONLY:
- return PROT_WRITE;
-
- case O_RDWR:
- return PROT_READ|PROT_WRITE;
-
- default:
- return -EINVAL;
- }
-}
-
-char *format_bytes(char *buf, size_t l, off_t t) {
- unsigned i;
-
- static const struct {
- const char *suffix;
- off_t factor;
- } table[] = {
- { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
- { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
- { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
- { "G", 1024ULL*1024ULL*1024ULL },
- { "M", 1024ULL*1024ULL },
- { "K", 1024ULL },
- };
-
- for (i = 0; i < ELEMENTSOF(table); i++) {
-
- if (t >= table[i].factor) {
- snprintf(buf, l,
- "%llu.%llu%s",
- (unsigned long long) (t / table[i].factor),
- (unsigned long long) (((t*10ULL) / table[i].factor) % 10ULL),
- table[i].suffix);
-
- goto finish;
- }
- }
-
- snprintf(buf, l, "%lluB", (unsigned long long) t);
-
-finish:
- buf[l-1] = 0;
- return buf;
-
-}
-
-void* memdup(const void *p, size_t l) {
- void *r;
-
- assert(p);
-
- r = malloc(l);
- if (!r)
- return NULL;
-
- memcpy(r, p, l);
- return r;
-}
-
-int fd_inc_sndbuf(int fd, size_t n) {
- int r, value;
- socklen_t l = sizeof(value);
-
- r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
- if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
- return 0;
-
- /* If we have the privileges we will ignore the kernel limit. */
-
- value = (int) n;
- if (setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &value, sizeof(value)) < 0)
- if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0)
- return -errno;
-
- return 1;
-}
-
-int fd_inc_rcvbuf(int fd, size_t n) {
- int r, value;
- socklen_t l = sizeof(value);
-
- r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
- if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
- return 0;
-
- /* If we have the privileges we will ignore the kernel limit. */
-
- value = (int) n;
- if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &value, sizeof(value)) < 0)
- if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0)
- return -errno;
- return 1;
-}
-
-int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) {
- bool stdout_is_tty, stderr_is_tty;
- pid_t parent_pid, agent_pid;
- sigset_t ss, saved_ss;
- unsigned n, i;
- va_list ap;
- char **l;
-
- assert(pid);
- assert(path);
-
- /* Spawns a temporary TTY agent, making sure it goes away when
- * we go away */
-
- parent_pid = getpid();
-
- /* First we temporarily block all signals, so that the new
- * child has them blocked initially. This way, we can be sure
- * that SIGTERMs are not lost we might send to the agent. */
- assert_se(sigfillset(&ss) >= 0);
- assert_se(sigprocmask(SIG_SETMASK, &ss, &saved_ss) >= 0);
-
- agent_pid = fork();
- if (agent_pid < 0) {
- assert_se(sigprocmask(SIG_SETMASK, &saved_ss, NULL) >= 0);
- return -errno;
- }
-
- if (agent_pid != 0) {
- assert_se(sigprocmask(SIG_SETMASK, &saved_ss, NULL) >= 0);
- *pid = agent_pid;
- return 0;
- }
-
- /* In the child:
- *
- * Make sure the agent goes away when the parent dies */
- if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
- _exit(EXIT_FAILURE);
-
- /* Make sure we actually can kill the agent, if we need to, in
- * case somebody invoked us from a shell script that trapped
- * SIGTERM or so... */
- reset_all_signal_handlers();
- reset_signal_mask();
-
- /* Check whether our parent died before we were able
- * to set the death signal and unblock the signals */
- if (getppid() != parent_pid)
- _exit(EXIT_SUCCESS);
-
- /* Don't leak fds to the agent */
- close_all_fds(except, n_except);
-
- stdout_is_tty = isatty(STDOUT_FILENO);
- stderr_is_tty = isatty(STDERR_FILENO);
-
- if (!stdout_is_tty || !stderr_is_tty) {
- int fd;
-
- /* Detach from stdout/stderr. and reopen
- * /dev/tty for them. This is important to
- * ensure that when systemctl is started via
- * popen() or a similar call that expects to
- * read EOF we actually do generate EOF and
- * not delay this indefinitely by because we
- * keep an unused copy of stdin around. */
- fd = open("/dev/tty", O_WRONLY);
- if (fd < 0) {
- log_error_errno(errno, "Failed to open /dev/tty: %m");
- _exit(EXIT_FAILURE);
- }
-
- if (!stdout_is_tty)
- dup2(fd, STDOUT_FILENO);
-
- if (!stderr_is_tty)
- dup2(fd, STDERR_FILENO);
-
- if (fd > 2)
- close(fd);
- }
-
- /* Count arguments */
- va_start(ap, path);
- for (n = 0; va_arg(ap, char*); n++)
- ;
- va_end(ap);
-
- /* Allocate strv */
- l = alloca(sizeof(char *) * (n + 1));
-
- /* Fill in arguments */
- va_start(ap, path);
- for (i = 0; i <= n; i++)
- l[i] = va_arg(ap, char*);
- va_end(ap);
-
- execv(path, l);
- _exit(EXIT_FAILURE);
-}
-
-int setrlimit_closest(int resource, const struct rlimit *rlim) {
- struct rlimit highest, fixed;
-
- assert(rlim);
-
- if (setrlimit(resource, rlim) >= 0)
- return 0;
-
- if (errno != EPERM)
- return -errno;
-
- /* So we failed to set the desired setrlimit, then let's try
- * to get as close as we can */
- assert_se(getrlimit(resource, &highest) == 0);
-
- fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
- fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
-
- if (setrlimit(resource, &fixed) < 0)
- return -errno;
-
- return 0;
-}
-
-int getenv_for_pid(pid_t pid, const char *field, char **_value) {
- _cleanup_fclose_ FILE *f = NULL;
- char *value = NULL;
- int r;
- bool done = false;
- size_t l;
- const char *path;
-
- assert(pid >= 0);
- assert(field);
- assert(_value);
-
- path = procfs_file_alloca(pid, "environ");
-
- f = fopen(path, "re");
- if (!f)
- return -errno;
-
- l = strlen(field);
- r = 0;
-
- do {
- char line[LINE_MAX];
- unsigned i;
-
- for (i = 0; i < sizeof(line)-1; i++) {
- int c;
-
- c = getc(f);
- if (_unlikely_(c == EOF)) {
- done = true;
- break;
- } else if (c == 0)
- break;
-
- line[i] = c;
- }
- line[i] = 0;
-
- if (memcmp(line, field, l) == 0 && line[l] == '=') {
- value = strdup(line + l + 1);
- if (!value)
- return -ENOMEM;
-
- r = 1;
- break;
- }
-
- } while (!done);
-
- *_value = value;
- return r;
-}
-
-bool is_valid_documentation_url(const char *url) {
- assert(url);
-
- if (startswith(url, "http://") && url[7])
- return true;
-
- if (startswith(url, "https://") && url[8])
- return true;
-
- if (startswith(url, "file:") && url[5])
- return true;
-
- if (startswith(url, "info:") && url[5])
- return true;
-
- if (startswith(url, "man:") && url[4])
- return true;
-
- return false;
-}
-
-bool in_initrd(void) {
- static int saved = -1;
- struct statfs s;
-
- if (saved >= 0)
- return saved;
-
- /* We make two checks here:
- *
- * 1. the flag file /etc/initrd-release must exist
- * 2. the root file system must be a memory file system
- *
- * The second check is extra paranoia, since misdetecting an
- * initrd can have bad bad consequences due the initrd
- * emptying when transititioning to the main systemd.
- */
-
- saved = access("/etc/initrd-release", F_OK) >= 0 &&
- statfs("/", &s) >= 0 &&
- is_temporary_fs(&s);
-
- return saved;
-}
-
-void warn_melody(void) {
- _cleanup_close_ int fd = -1;
-
- fd = open("/dev/console", O_WRONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0)
- return;
-
- /* Yeah, this is synchronous. Kinda sucks. But well... */
-
- ioctl(fd, KIOCSOUND, (int)(1193180/440));
- usleep(125*USEC_PER_MSEC);
-
- ioctl(fd, KIOCSOUND, (int)(1193180/220));
- usleep(125*USEC_PER_MSEC);
-
- ioctl(fd, KIOCSOUND, (int)(1193180/220));
- usleep(125*USEC_PER_MSEC);
-
- ioctl(fd, KIOCSOUND, 0);
-}
-
-int make_console_stdio(void) {
- int fd, r;
-
- /* Make /dev/console the controlling terminal and stdin/stdout/stderr */
-
- fd = acquire_terminal("/dev/console", false, true, true, USEC_INFINITY);
- if (fd < 0)
- return log_error_errno(fd, "Failed to acquire terminal: %m");
-
- r = make_stdio(fd);
- if (r < 0)
- return log_error_errno(r, "Failed to duplicate terminal fd: %m");
-
- return 0;
-}
-
-int get_home_dir(char **_h) {
- struct passwd *p;
- const char *e;
- char *h;
- uid_t u;
-
- assert(_h);
-
- /* Take the user specified one */
- e = secure_getenv("HOME");
- if (e && path_is_absolute(e)) {
- h = strdup(e);
- if (!h)
- return -ENOMEM;
-
- *_h = h;
- return 0;
- }
-
- /* Hardcode home directory for root to avoid NSS */
- u = getuid();
- if (u == 0) {
- h = strdup("/root");
- if (!h)
- return -ENOMEM;
-
- *_h = h;
- return 0;
- }
-
- /* Check the database... */
- errno = 0;
- p = getpwuid(u);
- if (!p)
- return errno > 0 ? -errno : -ESRCH;
-
- if (!path_is_absolute(p->pw_dir))
- return -EINVAL;
-
- h = strdup(p->pw_dir);
- if (!h)
- return -ENOMEM;
-
- *_h = h;
- return 0;
-}
-
-int get_shell(char **_s) {
- struct passwd *p;
- const char *e;
- char *s;
- uid_t u;
-
- assert(_s);
-
- /* Take the user specified one */
- e = getenv("SHELL");
- if (e) {
- s = strdup(e);
- if (!s)
- return -ENOMEM;
-
- *_s = s;
- return 0;
- }
-
- /* Hardcode home directory for root to avoid NSS */
- u = getuid();
- if (u == 0) {
- s = strdup("/bin/sh");
- if (!s)
- return -ENOMEM;
-
- *_s = s;
- return 0;
- }
-
- /* Check the database... */
- errno = 0;
- p = getpwuid(u);
- if (!p)
- return errno > 0 ? -errno : -ESRCH;
-
- if (!path_is_absolute(p->pw_shell))
- return -EINVAL;
-
- s = strdup(p->pw_shell);
- if (!s)
- return -ENOMEM;
-
- *_s = s;
- return 0;
-}
-
-bool filename_is_safe(const char *p) {
-
- if (isempty(p))
- return false;
-
- if (strchr(p, '/'))
- return false;
-
- if (streq(p, "."))
- return false;
-
- if (streq(p, ".."))
- return false;
-
- if (strlen(p) > FILENAME_MAX)
- return false;
-
- return true;
-}
-
-bool string_is_safe(const char *p) {
- const char *t;
-
- if (!p)
- return false;
-
- for (t = p; *t; t++) {
- if (*t > 0 && *t < ' ')
- return false;
-
- if (strchr("\\\"\'\0x7f", *t))
- return false;
- }
-
- return true;
-}
-
-/**
- * Check if a string contains control characters. If 'ok' is non-NULL
- * it may be a string containing additional CCs to be considered OK.
- */
-bool string_has_cc(const char *p, const char *ok) {
- const char *t;
-
- assert(p);
-
- for (t = p; *t; t++) {
- if (ok && strchr(ok, *t))
- continue;
-
- if (*t > 0 && *t < ' ')
- return true;
-
- if (*t == 127)
- return true;
- }
-
- return false;
-}
-
-bool path_is_safe(const char *p) {
-
- if (isempty(p))
- return false;
-
- if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
- return false;
-
- if (strlen(p) > PATH_MAX)
- return false;
-
- /* The following two checks are not really dangerous, but hey, they still are confusing */
- if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
- return false;
-
- if (strstr(p, "//"))
- return false;
-
- return true;
-}
-
-/* hey glibc, APIs with callbacks without a user pointer are so useless */
-void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
- int (*compar) (const void *, const void *, void *), void *arg) {
- size_t l, u, idx;
- const void *p;
- int comparison;
-
- l = 0;
- u = nmemb;
- while (l < u) {
- idx = (l + u) / 2;
- p = (void *)(((const char *) base) + (idx * size));
- comparison = compar(key, p, arg);
- if (comparison < 0)
- u = idx;
- else if (comparison > 0)
- l = idx + 1;
- else
- return (void *)p;
- }
- return NULL;
-}
-
-bool is_locale_utf8(void) {
- const char *set;
- static int cached_answer = -1;
-
- if (cached_answer >= 0)
- goto out;
-
- if (!setlocale(LC_ALL, "")) {
- cached_answer = true;
- goto out;
- }
-
- set = nl_langinfo(CODESET);
- if (!set) {
- cached_answer = true;
- goto out;
- }
-
- if (streq(set, "UTF-8")) {
- cached_answer = true;
- goto out;
- }
-
- /* For LC_CTYPE=="C" return true, because CTYPE is effectly
- * unset and everything can do to UTF-8 nowadays. */
- set = setlocale(LC_CTYPE, NULL);
- if (!set) {
- cached_answer = true;
- goto out;
- }
-
- /* Check result, but ignore the result if C was set
- * explicitly. */
- cached_answer =
- streq(set, "C") &&
- !getenv("LC_ALL") &&
- !getenv("LC_CTYPE") &&
- !getenv("LANG");
-
-out:
- return (bool) cached_answer;
-}
-
-const char *draw_special_char(DrawSpecialChar ch) {
- static const char *draw_table[2][_DRAW_SPECIAL_CHAR_MAX] = {
-
- /* UTF-8 */ {
- [DRAW_TREE_VERTICAL] = "\342\224\202 ", /* │ */
- [DRAW_TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */
- [DRAW_TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */
- [DRAW_TREE_SPACE] = " ", /* */
- [DRAW_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */
- [DRAW_BLACK_CIRCLE] = "\342\227\217", /* ● */
- [DRAW_ARROW] = "\342\206\222", /* → */
- [DRAW_DASH] = "\342\200\223", /* – */
- },
-
- /* ASCII fallback */ {
- [DRAW_TREE_VERTICAL] = "| ",
- [DRAW_TREE_BRANCH] = "|-",
- [DRAW_TREE_RIGHT] = "`-",
- [DRAW_TREE_SPACE] = " ",
- [DRAW_TRIANGULAR_BULLET] = ">",
- [DRAW_BLACK_CIRCLE] = "*",
- [DRAW_ARROW] = "->",
- [DRAW_DASH] = "-",
- }
- };
-
- return draw_table[!is_locale_utf8()][ch];
-}
-
-char *strreplace(const char *text, const char *old_string, const char *new_string) {
- const char *f;
- char *t, *r;
- size_t l, old_len, new_len;
-
- assert(text);
- assert(old_string);
- assert(new_string);
-
- old_len = strlen(old_string);
- new_len = strlen(new_string);
-
- l = strlen(text);
- r = new(char, l+1);
- if (!r)
- return NULL;
-
- f = text;
- t = r;
- while (*f) {
- char *a;
- size_t d, nl;
-
- if (!startswith(f, old_string)) {
- *(t++) = *(f++);
- continue;
- }
-
- d = t - r;
- nl = l - old_len + new_len;
- a = realloc(r, nl + 1);
- if (!a)
- goto oom;
-
- l = nl;
- r = a;
- t = r + d;
-
- t = stpcpy(t, new_string);
- f += old_len;
+ root = procfs_file_alloca(pid, "root");
+ rfd = open(root, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
+ if (rfd < 0)
+ return -errno;
}
- *t = 0;
- return r;
-
-oom:
- free(r);
- return NULL;
-}
-
-char *strip_tab_ansi(char **ibuf, size_t *_isz) {
- const char *i, *begin = NULL;
- enum {
- STATE_OTHER,
- STATE_ESCAPE,
- STATE_BRACKET
- } state = STATE_OTHER;
- char *obuf = NULL;
- size_t osz = 0, isz;
- FILE *f;
-
- assert(ibuf);
- assert(*ibuf);
-
- /* Strips ANSI color and replaces TABs by 8 spaces */
-
- isz = _isz ? *_isz : strlen(*ibuf);
-
- f = open_memstream(&obuf, &osz);
- if (!f)
- return NULL;
-
- for (i = *ibuf; i < *ibuf + isz + 1; i++) {
-
- switch (state) {
-
- case STATE_OTHER:
- if (i >= *ibuf + isz) /* EOT */
- break;
- else if (*i == '\x1B')
- state = STATE_ESCAPE;
- else if (*i == '\t')
- fputs(" ", f);
- else
- fputc(*i, f);
- break;
-
- case STATE_ESCAPE:
- if (i >= *ibuf + isz) { /* EOT */
- fputc('\x1B', f);
- break;
- } else if (*i == '[') {
- state = STATE_BRACKET;
- begin = i + 1;
- } else {
- fputc('\x1B', f);
- fputc(*i, f);
- state = STATE_OTHER;
- }
-
- break;
-
- case STATE_BRACKET:
-
- if (i >= *ibuf + isz || /* EOT */
- (!(*i >= '0' && *i <= '9') && *i != ';' && *i != 'm')) {
- fputc('\x1B', f);
- fputc('[', f);
- state = STATE_OTHER;
- i = begin-1;
- } else if (*i == 'm')
- state = STATE_OTHER;
- break;
- }
- }
+ if (pidns_fd)
+ *pidns_fd = pidnsfd;
- if (ferror(f)) {
- fclose(f);
- free(obuf);
- return NULL;
- }
+ if (mntns_fd)
+ *mntns_fd = mntnsfd;
- fclose(f);
+ if (netns_fd)
+ *netns_fd = netnsfd;
- free(*ibuf);
- *ibuf = obuf;
+ if (root_fd)
+ *root_fd = rfd;
- if (_isz)
- *_isz = osz;
+ pidnsfd = mntnsfd = netnsfd = -1;
- return obuf;
+ return 0;
}
-int on_ac_power(void) {
- bool found_offline = false, found_online = false;
- _cleanup_closedir_ DIR *d = NULL;
-
- d = opendir("/sys/class/power_supply");
- if (!d)
- return -errno;
-
- for (;;) {
- struct dirent *de;
- _cleanup_close_ int fd = -1, device = -1;
- char contents[6];
- ssize_t n;
-
- errno = 0;
- de = readdir(d);
- if (!de && errno != 0)
- return -errno;
-
- if (!de)
- break;
-
- if (ignore_file(de->d_name))
- continue;
-
- device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
- if (device < 0) {
- if (errno == ENOENT || errno == ENOTDIR)
- continue;
-
- return -errno;
- }
-
- fd = openat(device, "type", O_RDONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0) {
- if (errno == ENOENT)
- continue;
+int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd) {
+ if (pidns_fd >= 0)
+ if (setns(pidns_fd, CLONE_NEWPID) < 0)
return -errno;
- }
- n = read(fd, contents, sizeof(contents));
- if (n < 0)
+ if (mntns_fd >= 0)
+ if (setns(mntns_fd, CLONE_NEWNS) < 0)
return -errno;
- if (n != 6 || memcmp(contents, "Mains\n", 6))
- continue;
-
- safe_close(fd);
- fd = openat(device, "online", O_RDONLY|O_CLOEXEC|O_NOCTTY);
- if (fd < 0) {
- if (errno == ENOENT)
- continue;
+ if (netns_fd >= 0)
+ if (setns(netns_fd, CLONE_NEWNET) < 0)
+ return -errno;
+ if (root_fd >= 0) {
+ if (fchdir(root_fd) < 0)
return -errno;
- }
- n = read(fd, contents, sizeof(contents));
- if (n < 0)
+ if (chroot(".") < 0)
return -errno;
+ }
- if (n != 2 || contents[1] != '\n')
- return -EIO;
+ if (setresgid(0, 0, 0) < 0)
+ return -errno;
- if (contents[0] == '1') {
- found_online = true;
- break;
- } else if (contents[0] == '0')
- found_offline = true;
- else
- return -EIO;
- }
+ if (setgroups(0, NULL) < 0)
+ return -errno;
- return found_online || !found_offline;
+ if (setresuid(0, 0, 0) < 0)
+ return -errno;
+
+ return 0;
}
-static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
- char **i;
+int getpeercred(int fd, struct ucred *ucred) {
+ socklen_t n = sizeof(struct ucred);
+ struct ucred u;
+ int r;
- assert(path);
- assert(mode);
- assert(_f);
+ assert(fd >= 0);
+ assert(ucred);
- if (!path_strv_resolve_uniq(search, root))
- return -ENOMEM;
+ r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n);
+ if (r < 0)
+ return -errno;
- STRV_FOREACH(i, search) {
- _cleanup_free_ char *p = NULL;
- FILE *f;
+ if (n != sizeof(struct ucred))
+ return -EIO;
- if (root)
- p = strjoin(root, *i, "/", path, NULL);
- else
- p = strjoin(*i, "/", path, NULL);
- if (!p)
- return -ENOMEM;
+ /* Check if the data is actually useful and not suppressed due
+ * to namespacing issues */
+ if (u.pid <= 0)
+ return -ENODATA;
+ if (u.uid == UID_INVALID)
+ return -ENODATA;
+ if (u.gid == GID_INVALID)
+ return -ENODATA;
- f = fopen(p, mode);
- if (f) {
- *_f = f;
- return 0;
- }
+ *ucred = u;
+ return 0;
+}
- if (errno != ENOENT)
- return -errno;
- }
+int getpeersec(int fd, char **ret) {
+ socklen_t n = 64;
+ char *s;
+ int r;
- return -ENOENT;
-}
+ assert(fd >= 0);
+ assert(ret);
-int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
- _cleanup_strv_free_ char **copy = NULL;
+ s = new0(char, n);
+ if (!s)
+ return -ENOMEM;
- assert(path);
- assert(mode);
- assert(_f);
+ r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n);
+ if (r < 0) {
+ free(s);
- if (path_is_absolute(path)) {
- FILE *f;
+ if (errno != ERANGE)
+ return -errno;
- f = fopen(path, mode);
- if (f) {
- *_f = f;
- return 0;
- }
+ s = new0(char, n);
+ if (!s)
+ return -ENOMEM;
- return -errno;
+ r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n);
+ if (r < 0) {
+ free(s);
+ return -errno;
+ }
}
- copy = strv_copy((char**) search);
- if (!copy)
- return -ENOMEM;
+ if (isempty(s)) {
+ free(s);
+ return -EOPNOTSUPP;
+ }
- return search_and_fopen_internal(path, mode, root, copy, _f);
+ *ret = s;
+ return 0;
}
-int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
- _cleanup_strv_free_ char **s = NULL;
+/* This is much like like mkostemp() but is subject to umask(). */
+int mkostemp_safe(char *pattern, int flags) {
+ _cleanup_umask_ mode_t u;
+ int fd;
- if (path_is_absolute(path)) {
- FILE *f;
+ assert(pattern);
- f = fopen(path, mode);
- if (f) {
- *_f = f;
- return 0;
- }
+ u = umask(077);
+ fd = mkostemp(pattern, flags);
+ if (fd < 0)
return -errno;
- }
-
- s = strv_split_nulstr(search);
- if (!s)
- return -ENOMEM;
- return search_and_fopen_internal(path, mode, root, s, _f);
+ return fd;
}
-char *strextend(char **x, ...) {
- va_list ap;
- size_t f, l;
- char *r, *p;
+int open_tmpfile(const char *path, int flags) {
+ char *p;
+ int fd;
- assert(x);
+ assert(path);
- l = f = *x ? strlen(*x) : 0;
+#ifdef O_TMPFILE
+ /* Try O_TMPFILE first, if it is supported */
+ fd = open(path, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
+ if (fd >= 0)
+ return fd;
+#endif
- va_start(ap, x);
- for (;;) {
- const char *t;
- size_t n;
+ /* Fall back to unguessable name + unlinking */
+ p = strjoina(path, "/systemd-tmp-XXXXXX");
- t = va_arg(ap, const char *);
- if (!t)
- break;
+ fd = mkostemp_safe(p, flags);
+ if (fd < 0)
+ return fd;
- n = strlen(t);
- if (n > ((size_t) -1) - l) {
- va_end(ap);
- return NULL;
- }
+ unlink(p);
+ return fd;
+}
- l += n;
- }
- va_end(ap);
+int fd_warn_permissions(const char *path, int fd) {
+ struct stat st;
- r = realloc(*x, l+1);
- if (!r)
- return NULL;
+ if (fstat(fd, &st) < 0)
+ return -errno;
- p = r + f;
+ if (st.st_mode & 0111)
+ log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path);
- va_start(ap, x);
- for (;;) {
- const char *t;
+ if (st.st_mode & 0002)
+ log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path);
- t = va_arg(ap, const char *);
- if (!t)
- break;
+ if (getpid() == 1 && (st.st_mode & 0044) != 0044)
+ log_warning("Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.", path);
- p = stpcpy(p, t);
- }
- va_end(ap);
+ return 0;
+}
- *p = 0;
- *x = r;
+unsigned long personality_from_string(const char *p) {
- return r + l;
-}
+ /* Parse a personality specifier. We introduce our own
+ * identifiers that indicate specific ABIs, rather than just
+ * hints regarding the register size, since we want to keep
+ * things open for multiple locally supported ABIs for the
+ * same register size. We try to reuse the ABI identifiers
+ * used by libseccomp. */
-char *strrep(const char *s, unsigned n) {
- size_t l;
- char *r, *p;
- unsigned i;
+#if defined(__x86_64__)
- assert(s);
+ if (streq(p, "x86"))
+ return PER_LINUX32;
- l = strlen(s);
- p = r = malloc(l * n + 1);
- if (!r)
- return NULL;
+ if (streq(p, "x86-64"))
+ return PER_LINUX;
- for (i = 0; i < n; i++)
- p = stpcpy(p, s);
+#elif defined(__i386__)
- *p = 0;
- return r;
+ if (streq(p, "x86"))
+ return PER_LINUX;
+#endif
+
+ /* personality(7) documents that 0xffffffffUL is used for
+ * querying the current personality, hence let's use that here
+ * as error indicator. */
+ return 0xffffffffUL;
}
-void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
- size_t a, newalloc;
- void *q;
+const char* personality_to_string(unsigned long p) {
- assert(p);
- assert(allocated);
+#if defined(__x86_64__)
- if (*allocated >= need)
- return *p;
+ if (p == PER_LINUX32)
+ return "x86";
- newalloc = MAX(need * 2, 64u / size);
- a = newalloc * size;
+ if (p == PER_LINUX)
+ return "x86-64";
- /* check for overflows */
- if (a < size * need)
- return NULL;
+#elif defined(__i386__)
- q = realloc(*p, a);
- if (!q)
- return NULL;
+ if (p == PER_LINUX)
+ return "x86";
+#endif
- *p = q;
- *allocated = newalloc;
- return q;
+ return NULL;
}
-void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) {
- size_t prev;
- uint8_t *q;
+uint64_t physical_memory(void) {
+ long mem;
- assert(p);
- assert(allocated);
+ /* We return this as uint64_t in case we are running as 32bit
+ * process on a 64bit kernel with huge amounts of memory */
- prev = *allocated;
+ mem = sysconf(_SC_PHYS_PAGES);
+ assert(mem > 0);
- q = greedy_realloc(p, allocated, need, size);
- if (!q)
- return NULL;
+ return (uint64_t) mem * (uint64_t) page_size();
+}
- if (*allocated > prev)
- memzero(q + prev * size, (*allocated - prev) * size);
+void hexdump(FILE *f, const void *p, size_t s) {
+ const uint8_t *b = p;
+ unsigned n = 0;
- return q;
-}
+ assert(s == 0 || b);
-bool id128_is_valid(const char *s) {
- size_t i, l;
+ while (s > 0) {
+ size_t i;
- l = strlen(s);
- if (l == 32) {
+ fprintf(f, "%04x ", n);
- /* Simple formatted 128bit hex string */
+ for (i = 0; i < 16; i++) {
- for (i = 0; i < l; i++) {
- char c = s[i];
+ if (i >= s)
+ fputs(" ", f);
+ else
+ fprintf(f, "%02x ", b[i]);
- if (!(c >= '0' && c <= '9') &&
- !(c >= 'a' && c <= 'z') &&
- !(c >= 'A' && c <= 'Z'))
- return false;
+ if (i == 7)
+ fputc(' ', f);
}
- } else if (l == 36) {
-
- /* Formatted UUID */
+ fputc(' ', f);
- for (i = 0; i < l; i++) {
- char c = s[i];
+ for (i = 0; i < 16; i++) {
- if ((i == 8 || i == 13 || i == 18 || i == 23)) {
- if (c != '-')
- return false;
- } else {
- if (!(c >= '0' && c <= '9') &&
- !(c >= 'a' && c <= 'z') &&
- !(c >= 'A' && c <= 'Z'))
- return false;
- }
+ if (i >= s)
+ fputc(' ', f);
+ else
+ fputc(isprint(b[i]) ? (char) b[i] : '.', f);
}
- } else
- return false;
+ fputc('\n', f);
- return true;
-}
+ if (s < 16)
+ break;
-int split_pair(const char *s, const char *sep, char **l, char **r) {
- char *x, *a, *b;
+ n += 16;
+ b += 16;
+ s -= 16;
+ }
+}
- assert(s);
- assert(sep);
- assert(l);
- assert(r);
+int update_reboot_param_file(const char *param) {
+ int r = 0;
- if (isempty(sep))
- return -EINVAL;
+ if (param) {
- x = strstr(s, sep);
- if (!x)
- return -EINVAL;
+ r = write_string_file(REBOOT_PARAM_FILE, param);
+ if (r < 0)
+ log_error("Failed to write reboot param to "
+ REBOOT_PARAM_FILE": %s", strerror(-r));
+ } else
+ unlink(REBOOT_PARAM_FILE);
- a = strndup(s, x - s);
- if (!a)
- return -ENOMEM;
+ return r;
+}
- b = strdup(x + strlen(sep));
- if (!b) {
- free(a);
- return -ENOMEM;
- }
+int umount_recursive(const char *prefix, int flags) {
+ bool again;
+ int n = 0, r;
- *l = a;
- *r = b;
+ /* Try to umount everything recursively below a
+ * directory. Also, take care of stacked mounts, and keep
+ * unmounting them until they are gone. */
- return 0;
-}
+ do {
+ _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
-int shall_restore_state(void) {
- _cleanup_free_ char *value = NULL;
- int r;
+ again = false;
+ r = 0;
- r = get_proc_cmdline_key("systemd.restore_state=", &value);
- if (r < 0)
- return r;
- if (r == 0)
- return true;
+ proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
+ if (!proc_self_mountinfo)
+ return -errno;
- return parse_boolean(value) != 0;
-}
+ for (;;) {
+ _cleanup_free_ char *path = NULL, *p = NULL;
+ int k;
-int proc_cmdline(char **ret) {
- assert(ret);
+ k = fscanf(proc_self_mountinfo,
+ "%*s " /* (1) mount id */
+ "%*s " /* (2) parent id */
+ "%*s " /* (3) major:minor */
+ "%*s " /* (4) root */
+ "%ms " /* (5) mount point */
+ "%*s" /* (6) mount options */
+ "%*[^-]" /* (7) optional fields */
+ "- " /* (8) separator */
+ "%*s " /* (9) file system type */
+ "%*s" /* (10) mount source */
+ "%*s" /* (11) mount options 2 */
+ "%*[^\n]", /* some rubbish at the end */
+ &path);
+ if (k != 1) {
+ if (k == EOF)
+ break;
- if (detect_container(NULL) > 0)
- return get_process_cmdline(1, 0, false, ret);
- else
- return read_one_line_file("/proc/cmdline", ret);
-}
+ continue;
+ }
-int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value)) {
- _cleanup_free_ char *line = NULL;
- const char *p;
- int r;
+ r = cunescape(path, UNESCAPE_RELAX, &p);
+ if (r < 0)
+ return r;
- assert(parse_item);
+ if (!path_startswith(p, prefix))
+ continue;
- r = proc_cmdline(&line);
- if (r < 0)
- return r;
+ if (umount2(p, flags) < 0) {
+ r = -errno;
+ continue;
+ }
- p = line;
- for (;;) {
- _cleanup_free_ char *word = NULL;
- char *value = NULL;
+ again = true;
+ n++;
- r = unquote_first_word(&p, &word, true);
- if (r < 0)
- return r;
- if (r == 0)
break;
+ }
- /* Filter out arguments that are intended only for the
- * initrd */
- if (!in_initrd() && startswith(word, "rd."))
- continue;
+ } while (again);
- value = strchr(word, '=');
- if (value)
- *(value++) = 0;
+ return r ? r : n;
+}
+
+static int get_mount_flags(const char *path, unsigned long *flags) {
+ struct statvfs buf;
+
+ if (statvfs(path, &buf) < 0)
+ return -errno;
+ *flags = buf.f_flag;
+ return 0;
+}
+
+int bind_remount_recursive(const char *prefix, bool ro) {
+ _cleanup_set_free_free_ Set *done = NULL;
+ _cleanup_free_ char *cleaned = NULL;
+ int r;
+
+ /* Recursively remount a directory (and all its submounts)
+ * read-only or read-write. If the directory is already
+ * mounted, we reuse the mount and simply mark it
+ * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write
+ * operation). If it isn't we first make it one. Afterwards we
+ * apply MS_BIND|MS_RDONLY (or remove MS_RDONLY) to all
+ * submounts we can access, too. When mounts are stacked on
+ * the same mount point we only care for each individual
+ * "top-level" mount on each point, as we cannot
+ * influence/access the underlying mounts anyway. We do not
+ * have any effect on future submounts that might get
+ * propagated, they migt be writable. This includes future
+ * submounts that have been triggered via autofs. */
- r = parse_item(word, value);
- if (r < 0)
- return r;
- }
+ cleaned = strdup(prefix);
+ if (!cleaned)
+ return -ENOMEM;
- return 0;
-}
+ path_kill_slashes(cleaned);
-int get_proc_cmdline_key(const char *key, char **value) {
- _cleanup_free_ char *line = NULL, *ret = NULL;
- bool found = false;
- const char *p;
- int r;
+ done = set_new(&string_hash_ops);
+ if (!done)
+ return -ENOMEM;
- assert(key);
+ for (;;) {
+ _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
+ _cleanup_set_free_free_ Set *todo = NULL;
+ bool top_autofs = false;
+ char *x;
+ unsigned long orig_flags;
- r = proc_cmdline(&line);
- if (r < 0)
- return r;
+ todo = set_new(&string_hash_ops);
+ if (!todo)
+ return -ENOMEM;
- p = line;
- for (;;) {
- _cleanup_free_ char *word = NULL;
- const char *e;
+ proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
+ if (!proc_self_mountinfo)
+ return -errno;
- r = unquote_first_word(&p, &word, true);
- if (r < 0)
- return r;
- if (r == 0)
- break;
+ for (;;) {
+ _cleanup_free_ char *path = NULL, *p = NULL, *type = NULL;
+ int k;
- /* Filter out arguments that are intended only for the
- * initrd */
- if (!in_initrd() && startswith(word, "rd."))
- continue;
+ k = fscanf(proc_self_mountinfo,
+ "%*s " /* (1) mount id */
+ "%*s " /* (2) parent id */
+ "%*s " /* (3) major:minor */
+ "%*s " /* (4) root */
+ "%ms " /* (5) mount point */
+ "%*s" /* (6) mount options (superblock) */
+ "%*[^-]" /* (7) optional fields */
+ "- " /* (8) separator */
+ "%ms " /* (9) file system type */
+ "%*s" /* (10) mount source */
+ "%*s" /* (11) mount options (bind mount) */
+ "%*[^\n]", /* some rubbish at the end */
+ &path,
+ &type);
+ if (k != 2) {
+ if (k == EOF)
+ break;
- if (value) {
- e = startswith(word, key);
- if (!e)
continue;
+ }
- r = free_and_strdup(&ret, e);
+ r = cunescape(path, UNESCAPE_RELAX, &p);
if (r < 0)
return r;
- found = true;
- } else {
- if (streq(word, key))
- found = true;
- }
- }
+ /* Let's ignore autofs mounts. If they aren't
+ * triggered yet, we want to avoid triggering
+ * them, as we don't make any guarantees for
+ * future submounts anyway. If they are
+ * already triggered, then we will find
+ * another entry for this. */
+ if (streq(type, "autofs")) {
+ top_autofs = top_autofs || path_equal(cleaned, p);
+ continue;
+ }
- if (value) {
- *value = ret;
- ret = NULL;
- }
+ if (path_startswith(p, cleaned) &&
+ !set_contains(done, p)) {
- return found;
+ r = set_consume(todo, p);
+ p = NULL;
-}
+ if (r == -EEXIST)
+ continue;
+ if (r < 0)
+ return r;
+ }
+ }
-int container_get_leader(const char *machine, pid_t *pid) {
- _cleanup_free_ char *s = NULL, *class = NULL;
- const char *p;
- pid_t leader;
- int r;
+ /* If we have no submounts to process anymore and if
+ * the root is either already done, or an autofs, we
+ * are done */
+ if (set_isempty(todo) &&
+ (top_autofs || set_contains(done, cleaned)))
+ return 0;
- assert(machine);
- assert(pid);
+ if (!set_contains(done, cleaned) &&
+ !set_contains(todo, cleaned)) {
+ /* The prefix directory itself is not yet a
+ * mount, make it one. */
+ if (mount(cleaned, cleaned, NULL, MS_BIND|MS_REC, NULL) < 0)
+ return -errno;
- p = strappenda("/run/systemd/machines/", machine);
- r = parse_env_file(p, NEWLINE, "LEADER", &s, "CLASS", &class, NULL);
- if (r == -ENOENT)
- return -EHOSTDOWN;
- if (r < 0)
- return r;
- if (!s)
- return -EIO;
+ orig_flags = 0;
+ (void) get_mount_flags(cleaned, &orig_flags);
+ orig_flags &= ~MS_RDONLY;
- if (!streq_ptr(class, "container"))
- return -EIO;
+ if (mount(NULL, prefix, NULL, orig_flags|MS_BIND|MS_REMOUNT|(ro ? MS_RDONLY : 0), NULL) < 0)
+ return -errno;
- r = parse_pid(s, &leader);
- if (r < 0)
- return r;
- if (leader <= 1)
- return -EIO;
+ x = strdup(cleaned);
+ if (!x)
+ return -ENOMEM;
- *pid = leader;
- return 0;
-}
+ r = set_consume(done, x);
+ if (r < 0)
+ return r;
+ }
-int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd) {
- _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, netnsfd = -1;
- int rfd = -1;
+ while ((x = set_steal_first(todo))) {
- assert(pid >= 0);
+ r = set_consume(done, x);
+ if (r == -EEXIST)
+ continue;
+ if (r < 0)
+ return r;
- if (mntns_fd) {
- const char *mntns;
+ /* Try to reuse the original flag set, but
+ * don't care for errors, in case of
+ * obstructed mounts */
+ orig_flags = 0;
+ (void) get_mount_flags(x, &orig_flags);
+ orig_flags &= ~MS_RDONLY;
- mntns = procfs_file_alloca(pid, "ns/mnt");
- mntnsfd = open(mntns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (mntnsfd < 0)
- return -errno;
- }
+ if (mount(NULL, x, NULL, orig_flags|MS_BIND|MS_REMOUNT|(ro ? MS_RDONLY : 0), NULL) < 0) {
- if (pidns_fd) {
- const char *pidns;
+ /* Deal with mount points that are
+ * obstructed by a later mount */
- pidns = procfs_file_alloca(pid, "ns/pid");
- pidnsfd = open(pidns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (pidnsfd < 0)
- return -errno;
+ if (errno != ENOENT)
+ return -errno;
+ }
+
+ }
}
+}
- if (netns_fd) {
- const char *netns;
+int fflush_and_check(FILE *f) {
+ assert(f);
- netns = procfs_file_alloca(pid, "ns/net");
- netnsfd = open(netns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (netnsfd < 0)
- return -errno;
- }
+ errno = 0;
+ fflush(f);
- if (root_fd) {
- const char *root;
+ if (ferror(f))
+ return errno ? -errno : -EIO;
- root = procfs_file_alloca(pid, "root");
- rfd = open(root, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
- if (rfd < 0)
- return -errno;
- }
+ return 0;
+}
- if (pidns_fd)
- *pidns_fd = pidnsfd;
+int tempfn_xxxxxx(const char *p, char **ret) {
+ const char *fn;
+ char *t;
- if (mntns_fd)
- *mntns_fd = mntnsfd;
+ assert(p);
+ assert(ret);
- if (netns_fd)
- *netns_fd = netnsfd;
+ /*
+ * Turns this:
+ * /foo/bar/waldo
+ *
+ * Into this:
+ * /foo/bar/.#waldoXXXXXX
+ */
- if (root_fd)
- *root_fd = rfd;
+ fn = basename(p);
+ if (!filename_is_valid(fn))
+ return -EINVAL;
- pidnsfd = mntnsfd = netnsfd = -1;
+ t = new(char, strlen(p) + 2 + 6 + 1);
+ if (!t)
+ return -ENOMEM;
+ strcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), "XXXXXX");
+
+ *ret = path_kill_slashes(t);
return 0;
}
-int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd) {
+int tempfn_random(const char *p, char **ret) {
+ const char *fn;
+ char *t, *x;
+ uint64_t u;
+ unsigned i;
- if (pidns_fd >= 0)
- if (setns(pidns_fd, CLONE_NEWPID) < 0)
- return -errno;
+ assert(p);
+ assert(ret);
- if (mntns_fd >= 0)
- if (setns(mntns_fd, CLONE_NEWNS) < 0)
- return -errno;
+ /*
+ * Turns this:
+ * /foo/bar/waldo
+ *
+ * Into this:
+ * /foo/bar/.#waldobaa2a261115984a9
+ */
- if (netns_fd >= 0)
- if (setns(netns_fd, CLONE_NEWNET) < 0)
- return -errno;
+ fn = basename(p);
+ if (!filename_is_valid(fn))
+ return -EINVAL;
- if (root_fd >= 0) {
- if (fchdir(root_fd) < 0)
- return -errno;
+ t = new(char, strlen(p) + 2 + 16 + 1);
+ if (!t)
+ return -ENOMEM;
- if (chroot(".") < 0)
- return -errno;
+ x = stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn);
+
+ u = random_u64();
+ for (i = 0; i < 16; i++) {
+ *(x++) = hexchar(u & 0xF);
+ u >>= 4;
}
- if (setresgid(0, 0, 0) < 0)
- return -errno;
-
- if (setgroups(0, NULL) < 0)
- return -errno;
-
- if (setresuid(0, 0, 0) < 0)
- return -errno;
+ *x = 0;
+ *ret = path_kill_slashes(t);
return 0;
}
-bool pid_is_unwaited(pid_t pid) {
- /* Checks whether a PID is still valid at all, including a zombie */
-
- if (pid <= 0)
- return false;
+int tempfn_random_child(const char *p, char **ret) {
+ char *t, *x;
+ uint64_t u;
+ unsigned i;
- if (kill(pid, 0) >= 0)
- return true;
+ assert(p);
+ assert(ret);
- return errno != ESRCH;
-}
+ /* Turns this:
+ * /foo/bar/waldo
+ * Into this:
+ * /foo/bar/waldo/.#3c2b6219aa75d7d0
+ */
-bool pid_is_alive(pid_t pid) {
- int r;
+ t = new(char, strlen(p) + 3 + 16 + 1);
+ if (!t)
+ return -ENOMEM;
- /* Checks whether a PID is still valid and not a zombie */
+ x = stpcpy(stpcpy(t, p), "/.#");
- if (pid <= 0)
- return false;
+ u = random_u64();
+ for (i = 0; i < 16; i++) {
+ *(x++) = hexchar(u & 0xF);
+ u >>= 4;
+ }
- r = get_process_state(pid);
- if (r == -ENOENT || r == 'Z')
- return false;
+ *x = 0;
- return true;
+ *ret = path_kill_slashes(t);
+ return 0;
}
-int getpeercred(int fd, struct ucred *ucred) {
- socklen_t n = sizeof(struct ucred);
- struct ucred u;
- int r;
+/* make sure the hostname is not "localhost" */
+bool is_localhost(const char *hostname) {
+ assert(hostname);
- assert(fd >= 0);
- assert(ucred);
+ /* This tries to identify local host and domain names
+ * described in RFC6761 plus the redhatism of .localdomain */
- r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n);
- if (r < 0)
- return -errno;
+ return streq(hostname, "localhost") ||
+ streq(hostname, "localhost.") ||
+ streq(hostname, "localdomain.") ||
+ streq(hostname, "localdomain") ||
+ endswith(hostname, ".localhost") ||
+ endswith(hostname, ".localhost.") ||
+ endswith(hostname, ".localdomain") ||
+ endswith(hostname, ".localdomain.");
+}
- if (n != sizeof(struct ucred))
- return -EIO;
+int take_password_lock(const char *root) {
- /* Check if the data is actually useful and not suppressed due
- * to namespacing issues */
- if (u.pid <= 0)
- return -ENODATA;
- if (u.uid == UID_INVALID)
- return -ENODATA;
- if (u.gid == GID_INVALID)
- return -ENODATA;
+ struct flock flock = {
+ .l_type = F_WRLCK,
+ .l_whence = SEEK_SET,
+ .l_start = 0,
+ .l_len = 0,
+ };
- *ucred = u;
- return 0;
-}
+ const char *path;
+ int fd, r;
-int getpeersec(int fd, char **ret) {
- socklen_t n = 64;
- char *s;
- int r;
+ /* This is roughly the same as lckpwdf(), but not as awful. We
+ * don't want to use alarm() and signals, hence we implement
+ * our own trivial version of this.
+ *
+ * Note that shadow-utils also takes per-database locks in
+ * addition to lckpwdf(). However, we don't given that they
+ * are redundant as they they invoke lckpwdf() first and keep
+ * it during everything they do. The per-database locks are
+ * awfully racy, and thus we just won't do them. */
- assert(fd >= 0);
- assert(ret);
+ if (root)
+ path = strjoina(root, "/etc/.pwd.lock");
+ else
+ path = "/etc/.pwd.lock";
- s = new0(char, n);
- if (!s)
- return -ENOMEM;
+ fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
+ if (fd < 0)
+ return -errno;
- r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n);
+ r = fcntl(fd, F_SETLKW, &flock);
if (r < 0) {
- free(s);
+ safe_close(fd);
+ return -errno;
+ }
- if (errno != ERANGE)
- return -errno;
+ return fd;
+}
- s = new0(char, n);
- if (!s)
- return -ENOMEM;
+int is_symlink(const char *path) {
+ struct stat info;
- r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n);
- if (r < 0) {
- free(s);
- return -errno;
- }
- }
+ if (lstat(path, &info) < 0)
+ return -errno;
- if (isempty(s)) {
- free(s);
- return -ENOTSUP;
- }
+ return !!S_ISLNK(info.st_mode);
+}
- *ret = s;
- return 0;
+int is_dir(const char* path, bool follow) {
+ struct stat st;
+ int r;
+
+ if (follow)
+ r = stat(path, &st);
+ else
+ r = lstat(path, &st);
+ if (r < 0)
+ return -errno;
+
+ return !!S_ISDIR(st.st_mode);
}
-/* This is much like like mkostemp() but is subject to umask(). */
-int mkostemp_safe(char *pattern, int flags) {
- _cleanup_umask_ mode_t u;
- int fd;
+int unquote_first_word(const char **p, char **ret, UnquoteFlags flags) {
+ _cleanup_free_ char *s = NULL;
+ size_t allocated = 0, sz = 0;
+ int r;
- assert(pattern);
+ enum {
+ START,
+ VALUE,
+ VALUE_ESCAPE,
+ SINGLE_QUOTE,
+ SINGLE_QUOTE_ESCAPE,
+ DOUBLE_QUOTE,
+ DOUBLE_QUOTE_ESCAPE,
+ SPACE,
+ } state = START;
- u = umask(077);
+ assert(p);
+ assert(*p);
+ assert(ret);
- fd = mkostemp(pattern, flags);
- if (fd < 0)
- return -errno;
+ /* Parses the first word of a string, and returns it in
+ * *ret. Removes all quotes in the process. When parsing fails
+ * (because of an uneven number of quotes or similar), leaves
+ * the pointer *p at the first invalid character. */
- return fd;
-}
+ for (;;) {
+ char c = **p;
-int open_tmpfile(const char *path, int flags) {
- char *p;
- int fd;
+ switch (state) {
- assert(path);
+ case START:
+ if (c == 0)
+ goto finish;
+ else if (strchr(WHITESPACE, c))
+ break;
-#ifdef O_TMPFILE
- /* Try O_TMPFILE first, if it is supported */
- fd = open(path, flags|O_TMPFILE, S_IRUSR|S_IWUSR);
- if (fd >= 0)
- return fd;
-#endif
+ state = VALUE;
+ /* fallthrough */
- /* Fall back to unguessable name + unlinking */
- p = strappenda(path, "/systemd-tmp-XXXXXX");
+ case VALUE:
+ if (c == 0)
+ goto finish;
+ else if (c == '\'')
+ state = SINGLE_QUOTE;
+ else if (c == '\\')
+ state = VALUE_ESCAPE;
+ else if (c == '\"')
+ state = DOUBLE_QUOTE;
+ else if (strchr(WHITESPACE, c))
+ state = SPACE;
+ else {
+ if (!GREEDY_REALLOC(s, allocated, sz+2))
+ return -ENOMEM;
- fd = mkostemp_safe(p, flags);
- if (fd < 0)
- return fd;
+ s[sz++] = c;
+ }
- unlink(p);
- return fd;
-}
+ break;
-int fd_warn_permissions(const char *path, int fd) {
- struct stat st;
+ case VALUE_ESCAPE:
+ if (c == 0) {
+ if (flags & UNQUOTE_RELAX)
+ goto finish;
+ return -EINVAL;
+ }
- if (fstat(fd, &st) < 0)
- return -errno;
+ if (!GREEDY_REALLOC(s, allocated, sz+7))
+ return -ENOMEM;
- if (st.st_mode & 0111)
- log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path);
+ if (flags & UNQUOTE_CUNESCAPE) {
+ uint32_t u;
- if (st.st_mode & 0002)
- log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path);
+ r = cunescape_one(*p, (size_t) -1, &c, &u);
+ if (r < 0)
+ return -EINVAL;
- if (getpid() == 1 && (st.st_mode & 0044) != 0044)
- log_warning("Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.", path);
+ (*p) += r - 1;
- return 0;
-}
+ if (c != 0)
+ s[sz++] = c; /* normal explicit char */
+ else
+ sz += utf8_encode_unichar(s + sz, u); /* unicode chars we'll encode as utf8 */
+ } else
+ s[sz++] = c;
-unsigned long personality_from_string(const char *p) {
+ state = VALUE;
+ break;
+
+ case SINGLE_QUOTE:
+ if (c == 0) {
+ if (flags & UNQUOTE_RELAX)
+ goto finish;
+ return -EINVAL;
+ } else if (c == '\'')
+ state = VALUE;
+ else if (c == '\\')
+ state = SINGLE_QUOTE_ESCAPE;
+ else {
+ if (!GREEDY_REALLOC(s, allocated, sz+2))
+ return -ENOMEM;
+
+ s[sz++] = c;
+ }
- /* Parse a personality specifier. We introduce our own
- * identifiers that indicate specific ABIs, rather than just
- * hints regarding the register size, since we want to keep
- * things open for multiple locally supported ABIs for the
- * same register size. We try to reuse the ABI identifiers
- * used by libseccomp. */
+ break;
-#if defined(__x86_64__)
+ case SINGLE_QUOTE_ESCAPE:
+ if (c == 0) {
+ if (flags & UNQUOTE_RELAX)
+ goto finish;
+ return -EINVAL;
+ }
- if (streq(p, "x86"))
- return PER_LINUX32;
+ if (!GREEDY_REALLOC(s, allocated, sz+7))
+ return -ENOMEM;
- if (streq(p, "x86-64"))
- return PER_LINUX;
+ if (flags & UNQUOTE_CUNESCAPE) {
+ uint32_t u;
-#elif defined(__i386__)
+ r = cunescape_one(*p, (size_t) -1, &c, &u);
+ if (r < 0)
+ return -EINVAL;
- if (streq(p, "x86"))
- return PER_LINUX;
-#endif
+ (*p) += r - 1;
- /* personality(7) documents that 0xffffffffUL is used for
- * querying the current personality, hence let's use that here
- * as error indicator. */
- return 0xffffffffUL;
-}
+ if (c != 0)
+ s[sz++] = c;
+ else
+ sz += utf8_encode_unichar(s + sz, u);
+ } else
+ s[sz++] = c;
-const char* personality_to_string(unsigned long p) {
+ state = SINGLE_QUOTE;
+ break;
-#if defined(__x86_64__)
+ case DOUBLE_QUOTE:
+ if (c == 0)
+ return -EINVAL;
+ else if (c == '\"')
+ state = VALUE;
+ else if (c == '\\')
+ state = DOUBLE_QUOTE_ESCAPE;
+ else {
+ if (!GREEDY_REALLOC(s, allocated, sz+2))
+ return -ENOMEM;
- if (p == PER_LINUX32)
- return "x86";
+ s[sz++] = c;
+ }
- if (p == PER_LINUX)
- return "x86-64";
+ break;
-#elif defined(__i386__)
+ case DOUBLE_QUOTE_ESCAPE:
+ if (c == 0) {
+ if (flags & UNQUOTE_RELAX)
+ goto finish;
+ return -EINVAL;
+ }
- if (p == PER_LINUX)
- return "x86";
-#endif
+ if (!GREEDY_REALLOC(s, allocated, sz+7))
+ return -ENOMEM;
- return NULL;
-}
+ if (flags & UNQUOTE_CUNESCAPE) {
+ uint32_t u;
-uint64_t physical_memory(void) {
- long mem;
+ r = cunescape_one(*p, (size_t) -1, &c, &u);
+ if (r < 0)
+ return -EINVAL;
- /* We return this as uint64_t in case we are running as 32bit
- * process on a 64bit kernel with huge amounts of memory */
+ (*p) += r - 1;
- mem = sysconf(_SC_PHYS_PAGES);
- assert(mem > 0);
+ if (c != 0)
+ s[sz++] = c;
+ else
+ sz += utf8_encode_unichar(s + sz, u);
+ } else
+ s[sz++] = c;
- return (uint64_t) mem * (uint64_t) page_size();
-}
+ state = DOUBLE_QUOTE;
+ break;
-char* mount_test_option(const char *haystack, const char *needle) {
+ case SPACE:
+ if (c == 0)
+ goto finish;
+ if (!strchr(WHITESPACE, c))
+ goto finish;
- struct mntent me = {
- .mnt_opts = (char*) haystack
- };
+ break;
+ }
- assert(needle);
+ (*p) ++;
+ }
- /* Like glibc's hasmntopt(), but works on a string, not a
- * struct mntent */
+finish:
+ if (!s) {
+ *ret = NULL;
+ return 0;
+ }
- if (!haystack)
- return NULL;
+ s[sz] = 0;
+ *ret = s;
+ s = NULL;
- return hasmntopt(&me, needle);
+ return 1;
}
-void hexdump(FILE *f, const void *p, size_t s) {
- const uint8_t *b = p;
- unsigned n = 0;
-
- assert(s == 0 || b);
+int unquote_many_words(const char **p, UnquoteFlags flags, ...) {
+ va_list ap;
+ char **l;
+ int n = 0, i, c, r;
- while (s > 0) {
- size_t i;
+ /* Parses a number of words from a string, stripping any
+ * quotes if necessary. */
- fprintf(f, "%04x ", n);
+ assert(p);
- for (i = 0; i < 16; i++) {
+ /* Count how many words are expected */
+ va_start(ap, flags);
+ for (;;) {
+ if (!va_arg(ap, char **))
+ break;
+ n++;
+ }
+ va_end(ap);
- if (i >= s)
- fputs(" ", f);
- else
- fprintf(f, "%02x ", b[i]);
+ if (n <= 0)
+ return 0;
- if (i == 7)
- fputc(' ', f);
- }
+ /* Read all words into a temporary array */
+ l = newa0(char*, n);
+ for (c = 0; c < n; c++) {
- fputc(' ', f);
+ r = unquote_first_word(p, &l[c], flags);
+ if (r < 0) {
+ int j;
- for (i = 0; i < 16; i++) {
+ for (j = 0; j < c; j++)
+ free(l[j]);
- if (i >= s)
- fputc(' ', f);
- else
- fputc(isprint(b[i]) ? (char) b[i] : '.', f);
+ return r;
}
- fputc('\n', f);
-
- if (s < 16)
+ if (r == 0)
break;
+ }
- n += 16;
- b += 16;
- s -= 16;
+ /* If we managed to parse all words, return them in the passed
+ * in parameters */
+ va_start(ap, flags);
+ for (i = 0; i < n; i++) {
+ char **v;
+
+ v = va_arg(ap, char **);
+ assert(v);
+
+ *v = l[i];
}
+ va_end(ap);
+
+ return c;
}
-int update_reboot_param_file(const char *param) {
- int r = 0;
+int free_and_strdup(char **p, const char *s) {
+ char *t;
- if (param) {
+ assert(p);
- r = write_string_file(REBOOT_PARAM_FILE, param);
- if (r < 0)
- log_error("Failed to write reboot param to "
- REBOOT_PARAM_FILE": %s", strerror(-r));
- } else
- unlink(REBOOT_PARAM_FILE);
+ /* Replaces a string pointer with an strdup()ed new string,
+ * possibly freeing the old one. */
- return r;
-}
+ if (streq_ptr(*p, s))
+ return 0;
-int umount_recursive(const char *prefix, int flags) {
- bool again;
- int n = 0, r;
+ if (s) {
+ t = strdup(s);
+ if (!t)
+ return -ENOMEM;
+ } else
+ t = NULL;
- /* Try to umount everything recursively below a
- * directory. Also, take care of stacked mounts, and keep
- * unmounting them until they are gone. */
+ free(*p);
+ *p = t;
- do {
- _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
+ return 1;
+}
- again = false;
- r = 0;
+int sethostname_idempotent(const char *s) {
+ int r;
+ char buf[HOST_NAME_MAX + 1] = {};
- proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
- if (!proc_self_mountinfo)
- return -errno;
+ assert(s);
- for (;;) {
- _cleanup_free_ char *path = NULL, *p = NULL;
- int k;
+ r = gethostname(buf, sizeof(buf));
+ if (r < 0)
+ return -errno;
- k = fscanf(proc_self_mountinfo,
- "%*s " /* (1) mount id */
- "%*s " /* (2) parent id */
- "%*s " /* (3) major:minor */
- "%*s " /* (4) root */
- "%ms " /* (5) mount point */
- "%*s" /* (6) mount options */
- "%*[^-]" /* (7) optional fields */
- "- " /* (8) separator */
- "%*s " /* (9) file system type */
- "%*s" /* (10) mount source */
- "%*s" /* (11) mount options 2 */
- "%*[^\n]", /* some rubbish at the end */
- &path);
- if (k != 1) {
- if (k == EOF)
- break;
+ if (streq(buf, s))
+ return 0;
- continue;
- }
+ r = sethostname(s, strlen(s));
+ if (r < 0)
+ return -errno;
- p = cunescape(path);
- if (!p)
- return -ENOMEM;
+ return 1;
+}
- if (!path_startswith(p, prefix))
- continue;
+int ptsname_malloc(int fd, char **ret) {
+ size_t l = 100;
- if (umount2(p, flags) < 0) {
- r = -errno;
- continue;
- }
+ assert(fd >= 0);
+ assert(ret);
- again = true;
- n++;
+ for (;;) {
+ char *c;
+
+ c = new(char, l);
+ if (!c)
+ return -ENOMEM;
- break;
+ if (ptsname_r(fd, c, l) == 0) {
+ *ret = c;
+ return 0;
+ }
+ if (errno != ERANGE) {
+ free(c);
+ return -errno;
}
- } while (again);
-
- return r ? r : n;
+ free(c);
+ l *= 2;
+ }
}
-int bind_remount_recursive(const char *prefix, bool ro) {
- _cleanup_set_free_free_ Set *done = NULL;
- _cleanup_free_ char *cleaned = NULL;
+int openpt_in_namespace(pid_t pid, int flags) {
+ _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
+ _cleanup_close_pair_ int pair[2] = { -1, -1 };
+ union {
+ struct cmsghdr cmsghdr;
+ uint8_t buf[CMSG_SPACE(sizeof(int))];
+ } control = {};
+ struct msghdr mh = {
+ .msg_control = &control,
+ .msg_controllen = sizeof(control),
+ };
+ struct cmsghdr *cmsg;
+ siginfo_t si;
+ pid_t child;
int r;
- /* Recursively remount a directory (and all its submounts)
- * read-only or read-write. If the directory is already
- * mounted, we reuse the mount and simply mark it
- * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write
- * operation). If it isn't we first make it one. Afterwards we
- * apply MS_BIND|MS_RDONLY (or remove MS_RDONLY) to all
- * submounts we can access, too. When mounts are stacked on
- * the same mount point we only care for each individual
- * "top-level" mount on each point, as we cannot
- * influence/access the underlying mounts anyway. We do not
- * have any effect on future submounts that might get
- * propagated, they migt be writable. This includes future
- * submounts that have been triggered via autofs. */
-
- cleaned = strdup(prefix);
- if (!cleaned)
- return -ENOMEM;
+ assert(pid > 0);
- path_kill_slashes(cleaned);
+ r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &rootfd);
+ if (r < 0)
+ return r;
- done = set_new(&string_hash_ops);
- if (!done)
- return -ENOMEM;
+ if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
+ return -errno;
- for (;;) {
- _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
- _cleanup_set_free_free_ Set *todo = NULL;
- bool top_autofs = false;
- char *x;
+ child = fork();
+ if (child < 0)
+ return -errno;
- todo = set_new(&string_hash_ops);
- if (!todo)
- return -ENOMEM;
+ if (child == 0) {
+ int master;
- proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
- if (!proc_self_mountinfo)
- return -errno;
+ pair[0] = safe_close(pair[0]);
- for (;;) {
- _cleanup_free_ char *path = NULL, *p = NULL, *type = NULL;
- int k;
+ r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
+ if (r < 0)
+ _exit(EXIT_FAILURE);
- k = fscanf(proc_self_mountinfo,
- "%*s " /* (1) mount id */
- "%*s " /* (2) parent id */
- "%*s " /* (3) major:minor */
- "%*s " /* (4) root */
- "%ms " /* (5) mount point */
- "%*s" /* (6) mount options (superblock) */
- "%*[^-]" /* (7) optional fields */
- "- " /* (8) separator */
- "%ms " /* (9) file system type */
- "%*s" /* (10) mount source */
- "%*s" /* (11) mount options (bind mount) */
- "%*[^\n]", /* some rubbish at the end */
- &path,
- &type);
- if (k != 2) {
- if (k == EOF)
- break;
+ master = posix_openpt(flags);
+ if (master < 0)
+ _exit(EXIT_FAILURE);
- continue;
- }
+ cmsg = CMSG_FIRSTHDR(&mh);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ memcpy(CMSG_DATA(cmsg), &master, sizeof(int));
- p = cunescape(path);
- if (!p)
- return -ENOMEM;
+ mh.msg_controllen = cmsg->cmsg_len;
- /* Let's ignore autofs mounts. If they aren't
- * triggered yet, we want to avoid triggering
- * them, as we don't make any guarantees for
- * future submounts anyway. If they are
- * already triggered, then we will find
- * another entry for this. */
- if (streq(type, "autofs")) {
- top_autofs = top_autofs || path_equal(cleaned, p);
- continue;
- }
+ if (sendmsg(pair[1], &mh, MSG_NOSIGNAL) < 0)
+ _exit(EXIT_FAILURE);
- if (path_startswith(p, cleaned) &&
- !set_contains(done, p)) {
+ _exit(EXIT_SUCCESS);
+ }
- r = set_consume(todo, p);
- p = NULL;
+ pair[1] = safe_close(pair[1]);
- if (r == -EEXIST)
- continue;
- if (r < 0)
- return r;
- }
- }
+ r = wait_for_terminate(child, &si);
+ if (r < 0)
+ return r;
+ if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
+ return -EIO;
- /* If we have no submounts to process anymore and if
- * the root is either already done, or an autofs, we
- * are done */
- if (set_isempty(todo) &&
- (top_autofs || set_contains(done, cleaned)))
- return 0;
+ if (recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
+ return -errno;
- if (!set_contains(done, cleaned) &&
- !set_contains(todo, cleaned)) {
- /* The prefix directory itself is not yet a
- * mount, make it one. */
- if (mount(cleaned, cleaned, NULL, MS_BIND|MS_REC, NULL) < 0)
- return -errno;
+ for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
+ if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
+ int *fds;
+ unsigned n_fds;
- if (mount(NULL, prefix, NULL, MS_BIND|MS_REMOUNT|(ro ? MS_RDONLY : 0), NULL) < 0)
- return -errno;
+ fds = (int*) CMSG_DATA(cmsg);
+ n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
- x = strdup(cleaned);
- if (!x)
- return -ENOMEM;
+ if (n_fds != 1) {
+ close_many(fds, n_fds);
+ return -EIO;
+ }
- r = set_consume(done, x);
- if (r < 0)
- return r;
+ return fds[0];
}
- while ((x = set_steal_first(todo))) {
+ return -EIO;
+}
- r = set_consume(done, x);
- if (r == -EEXIST)
- continue;
- if (r < 0)
- return r;
+ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags) {
+ _cleanup_close_ int fd = -1;
+ ssize_t l;
- if (mount(NULL, x, NULL, MS_BIND|MS_REMOUNT|(ro ? MS_RDONLY : 0), NULL) < 0) {
+ /* The kernel doesn't have a fgetxattrat() command, hence let's emulate one */
- /* Deal with mount points that are
- * obstructed by a later mount */
+ fd = openat(dirfd, filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOATIME|(flags & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW : 0));
+ if (fd < 0)
+ return -errno;
- if (errno != ENOENT)
- return -errno;
- }
+ l = fgetxattr(fd, attribute, value, size);
+ if (l < 0)
+ return -errno;
- }
- }
+ return l;
}
-int fflush_and_check(FILE *f) {
- assert(f);
+static int parse_crtime(le64_t le, usec_t *usec) {
+ uint64_t u;
- errno = 0;
- fflush(f);
+ assert(usec);
- if (ferror(f))
- return errno ? -errno : -EIO;
+ u = le64toh(le);
+ if (u == 0 || u == (uint64_t) -1)
+ return -EIO;
+ *usec = (usec_t) u;
return 0;
}
-char *tempfn_xxxxxx(const char *p) {
- const char *fn;
- char *t;
- size_t k;
-
- assert(p);
+int fd_getcrtime(int fd, usec_t *usec) {
+ le64_t le;
+ ssize_t n;
- t = new(char, strlen(p) + 1 + 6 + 1);
- if (!t)
- return NULL;
+ assert(fd >= 0);
+ assert(usec);
- fn = basename(p);
- k = fn - p;
+ /* Until Linux gets a real concept of birthtime/creation time,
+ * let's fake one with xattrs */
- strcpy(stpcpy(stpcpy(mempcpy(t, p, k), "."), fn), "XXXXXX");
+ n = fgetxattr(fd, "user.crtime_usec", &le, sizeof(le));
+ if (n < 0)
+ return -errno;
+ if (n != sizeof(le))
+ return -EIO;
- return t;
+ return parse_crtime(le, usec);
}
-char *tempfn_random(const char *p) {
- const char *fn;
- char *t, *x;
- uint64_t u;
- size_t k;
- unsigned i;
-
- assert(p);
+int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags) {
+ le64_t le;
+ ssize_t n;
- t = new(char, strlen(p) + 1 + 16 + 1);
- if (!t)
- return NULL;
+ n = fgetxattrat_fake(dirfd, name, "user.crtime_usec", &le, sizeof(le), flags);
+ if (n < 0)
+ return -errno;
+ if (n != sizeof(le))
+ return -EIO;
- fn = basename(p);
- k = fn - p;
+ return parse_crtime(le, usec);
+}
- x = stpcpy(stpcpy(mempcpy(t, p, k), "."), fn);
+int path_getcrtime(const char *p, usec_t *usec) {
+ le64_t le;
+ ssize_t n;
- u = random_u64();
- for (i = 0; i < 16; i++) {
- *(x++) = hexchar(u & 0xF);
- u >>= 4;
- }
+ assert(p);
+ assert(usec);
- *x = 0;
+ n = getxattr(p, "user.crtime_usec", &le, sizeof(le));
+ if (n < 0)
+ return -errno;
+ if (n != sizeof(le))
+ return -EIO;
- return t;
+ return parse_crtime(le, usec);
}
-/* make sure the hostname is not "localhost" */
-bool is_localhost(const char *hostname) {
- assert(hostname);
+int fd_setcrtime(int fd, usec_t usec) {
+ le64_t le;
- /* This tries to identify local host and domain names
- * described in RFC6761 plus the redhatism of .localdomain */
+ assert(fd >= 0);
- return streq(hostname, "localhost") ||
- streq(hostname, "localhost.") ||
- streq(hostname, "localdomain.") ||
- streq(hostname, "localdomain") ||
- endswith(hostname, ".localhost") ||
- endswith(hostname, ".localhost.") ||
- endswith(hostname, ".localdomain") ||
- endswith(hostname, ".localdomain.");
+ if (usec <= 0)
+ usec = now(CLOCK_REALTIME);
+
+ le = htole64((uint64_t) usec);
+ if (fsetxattr(fd, "user.crtime_usec", &le, sizeof(le), 0) < 0)
+ return -errno;
+
+ return 0;
}
-int take_password_lock(const char *root) {
+int chattr_fd(int fd, unsigned value, unsigned mask) {
+ unsigned old_attr, new_attr;
+ struct stat st;
- struct flock flock = {
- .l_type = F_WRLCK,
- .l_whence = SEEK_SET,
- .l_start = 0,
- .l_len = 0,
- };
+ assert(fd >= 0);
- const char *path;
- int fd, r;
+ if (fstat(fd, &st) < 0)
+ return -errno;
- /* This is roughly the same as lckpwdf(), but not as awful. We
- * don't want to use alarm() and signals, hence we implement
- * our own trivial version of this.
- *
- * Note that shadow-utils also takes per-database locks in
- * addition to lckpwdf(). However, we don't given that they
- * are redundant as they they invoke lckpwdf() first and keep
- * it during everything they do. The per-database locks are
- * awfully racy, and thus we just won't do them. */
+ /* Explicitly check whether this is a regular file or
+ * directory. If it is anything else (such as a device node or
+ * fifo), then the ioctl will not hit the file systems but
+ * possibly drivers, where the ioctl might have different
+ * effects. Notably, DRM is using the same ioctl() number. */
- if (root)
- path = strappenda(root, "/etc/.pwd.lock");
- else
- path = "/etc/.pwd.lock";
+ if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
+ return -ENOTTY;
- fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
- if (fd < 0)
+ if (mask == 0)
+ return 0;
+
+ if (ioctl(fd, FS_IOC_GETFLAGS, &old_attr) < 0)
return -errno;
- r = fcntl(fd, F_SETLKW, &flock);
- if (r < 0) {
- safe_close(fd);
+ new_attr = (old_attr & ~mask) | (value & mask);
+ if (new_attr == old_attr)
+ return 0;
+
+ if (ioctl(fd, FS_IOC_SETFLAGS, &new_attr) < 0)
return -errno;
- }
- return fd;
+ return 1;
}
-int is_symlink(const char *path) {
- struct stat info;
+int chattr_path(const char *p, unsigned value, unsigned mask) {
+ _cleanup_close_ int fd = -1;
- if (lstat(path, &info) < 0)
+ assert(p);
+
+ if (mask == 0)
+ return 0;
+
+ fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
+ if (fd < 0)
return -errno;
- return !!S_ISLNK(info.st_mode);
+ return chattr_fd(fd, value, mask);
}
-int is_dir(const char* path, bool follow) {
+int read_attr_fd(int fd, unsigned *ret) {
struct stat st;
- int r;
- if (follow)
- r = stat(path, &st);
- else
- r = lstat(path, &st);
- if (r < 0)
+ assert(fd >= 0);
+
+ if (fstat(fd, &st) < 0)
return -errno;
- return !!S_ISDIR(st.st_mode);
-}
+ if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
+ return -ENOTTY;
-int unquote_first_word(const char **p, char **ret, bool relax) {
- _cleanup_free_ char *s = NULL;
- size_t allocated = 0, sz = 0;
+ if (ioctl(fd, FS_IOC_GETFLAGS, ret) < 0)
+ return -errno;
- enum {
- START,
- VALUE,
- VALUE_ESCAPE,
- SINGLE_QUOTE,
- SINGLE_QUOTE_ESCAPE,
- DOUBLE_QUOTE,
- DOUBLE_QUOTE_ESCAPE,
- SPACE,
- } state = START;
+ return 0;
+}
+
+int read_attr_path(const char *p, unsigned *ret) {
+ _cleanup_close_ int fd = -1;
assert(p);
- assert(*p);
assert(ret);
- /* Parses the first word of a string, and returns it in
- * *ret. Removes all quotes in the process. When parsing fails
- * (because of an uneven number of quotes or similar), leaves
- * the pointer *p at the first invalid character. */
+ fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
+ if (fd < 0)
+ return -errno;
- for (;;) {
- char c = **p;
+ return read_attr_fd(fd, ret);
+}
- switch (state) {
+static size_t nul_length(const uint8_t *p, size_t sz) {
+ size_t n = 0;
- case START:
- if (c == 0)
- goto finish;
- else if (strchr(WHITESPACE, c))
- break;
+ while (sz > 0) {
+ if (*p != 0)
+ break;
- state = VALUE;
- /* fallthrough */
+ n++;
+ p++;
+ sz--;
+ }
- case VALUE:
- if (c == 0)
- goto finish;
- else if (c == '\'')
- state = SINGLE_QUOTE;
- else if (c == '\\')
- state = VALUE_ESCAPE;
- else if (c == '\"')
- state = DOUBLE_QUOTE;
- else if (strchr(WHITESPACE, c))
- state = SPACE;
- else {
- if (!GREEDY_REALLOC(s, allocated, sz+2))
- return -ENOMEM;
+ return n;
+}
- s[sz++] = c;
- }
+ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
+ const uint8_t *q, *w, *e;
+ ssize_t l;
- break;
+ q = w = p;
+ e = q + sz;
+ while (q < e) {
+ size_t n;
- case VALUE_ESCAPE:
- if (c == 0) {
- if (relax)
- goto finish;
- return -EINVAL;
+ n = nul_length(q, e - q);
+
+ /* If there are more than the specified run length of
+ * NUL bytes, or if this is the beginning or the end
+ * of the buffer, then seek instead of write */
+ if ((n > run_length) ||
+ (n > 0 && q == p) ||
+ (n > 0 && q + n >= e)) {
+ if (q > w) {
+ l = write(fd, w, q - w);
+ if (l < 0)
+ return -errno;
+ if (l != q -w)
+ return -EIO;
}
- if (!GREEDY_REALLOC(s, allocated, sz+2))
- return -ENOMEM;
+ if (lseek(fd, n, SEEK_CUR) == (off_t) -1)
+ return -errno;
- s[sz++] = c;
- state = VALUE;
+ q += n;
+ w = q;
+ } else if (n > 0)
+ q += n;
+ else
+ q ++;
+ }
- break;
+ if (q > w) {
+ l = write(fd, w, q - w);
+ if (l < 0)
+ return -errno;
+ if (l != q - w)
+ return -EIO;
+ }
- case SINGLE_QUOTE:
- if (c == 0) {
- if (relax)
- goto finish;
- return -EINVAL;
- } else if (c == '\'')
- state = VALUE;
- else if (c == '\\')
- state = SINGLE_QUOTE_ESCAPE;
- else {
- if (!GREEDY_REALLOC(s, allocated, sz+2))
- return -ENOMEM;
+ return q - (const uint8_t*) p;
+}
- s[sz++] = c;
- }
+void sigkill_wait(pid_t *pid) {
+ if (!pid)
+ return;
+ if (*pid <= 1)
+ return;
- break;
+ if (kill(*pid, SIGKILL) > 0)
+ (void) wait_for_terminate(*pid, NULL);
+}
- case SINGLE_QUOTE_ESCAPE:
- if (c == 0) {
- if (relax)
- goto finish;
- return -EINVAL;
- }
+int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
+ int a = 0, b = 0, c = 0;
+ int k;
- if (!GREEDY_REALLOC(s, allocated, sz+2))
- return -ENOMEM;
+ assert(p);
+ assert(*p);
+ assert(priority);
- s[sz++] = c;
- state = SINGLE_QUOTE;
- break;
+ if ((*p)[0] != '<')
+ return 0;
- case DOUBLE_QUOTE:
- if (c == 0)
- return -EINVAL;
- else if (c == '\"')
- state = VALUE;
- else if (c == '\\')
- state = DOUBLE_QUOTE_ESCAPE;
- else {
- if (!GREEDY_REALLOC(s, allocated, sz+2))
- return -ENOMEM;
+ if (!strchr(*p, '>'))
+ return 0;
- s[sz++] = c;
- }
+ if ((*p)[2] == '>') {
+ c = undecchar((*p)[1]);
+ k = 3;
+ } else if ((*p)[3] == '>') {
+ b = undecchar((*p)[1]);
+ c = undecchar((*p)[2]);
+ k = 4;
+ } else if ((*p)[4] == '>') {
+ a = undecchar((*p)[1]);
+ b = undecchar((*p)[2]);
+ c = undecchar((*p)[3]);
+ k = 5;
+ } else
+ return 0;
- break;
+ if (a < 0 || b < 0 || c < 0 ||
+ (!with_facility && (a || b || c > 7)))
+ return 0;
- case DOUBLE_QUOTE_ESCAPE:
- if (c == 0) {
- if (relax)
- goto finish;
- return -EINVAL;
- }
+ if (with_facility)
+ *priority = a*100 + b*10 + c;
+ else
+ *priority = (*priority & LOG_FACMASK) | c;
- if (!GREEDY_REALLOC(s, allocated, sz+2))
- return -ENOMEM;
+ *p += k;
+ return 1;
+}
- s[sz++] = c;
- state = DOUBLE_QUOTE;
- break;
+ssize_t string_table_lookup(const char * const *table, size_t len, const char *key) {
+ size_t i;
- case SPACE:
- if (c == 0)
- goto finish;
- if (!strchr(WHITESPACE, c))
- goto finish;
+ if (!key)
+ return -1;
- break;
- }
+ for (i = 0; i < len; ++i)
+ if (streq_ptr(table[i], key))
+ return (ssize_t)i;
- (*p) ++;
- }
+ return -1;
+}
-finish:
- if (!s) {
- *ret = NULL;
- return 0;
- }
+void cmsg_close_all(struct msghdr *mh) {
+ struct cmsghdr *cmsg;
- s[sz] = 0;
- *ret = s;
- s = NULL;
+ assert(mh);
- return 1;
+ for (cmsg = CMSG_FIRSTHDR(mh); cmsg; cmsg = CMSG_NXTHDR(mh, cmsg))
+ if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
+ close_many((int*) CMSG_DATA(cmsg), (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int));
}
-int unquote_many_words(const char **p, ...) {
- va_list ap;
- char **l;
- int n = 0, i, c, r;
+int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
+ struct stat buf;
+ int ret;
- /* Parses a number of words from a string, stripping any
- * quotes if necessary. */
+ ret = renameat2(olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE);
+ if (ret >= 0)
+ return 0;
- assert(p);
+ /* Even though renameat2() exists since Linux 3.15, btrfs added
+ * support for it later. If it is not implemented, fallback to another
+ * method. */
+ if (errno != EINVAL)
+ return -errno;
- /* Count how many words are expected */
- va_start(ap, p);
- for (;;) {
- if (!va_arg(ap, char **))
- break;
- n++;
+ /* The link()/unlink() fallback does not work on directories. But
+ * renameat() without RENAME_NOREPLACE gives the same semantics on
+ * directories, except when newpath is an *empty* directory. This is
+ * good enough. */
+ ret = fstatat(olddirfd, oldpath, &buf, AT_SYMLINK_NOFOLLOW);
+ if (ret >= 0 && S_ISDIR(buf.st_mode)) {
+ ret = renameat(olddirfd, oldpath, newdirfd, newpath);
+ return ret >= 0 ? 0 : -errno;
}
- va_end(ap);
- if (n <= 0)
- return 0;
-
- /* Read all words into a temporary array */
- l = newa0(char*, n);
- for (c = 0; c < n; c++) {
+ /* If it is not a directory, use the link()/unlink() fallback. */
+ ret = linkat(olddirfd, oldpath, newdirfd, newpath, 0);
+ if (ret < 0)
+ return -errno;
- r = unquote_first_word(p, &l[c], false);
- if (r < 0) {
- int j;
+ ret = unlinkat(olddirfd, oldpath, 0);
+ if (ret < 0) {
+ /* backup errno before the following unlinkat() alters it */
+ ret = errno;
+ (void) unlinkat(newdirfd, newpath, 0);
+ errno = ret;
+ return -errno;
+ }
- for (j = 0; j < c; j++)
- free(l[j]);
+ return 0;
+}
- return r;
- }
+char *shell_maybe_quote(const char *s) {
+ const char *p;
+ char *r, *t;
- if (r == 0)
- break;
- }
+ assert(s);
- /* If we managed to parse all words, return them in the passed
- * in parameters */
- va_start(ap, p);
- for (i = 0; i < n; i++) {
- char **v;
+ /* Encloses a string in double quotes if necessary to make it
+ * OK as shell string. */
- v = va_arg(ap, char **);
- assert(v);
+ for (p = s; *p; p++)
+ if (*p <= ' ' ||
+ *p >= 127 ||
+ strchr(SHELL_NEED_QUOTES, *p))
+ break;
- *v = l[i];
- }
- va_end(ap);
+ if (!*p)
+ return strdup(s);
- return c;
-}
+ r = new(char, 1+strlen(s)*2+1+1);
+ if (!r)
+ return NULL;
-int free_and_strdup(char **p, const char *s) {
- char *t;
+ t = r;
+ *(t++) = '"';
+ t = mempcpy(t, s, p - s);
- assert(p);
+ for (; *p; p++) {
- /* Replaces a string pointer with an strdup()ed new string,
- * possibly freeing the old one. */
+ if (strchr(SHELL_NEED_ESCAPE, *p))
+ *(t++) = '\\';
- if (s) {
- t = strdup(s);
- if (!t)
- return -ENOMEM;
- } else
- t = NULL;
+ *(t++) = *p;
+ }
- free(*p);
- *p = t;
+ *(t++)= '"';
+ *t = 0;
- return 0;
+ return r;
}
-int sethostname_idempotent(const char *s) {
- int r;
- char buf[HOST_NAME_MAX + 1] = {};
+int parse_mode(const char *s, mode_t *ret) {
+ char *x;
+ long l;
assert(s);
+ assert(ret);
- r = gethostname(buf, sizeof(buf));
- if (r < 0)
+ errno = 0;
+ l = strtol(s, &x, 8);
+ if (errno != 0)
return -errno;
- if (streq(buf, s))
- return 0;
-
- r = sethostname(s, strlen(s));
- if (r < 0)
- return -errno;
+ if (!x || x == s || *x)
+ return -EINVAL;
+ if (l < 0 || l > 07777)
+ return -ERANGE;
- return 1;
+ *ret = (mode_t) l;
+ return 0;
}