00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <netlink/cli/utils.h>
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 uint32_t nl_cli_parse_u32(const char *arg)
00036 {
00037 unsigned long lval;
00038 char *endptr;
00039
00040 lval = strtoul(arg, &endptr, 0);
00041 if (endptr == arg || lval == ULONG_MAX)
00042 nl_cli_fatal(EINVAL, "Unable to parse \"%s\", not a number.",
00043 arg);
00044
00045 return (uint32_t) lval;
00046 }
00047
00048 void nl_cli_print_version(void)
00049 {
00050 printf("libnl tools version %s\n", LIBNL_VERSION);
00051 printf(
00052 "Copyright (C) 2003-2010 Thomas Graf <tgraf@redhat.com>\n"
00053 "\n"
00054 "This program comes with ABSOLUTELY NO WARRANTY. This is free \n"
00055 "software, and you are welcome to redistribute it under certain\n"
00056 "conditions. See the GNU General Public License for details.\n"
00057 );
00058
00059 exit(0);
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 void nl_cli_fatal(int err, const char *fmt, ...)
00071 {
00072 va_list ap;
00073
00074 fprintf(stderr, "Error: ");
00075
00076 if (fmt) {
00077 va_start(ap, fmt);
00078 vfprintf(stderr, fmt, ap);
00079 va_end(ap);
00080 fprintf(stderr, "\n");
00081 } else
00082 fprintf(stderr, "%s\n", strerror(err));
00083
00084 exit(abs(err));
00085 }
00086
00087 int nl_cli_connect(struct nl_sock *sk, int protocol)
00088 {
00089 int err;
00090
00091 if ((err = nl_connect(sk, protocol)) < 0)
00092 nl_cli_fatal(err, "Unable to connect netlink socket: %s",
00093 nl_geterror(err));
00094
00095 return err;
00096 }
00097
00098 struct nl_sock *nl_cli_alloc_socket(void)
00099 {
00100 struct nl_sock *sock;
00101
00102 if (!(sock = nl_socket_alloc()))
00103 nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
00104
00105 return sock;
00106 }
00107
00108 struct nl_addr *nl_cli_addr_parse(const char *str, int family)
00109 {
00110 struct nl_addr *addr;
00111 int err;
00112
00113 if ((err = nl_addr_parse(str, family, &addr)) < 0)
00114 nl_cli_fatal(err, "Unable to parse address \"%s\": %s",
00115 str, nl_geterror(err));
00116
00117 return addr;
00118 }
00119
00120 int nl_cli_parse_dumptype(const char *str)
00121 {
00122 if (!strcasecmp(str, "brief"))
00123 return NL_DUMP_LINE;
00124 else if (!strcasecmp(str, "details") || !strcasecmp(str, "detailed"))
00125 return NL_DUMP_DETAILS;
00126 else if (!strcasecmp(str, "stats"))
00127 return NL_DUMP_STATS;
00128 else
00129 nl_cli_fatal(EINVAL, "Invalid dump type \"%s\".\n", str);
00130
00131 return 0;
00132 }
00133
00134 int nl_cli_confirm(struct nl_object *obj, struct nl_dump_params *params,
00135 int default_yes)
00136 {
00137 nl_object_dump(obj, params);
00138
00139 for (;;) {
00140 char buf[32] = { 0 };
00141 int answer;
00142
00143 printf("Delete? (%c/%c) ",
00144 default_yes ? 'Y' : 'y',
00145 default_yes ? 'n' : 'N');
00146
00147 if (!fgets(buf, sizeof(buf), stdin)) {
00148 fprintf(stderr, "Error while reading\n.");
00149 continue;
00150 }
00151
00152 switch ((answer = tolower(buf[0]))) {
00153 case '\n':
00154 answer = default_yes ? 'y' : 'n';
00155 case 'y':
00156 case 'n':
00157 return answer == 'y';
00158 }
00159
00160 fprintf(stderr, "Invalid input, try again.\n");
00161 }
00162
00163 return 0;
00164
00165 }
00166
00167 struct nl_cache *nl_cli_alloc_cache(struct nl_sock *sock, const char *name,
00168 int (*ac)(struct nl_sock *, struct nl_cache **))
00169 {
00170 struct nl_cache *cache;
00171 int err;
00172
00173 if ((err = ac(sock, &cache)) < 0)
00174 nl_cli_fatal(err, "Unable to allocate %s cache: %s",
00175 name, nl_geterror(err));
00176
00177 nl_cache_mngt_provide(cache);
00178
00179 return cache;
00180 }
00181
00182 void nl_cli_load_module(const char *prefix, const char *name)
00183 {
00184 char path[FILENAME_MAX+1];
00185 void *handle;
00186
00187 snprintf(path, sizeof(path), "%s/%s/%s.so",
00188 PKGLIBDIR, prefix, name);
00189
00190 if (!(handle = dlopen(path, RTLD_NOW)))
00191 nl_cli_fatal(ENOENT, "Unable to load module \"%s\": %s\n",
00192 path, dlerror());
00193 }
00194
00195