| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "build.h" |
| 7 |
#include "format-table.h" |
| 8 |
#include "help-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "logs-show.h" |
| 11 |
#include "main-func.h" |
| 12 |
#include "networkctl.h" |
| 13 |
#include "networkctl-address-label.h" |
| 14 |
#include "networkctl-config-file.h" |
| 15 |
#include "networkctl-dhcp-lease.h" |
| 16 |
#include "networkctl-list.h" |
| 17 |
#include "networkctl-lldp.h" |
| 18 |
#include "networkctl-misc.h" |
| 19 |
#include "networkctl-status-link.h" |
| 20 |
#include "options.h" |
| 21 |
#include "parse-argument.h" |
| 22 |
#include "parse-util.h" |
| 23 |
#include "path-util.h" |
| 24 |
#include "string-util.h" |
| 25 |
#include "verbs.h" |
| 26 |
|
| 27 |
PagerFlags arg_pager_flags = 0; |
| 28 |
bool arg_legend = true; |
| 29 |
bool arg_no_reload = false; |
| 30 |
bool arg_all = false; |
| 31 |
bool arg_stats = false; |
| 32 |
bool arg_full = false; |
| 33 |
bool arg_runtime = false; |
| 34 |
bool arg_stdin = false; |
| 35 |
unsigned arg_lines = 10; |
| 36 |
char *arg_drop_in = NULL; |
| 37 |
sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; |
| 38 |
bool arg_ask_password = true; |
| 39 |
|
| 40 |
STATIC_DESTRUCTOR_REGISTER(arg_drop_in, freep); |
| 41 |
|
| 42 |
VERB_SCOPE(, verb_list_links, "list", "[PATTERN...]", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, |
| 43 |
"List links"); |
| 44 |
VERB_SCOPE(, verb_link_status, "status", "[PATTERN...]", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, |
| 45 |
"Show link status"); |
| 46 |
VERB_SCOPE(, verb_dhcp_lease, "dhcp-lease", "INTERFACE [CODE[:FORMAT]...]", 2, VERB_ANY, VERB_ONLINE_ONLY, |
| 47 |
"Show DHCP lease"); |
| 48 |
VERB_SCOPE(, verb_link_lldp_status, "lldp", "[PATTERN...]", VERB_ANY, VERB_ANY, 0, |
| 49 |
"Show LLDP neighbors"); |
| 50 |
VERB_SCOPE(, verb_list_address_labels, "label", NULL, 1, 1, 0, |
| 51 |
"Show current address label entries in the kernel"); |
| 52 |
VERB_SCOPE(, verb_link_delete, "delete", "DEVICES...", 2, VERB_ANY, 0, |
| 53 |
"Delete virtual netdevs"); |
| 54 |
VERB_SCOPE(, verb_link_varlink_simple_method, "up", "DEVICES...", 2, VERB_ANY, 0, |
| 55 |
"Bring devices up"); |
| 56 |
VERB_SCOPE(, verb_link_varlink_simple_method, "down", "DEVICES...", 2, VERB_ANY, 0, |
| 57 |
"Bring devices down"); |
| 58 |
VERB_SCOPE(, verb_link_varlink_simple_method, "renew", "DEVICES...", 2, VERB_ANY, VERB_ONLINE_ONLY, |
| 59 |
"Renew dynamic configurations"); |
| 60 |
VERB_SCOPE(, verb_link_varlink_simple_method, "forcerenew", "DEVICES...", 2, VERB_ANY, VERB_ONLINE_ONLY, |
| 61 |
"Trigger DHCP reconfiguration of all connected clients"); |
| 62 |
VERB_SCOPE(, verb_link_varlink_simple_method, "reconfigure", "DEVICES...", 2, VERB_ANY, VERB_ONLINE_ONLY, |
| 63 |
"Reconfigure interfaces"); |
| 64 |
VERB_SCOPE(, verb_reload, "reload", NULL, 1, 1, VERB_ONLINE_ONLY, |
| 65 |
"Reload .network and .netdev files"); |
| 66 |
VERB_SCOPE(, verb_edit, "edit", "FILES|DEVICES...", 2, VERB_ANY, 0, |
| 67 |
"Edit network configuration files"); |
| 68 |
VERB_SCOPE(, verb_cat, "cat", "[FILES|DEVICES...]", 1, VERB_ANY, 0, |
| 69 |
"Show network configuration files"); |
| 70 |
VERB_SCOPE(, verb_mask, "mask", "FILES...", 2, VERB_ANY, 0, |
| 71 |
"Mask network configuration files"); |
| 72 |
VERB_SCOPE(, verb_unmask, "unmask", "FILES...", 2, VERB_ANY, 0, |
| 73 |
"Unmask network configuration files"); |
| 74 |
VERB_SCOPE(, verb_persistent_storage, "persistent-storage", "BOOL", 2, 2, 0, |
| 75 |
"Notify systemd-networkd if persistent storage is ready"); |
| 76 |
|
| 77 |
static int help(void) { |
| 78 |
_cleanup_(table_unrefp) Table *verbs = NULL, *options = NULL; |
| 79 |
int r; |
| 80 |
|
| 81 |
r = verbs_get_help_table(&verbs); |
| 82 |
if (r < 0) |
| 83 |
return r; |
| 84 |
|
| 85 |
r = option_parser_get_help_table(&options); |
| 86 |
if (r < 0) |
| 87 |
return r; |
| 88 |
|
| 89 |
(void) table_sync_column_widths(0, verbs, options); |
| 90 |
|
| 91 |
help_cmdline("[OPTIONS...] COMMAND"); |
| 92 |
help_abstract("Query and control the networking subsystem."); |
| 93 |
|
| 94 |
help_section("Commands"); |
| 95 |
r = table_print_or_warn(verbs); |
| 96 |
if (r < 0) |
| 97 |
return r; |
| 98 |
|
| 99 |
help_section("Options"); |
| 100 |
r = table_print_or_warn(options); |
| 101 |
if (r < 0) |
| 102 |
return r; |
| 103 |
|
| 104 |
help_man_page_reference("networkctl", "1"); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
VERB_COMMON_HELP_HIDDEN(help); |
| 109 |
|
| 110 |
static int parse_argv(int argc, char *argv[], char ***remaining_args) { |
| 111 |
int r; |
| 112 |
|
| 113 |
assert(argc >= 0); |
| 114 |
assert(argv); |
| 115 |
assert(remaining_args); |
| 116 |
|
| 117 |
OptionParser opts = { argc, argv }; |
| 118 |
|
| 119 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 120 |
switch (c) { |
| 121 |
|
| 122 |
OPTION_COMMON_HELP: |
| 123 |
return help(); |
| 124 |
|
| 125 |
OPTION_COMMON_VERSION: |
| 126 |
return version(); |
| 127 |
|
| 128 |
OPTION_COMMON_NO_PAGER: |
| 129 |
arg_pager_flags |= PAGER_DISABLE; |
| 130 |
break; |
| 131 |
|
| 132 |
OPTION_COMMON_NO_LEGEND: |
| 133 |
arg_legend = false; |
| 134 |
break; |
| 135 |
|
| 136 |
OPTION_COMMON_NO_ASK_PASSWORD: |
| 137 |
arg_ask_password = false; |
| 138 |
break; |
| 139 |
|
| 140 |
OPTION('a', "all", NULL, "Show status for all links"): |
| 141 |
arg_all = true; |
| 142 |
break; |
| 143 |
|
| 144 |
OPTION('s', "stats", NULL, "Show detailed link statistics"): |
| 145 |
arg_stats = true; |
| 146 |
break; |
| 147 |
|
| 148 |
OPTION('l', "full", NULL, "Do not ellipsize output"): |
| 149 |
arg_full = true; |
| 150 |
break; |
| 151 |
|
| 152 |
OPTION('n', "lines", "INTEGER", "Number of journal entries to show"): |
| 153 |
r = safe_atou(opts.arg, &arg_lines); |
| 154 |
if (r < 0) |
| 155 |
return log_error_errno(r, "Failed to parse --lines value '%s': %m", opts.arg); |
| 156 |
break; |
| 157 |
|
| 158 |
OPTION_COMMON_JSON: |
| 159 |
r = parse_json_argument(opts.arg, &arg_json_format_flags); |
| 160 |
if (r <= 0) |
| 161 |
return r; |
| 162 |
break; |
| 163 |
|
| 164 |
OPTION_LONG("no-reload", NULL, |
| 165 |
"Do not reload systemd-networkd or systemd-udevd after editing network config"): |
| 166 |
arg_no_reload = true; |
| 167 |
break; |
| 168 |
|
| 169 |
OPTION_LONG("drop-in", "NAME", |
| 170 |
"Edit specified drop-in instead of main config file"): |
| 171 |
if (isempty(opts.arg)) |
| 172 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty drop-in file name."); |
| 173 |
|
| 174 |
if (!endswith(opts.arg, ".conf")) { |
| 175 |
char *conf; |
| 176 |
|
| 177 |
conf = strjoin(opts.arg, ".conf"); |
| 178 |
if (!conf) |
| 179 |
return log_oom(); |
| 180 |
|
| 181 |
free_and_replace(arg_drop_in, conf); |
| 182 |
} else { |
| 183 |
r = free_and_strdup(&arg_drop_in, opts.arg); |
| 184 |
if (r < 0) |
| 185 |
return log_oom(); |
| 186 |
} |
| 187 |
|
| 188 |
if (!filename_is_valid(arg_drop_in)) |
| 189 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 190 |
"Invalid drop-in file name '%s'.", arg_drop_in); |
| 191 |
|
| 192 |
break; |
| 193 |
|
| 194 |
OPTION_LONG("runtime", NULL, "Edit runtime config files"): |
| 195 |
arg_runtime = true; |
| 196 |
break; |
| 197 |
|
| 198 |
OPTION_LONG("stdin", NULL, "Read new contents of edited file from stdin"): |
| 199 |
arg_stdin = true; |
| 200 |
break; |
| 201 |
} |
| 202 |
|
| 203 |
*remaining_args = option_parser_get_args(&opts); |
| 204 |
return 1; |
| 205 |
} |
| 206 |
|
| 207 |
static int run(int argc, char* argv[]) { |
| 208 |
char **args = NULL; |
| 209 |
int r; |
| 210 |
|
| 211 |
log_setup(); |
| 212 |
|
| 213 |
r = parse_argv(argc, argv, &args); |
| 214 |
if (r <= 0) |
| 215 |
return r; |
| 216 |
|
| 217 |
journal_browse_prepare(); |
| 218 |
|
| 219 |
return dispatch_verb(args, NULL); |
| 220 |
} |
| 221 |
|
| 222 |
DEFINE_MAIN_FUNCTION(run); |