| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "build.h" |
| 7 |
#include "creds-util.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "format-table.h" |
| 11 |
#include "fs-util.h" |
| 12 |
#include "generator.h" |
| 13 |
#include "log.h" |
| 14 |
#include "main-func.h" |
| 15 |
#include "mkdir.h" |
| 16 |
#include "network-generator.h" |
| 17 |
#include "options.h" |
| 18 |
#include "path-util.h" |
| 19 |
#include "proc-cmdline.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "strv.h" |
| 22 |
|
| 23 |
#define NETWORK_UNIT_DIRECTORY "/run/systemd/network/" |
| 24 |
|
| 25 |
static const char *arg_root = NULL; |
| 26 |
|
| 27 |
static int network_save(Network *network, const char *dest_dir) { |
| 28 |
_cleanup_(unlink_and_freep) char *temp_path = NULL; |
| 29 |
_cleanup_fclose_ FILE *f = NULL; |
| 30 |
_cleanup_free_ char *p = NULL; |
| 31 |
int r; |
| 32 |
|
| 33 |
assert(network); |
| 34 |
|
| 35 |
r = generator_open_unit_file_full( |
| 36 |
dest_dir, |
| 37 |
/* source= */ NULL, |
| 38 |
/* name= */ NULL, |
| 39 |
&f, |
| 40 |
/* ret_final_path= */ NULL, |
| 41 |
&temp_path); |
| 42 |
if (r < 0) |
| 43 |
return r; |
| 44 |
|
| 45 |
network_dump(network, f); |
| 46 |
|
| 47 |
if (asprintf(&p, "%s/%s-%s.network", |
| 48 |
dest_dir, |
| 49 |
isempty(network->ifname) ? "71" : "70", |
| 50 |
isempty(network->ifname) ? "default" : network->ifname) < 0) |
| 51 |
return log_oom(); |
| 52 |
|
| 53 |
r = conservative_rename(temp_path, p); |
| 54 |
if (r < 0) |
| 55 |
return log_error_errno(r, "Failed to rename '%s' to '%s': %m", temp_path, p); |
| 56 |
|
| 57 |
temp_path = mfree(temp_path); |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static int netdev_save(NetDev *netdev, const char *dest_dir) { |
| 62 |
_cleanup_(unlink_and_freep) char *temp_path = NULL; |
| 63 |
_cleanup_fclose_ FILE *f = NULL; |
| 64 |
_cleanup_free_ char *p = NULL; |
| 65 |
int r; |
| 66 |
|
| 67 |
assert(netdev); |
| 68 |
|
| 69 |
r = generator_open_unit_file_full( |
| 70 |
dest_dir, |
| 71 |
/* source= */ NULL, |
| 72 |
/* name= */ NULL, |
| 73 |
&f, |
| 74 |
/* ret_final_path= */ NULL, |
| 75 |
&temp_path); |
| 76 |
if (r < 0) |
| 77 |
return r; |
| 78 |
|
| 79 |
netdev_dump(netdev, f); |
| 80 |
|
| 81 |
if (asprintf(&p, "%s/70-%s.netdev", dest_dir, netdev->ifname) < 0) |
| 82 |
return log_oom(); |
| 83 |
|
| 84 |
r = conservative_rename(temp_path, p); |
| 85 |
if (r < 0) |
| 86 |
return log_error_errno(r, "Failed to rename '%s' to '%s': %m", temp_path, p); |
| 87 |
|
| 88 |
temp_path = mfree(temp_path); |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
static int link_save(Link *link, const char *dest_dir) { |
| 93 |
_cleanup_(unlink_and_freep) char *temp_path = NULL; |
| 94 |
_cleanup_fclose_ FILE *f = NULL; |
| 95 |
_cleanup_free_ char *p = NULL; |
| 96 |
int r; |
| 97 |
|
| 98 |
assert(link); |
| 99 |
|
| 100 |
r = generator_open_unit_file_full( |
| 101 |
dest_dir, |
| 102 |
/* source= */ NULL, |
| 103 |
/* name= */ NULL, |
| 104 |
&f, |
| 105 |
/* ret_final_path= */ NULL, |
| 106 |
&temp_path); |
| 107 |
if (r < 0) |
| 108 |
return r; |
| 109 |
|
| 110 |
link_dump(link, f); |
| 111 |
|
| 112 |
if (asprintf(&p, "%s/%s-%s.link", |
| 113 |
dest_dir, |
| 114 |
!isempty(link->ifname) ? "70" : !hw_addr_is_null(&link->mac) ? "71" : "72", |
| 115 |
link->filename) < 0) |
| 116 |
return log_oom(); |
| 117 |
|
| 118 |
r = conservative_rename(temp_path, p); |
| 119 |
if (r < 0) |
| 120 |
return log_error_errno(r, "Failed to rename '%s' to '%s': %m", temp_path, p); |
| 121 |
|
| 122 |
temp_path = mfree(temp_path); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static int context_save(Context *context) { |
| 127 |
Network *network; |
| 128 |
NetDev *netdev; |
| 129 |
Link *link; |
| 130 |
int r; |
| 131 |
|
| 132 |
_cleanup_free_ char *p = path_join(arg_root, NETWORK_UNIT_DIRECTORY); |
| 133 |
if (!p) |
| 134 |
return log_oom(); |
| 135 |
|
| 136 |
r = mkdir_p(p, 0755); |
| 137 |
if (r < 0) |
| 138 |
return log_error_errno(r, "Failed to create directory " NETWORK_UNIT_DIRECTORY ": %m"); |
| 139 |
|
| 140 |
HASHMAP_FOREACH(network, context->networks_by_name) |
| 141 |
RET_GATHER(r, network_save(network, p)); |
| 142 |
|
| 143 |
HASHMAP_FOREACH(netdev, context->netdevs_by_name) |
| 144 |
RET_GATHER(r, netdev_save(netdev, p)); |
| 145 |
|
| 146 |
HASHMAP_FOREACH(link, context->links_by_filename) |
| 147 |
RET_GATHER(r, link_save(link, p)); |
| 148 |
|
| 149 |
return r; |
| 150 |
} |
| 151 |
|
| 152 |
static int help(void) { |
| 153 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 154 |
int r; |
| 155 |
|
| 156 |
r = option_parser_get_help_table(&options); |
| 157 |
if (r < 0) |
| 158 |
return r; |
| 159 |
|
| 160 |
printf("%s [OPTIONS...] [-- KERNEL_CMDLINE]\n\n", |
| 161 |
program_invocation_short_name); |
| 162 |
|
| 163 |
r = table_print_or_warn(options); |
| 164 |
if (r < 0) |
| 165 |
return r; |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
static int parse_argv(int argc, char *argv[], char ***ret_args) { |
| 171 |
assert(argc >= 0); |
| 172 |
assert(argv); |
| 173 |
assert(ret_args); |
| 174 |
|
| 175 |
OptionParser opts = { argc, argv }; |
| 176 |
|
| 177 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 178 |
switch (c) { |
| 179 |
|
| 180 |
OPTION_COMMON_HELP: |
| 181 |
return help(); |
| 182 |
|
| 183 |
OPTION_COMMON_VERSION: |
| 184 |
return version(); |
| 185 |
|
| 186 |
OPTION_LONG("root", "PATH", |
| 187 |
"Operate on an alternate filesystem root"): |
| 188 |
arg_root = opts.arg; |
| 189 |
break; |
| 190 |
} |
| 191 |
|
| 192 |
*ret_args = option_parser_get_args(&opts); |
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
static int run(int argc, char *argv[]) { |
| 197 |
_cleanup_(context_clear) Context context = {}; |
| 198 |
int r, ret = 0; |
| 199 |
|
| 200 |
log_setup(); |
| 201 |
|
| 202 |
umask(0022); |
| 203 |
|
| 204 |
char **args = NULL; |
| 205 |
r = parse_argv(argc, argv, &args); |
| 206 |
if (r <= 0) |
| 207 |
return r; |
| 208 |
|
| 209 |
if (strv_isempty(args)) { |
| 210 |
r = proc_cmdline_parse(parse_cmdline_item, &context, 0); |
| 211 |
if (r < 0) |
| 212 |
return log_warning_errno(r, "Failed to parse kernel command line: %m"); |
| 213 |
} else { |
| 214 |
STRV_FOREACH(a, args) { |
| 215 |
_cleanup_free_ char *word = NULL; |
| 216 |
char *value; |
| 217 |
|
| 218 |
word = strdup(*a); |
| 219 |
if (!word) |
| 220 |
return log_oom(); |
| 221 |
|
| 222 |
value = strchr(word, '='); |
| 223 |
if (value) |
| 224 |
*(value++) = 0; |
| 225 |
|
| 226 |
r = parse_cmdline_item(word, value, &context); |
| 227 |
if (r < 0) |
| 228 |
return log_warning_errno(r, "Failed to parse command line \"%s%s%s\": %m", |
| 229 |
word, value ? "=" : "", strempty(value)); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
context_finalize_bootif(&context); |
| 234 |
|
| 235 |
r = context_merge_networks(&context); |
| 236 |
if (r < 0) |
| 237 |
return log_warning_errno(r, "Failed to merge multiple command line options: %m"); |
| 238 |
|
| 239 |
RET_GATHER(ret, context_save(&context)); |
| 240 |
|
| 241 |
static const PickUpCredential table[] = { |
| 242 |
{ "network.conf.", "/run/systemd/networkd.conf.d/", ".conf" }, |
| 243 |
{ "network.link.", NETWORK_UNIT_DIRECTORY, ".link" }, |
| 244 |
{ "network.netdev.", NETWORK_UNIT_DIRECTORY, ".netdev" }, |
| 245 |
{ "network.network.", NETWORK_UNIT_DIRECTORY, ".network" }, |
| 246 |
}; |
| 247 |
RET_GATHER(ret, pick_up_credentials(table, ELEMENTSOF(table))); |
| 248 |
|
| 249 |
return ret; |
| 250 |
} |
| 251 |
|
| 252 |
DEFINE_MAIN_FUNCTION(run); |