return tmp_dir_internal("/tmp", ret);
}
+int unlink_or_warn(const char *filename) {
+ if (unlink(filename) < 0 && errno != ENOENT)
+ /* If the file doesn't exist and the fs simply was read-only (in which
+ * case unlink() returns EROFS even if the file doesn't exist), don't
+ * complain */
+ if (errno != EROFS || access(filename, F_OK) >= 0)
+ return log_error_errno(errno, "Failed to remove \"%s\": %m", filename);
+
+ return 0;
+}
+
int inotify_add_watch_fd(int fd, int what, uint32_t mask) {
char path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
int r;
int tmp_dir(const char **ret);
int var_tmp_dir(const char **ret);
+int unlink_or_warn(const char *filename);
+
#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
m->shutdown_dry_run = false;
if (m->unlink_nologin) {
- (void) unlink("/run/nologin");
+ (void) unlink_or_warn("/run/nologin");
m->unlink_nologin = false;
}
#include "dirent-util.h"
#include "fd-util.h"
#include "format-util.h"
+#include "fs-util.h"
#include "logind.h"
#include "process-util.h"
#include "selinux-util.h"
udev_unref(m->udev);
if (m->unlink_nologin)
- (void) unlink("/run/nologin");
+ (void) unlink_or_warn("/run/nologin");
bus_verify_polkit_async_registry_free(m->polkit_registry);
#include "fileio.h"
#include "fileio-label.h"
+#include "fs-util.h"
#include "log.h"
#include "selinux-util.h"
#include "string-util.h"
#include "util.h"
int main(int argc, char*argv[]) {
+ int r, k;
if (argc != 2) {
log_error("This program requires one argument.");
mac_selinux_init();
if (streq(argv[1], "start")) {
- int r = 0;
-
- if (unlink("/run/nologin") < 0 && errno != ENOENT)
- r = log_error_errno(errno,
- "Failed to remove /run/nologin file: %m");
-
- if (unlink("/etc/nologin") < 0 && errno != ENOENT) {
- /* If the file doesn't exist and /etc simply
- * was read-only (in which case unlink()
- * returns EROFS even if the file doesn't
- * exist), don't complain */
-
- if (errno != EROFS || access("/etc/nologin", F_OK) >= 0) {
- log_error_errno(errno, "Failed to remove /etc/nologin file: %m");
- return EXIT_FAILURE;
- }
- }
-
- if (r < 0)
+ r = unlink_or_warn("/run/nologin");
+ k = unlink_or_warn("/etc/nologin");
+ if (r < 0 || k < 0)
return EXIT_FAILURE;
} else if (streq(argv[1], "stop")) {
- int r;
-
r = write_string_file_atomic_label("/run/nologin", "System is going down.");
if (r < 0) {
log_error_errno(r, "Failed to create /run/nologin: %m");