chiark / gitweb /
fileio: imply /tmp as directory if passed as NULL to open_tmpfile_unlinkable()
[elogind.git] / src / basic / util.c
index 998fa7f1e25babb80ee675baa39dd5fa2a86a5fc..12f7e1b0512a900e9335582ae1045beb82e1f6c2 100644 (file)
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
 /***
   This file is part of systemd.
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-//#include <ctype.h>
+#include <alloca.h>
 //#include <dirent.h>
 //#include <errno.h>
 //#include <fcntl.h>
-//#include <grp.h>
-//#include <langinfo.h>
-//#include <libintl.h>
-//#include <limits.h>
-//#include <linux/magic.h>
-//#include <linux/oom.h>
-//#include <linux/sched.h>
-//#include <locale.h>
-//#include <poll.h>
-//#include <pwd.h>
 #include <sched.h>
 //#include <signal.h>
 //#include <stdarg.h>
 //#include <stdio.h>
-//#include <stdlib.h>
+#include <stdlib.h>
 //#include <string.h>
-//#include <sys/file.h>
-//#include <sys/ioctl.h>
 //#include <sys/mman.h>
-//#include <sys/mount.h>
-//#include <sys/personality.h>
 #include <sys/prctl.h>
-//#include <sys/stat.h>
-//#include <sys/statvfs.h>
-//#include <sys/time.h>
+#include <sys/statfs.h>
+#include <sys/sysmacros.h>
 //#include <sys/types.h>
-//#include <sys/utsname.h>
-//#include <sys/vfs.h>
-//#include <sys/wait.h>
-//#include <syslog.h>
 //#include <unistd.h>
 
-/* When we include libgen.h because we need dirname() we immediately
- * undefine basename() since libgen.h defines it as a macro to the
- * POSIX version which is really broken. We prefer GNU basename(). */
-//#include <libgen.h>
-//#undef basename
-
-#ifdef HAVE_SYS_AUXV_H
-#include <sys/auxv.h>
-#endif
-
-/* We include linux/fs.h as last of the system headers, as it
- * otherwise conflicts with sys/mount.h. Yay, Linux is great! */
-//#include <linux/fs.h>
-
 #include "alloc-util.h"
 #include "build.h"
 //#include "def.h"
-//#include "device-nodes.h"
 #include "dirent-util.h"
-//#include "env-util.h"
-//#include "escape.h"
-//#include "exit-status.h"
 #include "fd-util.h"
 #include "fileio.h"
 //#include "formats-util.h"
-//#include "gunicode.h"
 #include "hashmap.h"
-//#include "hexdecoct.h"
 #include "hostname-util.h"
-//#include "ioprio.h"
 //#include "log.h"
 #include "macro.h"
 //#include "missing.h"
-//#include "mkdir.h"
 #include "parse-util.h"
 //#include "path-util.h"
 #include "process-util.h"
-//#include "random-util.h"
-#include "signal-util.h"
 #include "set.h"
-//#include "sparse-endian.h"
+#include "signal-util.h"
 #include "stat-util.h"
-//#include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
-//#include "terminal-util.h"
+#include "time-util.h"
+#include "umask-util.h"
 #include "user-util.h"
-//#include "utf8.h"
 #include "util.h"
-//#include "virt.h"
 
 /* Put this test here for a lack of better place */
 assert_cc(EAGAIN == EWOULDBLOCK);
@@ -460,10 +413,10 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
 
                 /* Detach from stdout/stderr. and reopen
                  * /dev/tty for them. This is important to
-                 * ensure that when systemctl is started via
+                 * ensure that when loginctl 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
+                 * not delay this indefinitely because we
                  * keep an unused copy of stdin around. */
                 fd = open("/dev/tty", O_WRONLY);
                 if (fd < 0) {
@@ -471,13 +424,17 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
                         _exit(EXIT_FAILURE);
                 }
 
-                if (!stdout_is_tty)
-                        dup2(fd, STDOUT_FILENO);
+                if (!stdout_is_tty && dup2(fd, STDOUT_FILENO) < 0) {
+                        log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+                        _exit(EXIT_FAILURE);
+                }
 
-                if (!stderr_is_tty)
-                        dup2(fd, STDERR_FILENO);
+                if (!stderr_is_tty && dup2(fd, STDERR_FILENO) < 0) {
+                        log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+                        _exit(EXIT_FAILURE);
+                }
 
-                if (fd > 2)
+                if (fd > STDERR_FILENO)
                         close(fd);
         }
 
@@ -570,7 +527,7 @@ int on_ac_power(void) {
                 if (!de)
                         break;
 
-                if (hidden_file(de->d_name))
+                if (hidden_or_backup_file(de->d_name))
                         continue;
 
                 device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
@@ -828,15 +785,25 @@ uint64_t physical_memory(void) {
 }
 
 #if 0 /// UNNEEDED by elogind
-int update_reboot_param_file(const char *param) {
-        int r = 0;
+int update_reboot_parameter_and_warn(const char *param) {
+        int r;
+
+        if (isempty(param)) {
+                if (unlink("/run/systemd/reboot-param") < 0) {
+                        if (errno == ENOENT)
+                                return 0;
+
+                        return log_warning_errno(errno, "Failed to unlink reboot parameter file: %m");
+                }
 
-        if (param) {
-                r = write_string_file(REBOOT_PARAM_FILE, param, WRITE_STRING_FILE_CREATE);
+                return 0;
+        }
+
+        RUN_WITH_UMASK(0022) {
+                r = write_string_file("/run/systemd/reboot-param", param, WRITE_STRING_FILE_CREATE);
                 if (r < 0)
-                        return log_error_errno(r, "Failed to write reboot param to "REBOOT_PARAM_FILE": %m");
-        } else
-                (void) unlink(REBOOT_PARAM_FILE);
+                        return log_warning_errno(r, "Failed to write reboot parameter file: %m");
+        }
 
         return 0;
 }