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;
return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
}
-int readlink_malloc(const char *p, char **r) {
+int readlink_malloc(const char *p, char **ret) {
size_t l = 100;
+ int r;
assert(p);
- assert(r);
+ assert(ret);
for (;;) {
char *c;
ssize_t n;
- if (!(c = new(char, l)))
+ c = new(char, l);
+ if (!c)
return -ENOMEM;
- if ((n = readlink(p, c, l-1)) < 0) {
- int ret = -errno;
+ n = readlink(p, c, l-1);
+ if (n < 0) {
+ r = -errno;
free(c);
- return ret;
+ return r;
}
if ((size_t) n < l-1) {
c[n] = 0;
- *r = c;
+ *ret = c;
return 0;
}
if (!saved_argv[i])
break;
- memset(saved_argv[i], 0, strlen(saved_argv[i]));
+ memzero(saved_argv[i], strlen(saved_argv[i]));
}
}
}
assert(mode);
assert(_f);
- if (!path_strv_canonicalize_uniq(search))
+ if (!path_strv_canonicalize_absolute_uniq(search, NULL))
return -ENOMEM;
STRV_FOREACH(i, search) {
return NULL;
if (*allocated > prev)
- memset(&q[prev], 0, *allocated - prev);
+ memzero(&q[prev], *allocated - prev);
return q;
}
return true;
}
-void parse_user_at_host(char *arg, char **user, char **host) {
- assert(arg);
- assert(user);
- assert(host);
-
- *host = strchr(arg, '@');
- if (*host == NULL)
- *host = arg;
- else {
- *host[0]++ = '\0';
- *user = arg;
- }
-}
-
int split_pair(const char *s, const char *sep, char **l, char **r) {
char *x, *a, *b;
return 0;
}
-int writev_safe(int fd, const struct iovec *w, int j) {
- for (int i = 0; i < j; i++) {
- size_t written = 0;
-
- while (written < w[i].iov_len) {
- ssize_t r;
-
- r = write(fd, (char*) w[i].iov_base + written, w[i].iov_len - written);
- if (r < 0 && errno != -EINTR)
- return -errno;
-
- written += r;
- }
- }
-
- return 0;
-}
-
+/* This is much like like mkostemp() but is subject to umask(). */
int mkostemp_safe(char *pattern, int flags) {
- unsigned long tries = TMP_MAX;
- char *s;
- int r;
_cleanup_umask_ mode_t u;
+ int fd;
assert(pattern);
u = umask(077);
- /* This is much like like mkostemp() but avoids using any
- * static variables, thus is async signal safe. Also, it's not
- * subject to umask(). */
-
- s = endswith(pattern, "XXXXXX");
- if (!s)
- return -EINVAL;
-
- while (tries--) {
- unsigned i;
- int fd;
-
- r = dev_urandom(s, 6);
- if (r < 0)
- return r;
-
- for (i = 0; i < 6; i++)
- s[i] = ALPHANUMERICAL[(unsigned) s[i] % (sizeof(ALPHANUMERICAL)-1)];
-
- fd = open(pattern, flags|O_EXCL|O_CREAT|O_NOCTTY|O_NOFOLLOW, S_IRUSR|S_IWUSR);
- if (fd >= 0)
- return fd;
- if (!IN_SET(errno, EEXIST, EINTR))
- return -errno;
- }
+ fd = mkostemp(pattern, flags);
+ if (fd < 0)
+ return -errno;
- return -EEXIST;
+ return fd;
}
int open_tmpfile(const char *path, int flags) {
unlink(p);
return fd;
}
+
+int fd_warn_permissions(const char *path, int fd) {
+ struct stat st;
+
+ if (fstat(fd, &st) < 0)
+ return -errno;
+
+ if (st.st_mode & 0111)
+ log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path);
+
+ if (st.st_mode & 0002)
+ log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path);
+
+ 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);
+
+ return 0;
+}