int main(int argc,char* argv[]) { int i; for (i=1; i<argc; ++i) { cescape(argv[i]); } if (argc<2) { char src[1024]; int len=read(0,src,sizeof(src)-1); if (len==-1) return(1); src[len]=0; cescape(src); } return 0; }
int generator_hook_up_growfs( const char *dir, const char *where, const char *target) { _cleanup_free_ char *unit = NULL, *escaped = NULL, *where_unit = NULL; _cleanup_fclose_ FILE *f = NULL; const char *unit_file; int r; escaped = cescape(where); if (!escaped) return log_oom(); r = unit_name_from_path_instance("systemd-growfs", where, ".service", &unit); if (r < 0) return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m", where); r = unit_name_from_path(where, ".mount", &where_unit); if (r < 0) return log_error_errno(r, "Failed to make unit name from path \"%s\": %m", where); unit_file = strjoina(dir, "/", unit); log_debug("Creating %s", unit_file); f = fopen(unit_file, "wxe"); if (!f) return log_error_errno(errno, "Failed to create unit file %s: %m", unit_file); fprintf(f, "# Automatically generated by %s\n\n" "[Unit]\n" "Description=Grow File System on %%f\n" "Documentation=man:[email protected](8)\n" "DefaultDependencies=no\n" "BindsTo=%%i.mount\n" "After=%%i.mount\n" "Before=shutdown.target\n" "Before=%s\n" "\n" "[Service]\n" "Type=oneshot\n" "RemainAfterExit=yes\n" "ExecStart="SYSTEMD_GROWFS_PATH " %s\n" "TimeoutSec=0\n", program_invocation_short_name, target, escaped); return generator_add_symlink(dir, where_unit, "wants", unit); }
static int write_fsck_sysroot_service(const char *dir, const char *what) { _cleanup_free_ char *device = NULL, *escaped = NULL, *escaped2 = NULL; _cleanup_fclose_ FILE *f = NULL; const char *unit; int r; escaped = specifier_escape(what); if (!escaped) return log_oom(); escaped2 = cescape(escaped); if (!escaped2) return log_oom(); unit = strjoina(dir, "/systemd-fsck-root.service"); log_debug("Creating %s", unit); r = unit_name_from_path(what, ".device", &device); if (r < 0) return log_error_errno(r, "Failed to convert device \"%s\" to unit name: %m", what); f = fopen(unit, "wxe"); if (!f) return log_error_errno(errno, "Failed to create unit file %s: %m", unit); fprintf(f, "# Automatically generated by %1$s\n\n" "[Unit]\n" "Description=File System Check on %2$s\n" "Documentation=man:systemd-fsck-root.service(8)\n" "DefaultDependencies=no\n" "BindsTo=%3$s\n" "After=initrd-root-device.target local-fs-pre.target %3$s\n" "Before=shutdown.target\n" "\n" "[Service]\n" "Type=oneshot\n" "RemainAfterExit=yes\n" "ExecStart=" SYSTEMD_FSCK_PATH " %4$s\n" "TimeoutSec=0\n", program_invocation_short_name, escaped, device, escaped2); r = fflush_and_check(f); if (r < 0) return log_error_errno(r, "Failed to write unit file %s: %m", unit); return 0; }
int main(int argc, char *argv[]) { _cleanup_udev_unref_ struct udev *udev = NULL; _cleanup_udev_device_unref_ struct udev_device *device = NULL; _cleanup_free_ char *saved = NULL, *escaped_name = NULL, *escaped_path_id = NULL; const char *name, *path_id; int r; if (argc != 3) { log_error("This program requires two arguments."); return EXIT_FAILURE; } log_set_target(LOG_TARGET_AUTO); log_parse_environment(); log_open(); umask(0022); r = mkdir_p("/var/lib/systemd/rfkill", 0755); if (r < 0) { log_error("Failed to create rfkill directory: %s", strerror(-r)); return EXIT_FAILURE; } udev = udev_new(); if (!udev) { log_oom(); return EXIT_FAILURE; } errno = 0; device = udev_device_new_from_subsystem_sysname(udev, "rfkill", argv[2]); if (!device) { if (errno != 0) log_error("Failed to get rfkill device '%s': %m", argv[2]); else log_oom(); return EXIT_FAILURE; } name = udev_device_get_sysattr_value(device, "name"); if (!name) { log_error("rfkill device has no name?"); return EXIT_FAILURE; } escaped_name = cescape(name); if (!escaped_name) { log_oom(); return EXIT_FAILURE; } path_id = udev_device_get_property_value(device, "ID_PATH"); if (path_id) { escaped_path_id = cescape(path_id); if (!escaped_path_id) { log_oom(); return EXIT_FAILURE; } saved = strjoin("/var/lib/systemd/rfkill/", escaped_path_id, ":", escaped_name, NULL); } else saved = strjoin("/var/lib/systemd/rfkill/", escaped_name, NULL); if (!saved) { log_oom(); return EXIT_FAILURE; } if (streq(argv[1], "load") && restore_state()) { _cleanup_free_ char *value = NULL; r = read_one_line_file(saved, &value); if (r < 0) { if (r == -ENOENT) return EXIT_SUCCESS; log_error("Failed to read %s: %s", saved, strerror(-r)); return EXIT_FAILURE; } r = udev_device_set_sysattr_value(device, "soft", value); if (r < 0) { log_error("Failed to write system attribute: %s", strerror(-r)); return EXIT_FAILURE; } } else if (streq(argv[1], "save")) { const char *value; value = udev_device_get_sysattr_value(device, "soft"); if (!value) { log_error("Failed to read system attribute: %s", strerror(-r)); return EXIT_FAILURE; } r = write_string_file(saved, value); if (r < 0) { log_error("Failed to write %s: %s", saved, strerror(-r)); return EXIT_FAILURE; } } else { log_error("Unknown verb %s.", argv[1]); return EXIT_FAILURE; } return EXIT_SUCCESS; }
static void test_cescape(void) { _cleanup_free_ char *escaped; escaped = cescape("abc\\\"\b\f\n\r\t\v\003\177\234\313"); assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313")); }
int generator_hook_up_mkfs( const char *dir, const char *what, const char *where, const char *type) { _cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL; _cleanup_fclose_ FILE *f = NULL; const char *unit_file; int r; node = fstab_node_to_udev_node(what); if (!node) return log_oom(); /* Nothing to work on. */ if (!is_device_path(node)) { log_error("Cannot format something that is not a device node: %s", node); return -EINVAL; } if (!type || streq(type, "auto")) { log_error("Cannot format partition %s, filesystem type is not specified", node); return -EINVAL; } r = unit_name_from_path_instance("systemd-mkfs", node, ".service", &unit); if (r < 0) return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m", node); unit_file = strjoina(dir, "/", unit); log_debug("Creating %s", unit_file); escaped = cescape(node); if (!escaped) return log_oom(); r = unit_name_from_path(where, ".mount", &where_unit); if (r < 0) return log_error_errno(r, "Failed to make unit name from path \"%s\": %m", where); f = fopen(unit_file, "wxe"); if (!f) return log_error_errno(errno, "Failed to create unit file %s: %m", unit_file); fprintf(f, "# Automatically generated by %s\n\n" "[Unit]\n" "Description=Make File System on %%f\n" "Documentation=man:[email protected](8)\n" "DefaultDependencies=no\n" "BindsTo=%%i.device\n" "After=%%i.device\n" /* fsck might or might not be used, so let's be safe and order * ourselves before both [email protected] and the mount unit. */ "Before=systemd-fsck@%%i.service\n" "Before=%s\n" "Before=shutdown.target\n" "\n" "[Service]\n" "Type=oneshot\n" "RemainAfterExit=yes\n" "ExecStart="SYSTEMD_MAKEFS_PATH " %s %s\n" "TimeoutSec=0\n", program_invocation_short_name, where_unit, type, escaped); // XXX: what about local-fs-pre.target? r = fflush_and_check(f); if (r < 0) return log_error_errno(r, "Failed to write unit file %s: %m", unit_file); return generator_add_symlink(dir, where_unit, "requires", unit); }
int generator_hook_up_mkswap( const char *dir, const char *what) { _cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL; _cleanup_fclose_ FILE *f = NULL; const char *unit_file; int r; node = fstab_node_to_udev_node(what); if (!node) return log_oom(); /* Nothing to work on. */ if (!is_device_path(node)) { log_error("Cannot format something that is not a device node: %s", node); return -EINVAL; } r = unit_name_from_path_instance("systemd-mkswap", node, ".service", &unit); if (r < 0) return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m", node); unit_file = strjoina(dir, "/", unit); log_debug("Creating %s", unit_file); escaped = cescape(node); if (!escaped) return log_oom(); r = unit_name_from_path(what, ".swap", &where_unit); if (r < 0) return log_error_errno(r, "Failed to make unit name from path \"%s\": %m", what); f = fopen(unit_file, "wxe"); if (!f) return log_error_errno(errno, "Failed to create unit file %s: %m", unit_file); fprintf(f, "# Automatically generated by %s\n\n" "[Unit]\n" "Description=Make Swap on %%f\n" "Documentation=man:[email protected](8)\n" "DefaultDependencies=no\n" "BindsTo=%%i.device\n" "After=%%i.device\n" "Before=%s\n" "Before=shutdown.target\n" "\n" "[Service]\n" "Type=oneshot\n" "RemainAfterExit=yes\n" "ExecStart="SYSTEMD_MAKEFS_PATH " swap %s\n" "TimeoutSec=0\n", program_invocation_short_name, where_unit, escaped); r = fflush_and_check(f); if (r < 0) return log_error_errno(r, "Failed to write unit file %s: %m", unit_file); return generator_add_symlink(dir, where_unit, "requires", unit); }
static int stdout_stream_save(StdoutStream *s) { _cleanup_free_ char *temp_path = NULL; _cleanup_fclose_ FILE *f = NULL; int r; assert(s); if (s->state != STDOUT_STREAM_RUNNING) return 0; if (!s->state_file) { struct stat st; r = fstat(s->fd, &st); if (r < 0) return log_warning_errno(errno, "Failed to stat connected stream: %m"); /* We use device and inode numbers as identifier for the stream */ if (asprintf(&s->state_file, "/run/systemd/journal/streams/%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0) return log_oom(); } mkdir_p("/run/systemd/journal/streams", 0755); r = fopen_temporary(s->state_file, &f, &temp_path); if (r < 0) goto fail; fprintf(f, "# This is private data. Do not parse\n" "PRIORITY=%i\n" "LEVEL_PREFIX=%i\n" "FORWARD_TO_SYSLOG=%i\n" "FORWARD_TO_KMSG=%i\n" "FORWARD_TO_CONSOLE=%i\n" "STREAM_ID=%s\n", s->priority, s->level_prefix, s->forward_to_syslog, s->forward_to_kmsg, s->forward_to_console, s->id_field + strlen("_STREAM_ID=")); if (!isempty(s->identifier)) { _cleanup_free_ char *escaped; escaped = cescape(s->identifier); if (!escaped) { r = -ENOMEM; goto fail; } fprintf(f, "IDENTIFIER=%s\n", escaped); } if (!isempty(s->unit_id)) { _cleanup_free_ char *escaped; escaped = cescape(s->unit_id); if (!escaped) { r = -ENOMEM; goto fail; } fprintf(f, "UNIT=%s\n", escaped); } r = fflush_and_check(f); if (r < 0) goto fail; if (rename(temp_path, s->state_file) < 0) { r = -errno; goto fail; } if (!s->fdstore && !s->in_notify_queue) { LIST_PREPEND(stdout_stream_notify_queue, s->server->stdout_streams_notify_queue, s); s->in_notify_queue = true; if (s->server->notify_event_source) { r = sd_event_source_set_enabled(s->server->notify_event_source, SD_EVENT_ON); if (r < 0) log_warning_errno(r, "Failed to enable notify event source: %m"); } } return 0; fail: (void) unlink(s->state_file); if (temp_path) (void) unlink(temp_path); return log_error_errno(r, "Failed to save stream data %s: %m", s->state_file); }