blob: feb7eed07efc3b28edf39e3418d313ce22f2f57c [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <sys/types.h>
19#include <sys/mman.h>
20#include <sys/stat.h>
Dave Barach98cfc1a2016-07-18 14:23:36 -040021#include <pwd.h>
22#include <grp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023#include <netinet/in.h>
24#include <signal.h>
25#include <pthread.h>
26#include <unistd.h>
27#include <time.h>
28#include <fcntl.h>
29#include <string.h>
30#include <vppinfra/clib.h>
31#include <vppinfra/vec.h>
32#include <vppinfra/hash.h>
33#include <vppinfra/bitmap.h>
34#include <vppinfra/fifo.h>
35#include <vppinfra/time.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070036#include <vppinfra/heap.h>
37#include <vppinfra/pool.h>
38#include <vppinfra/format.h>
39#include <vppinfra/serialize.h>
40#include "svmdb.h"
41
Dave Barach98cfc1a2016-07-18 14:23:36 -040042typedef struct
43{
44 svmdb_map_args_t map_args;
45 int uid, gid;
46 uword size;
47} svmdbtool_main_t;
48
49svmdbtool_main_t svmdbtool_main;
50
Ed Warnicke853e7202016-08-12 11:42:26 -070051static inline svmdb_map_args_t *
52map_arg_setup (char *chroot_path)
Dave Barach98cfc1a2016-07-18 14:23:36 -040053{
54 svmdbtool_main_t *sm = &svmdbtool_main;
55 svmdb_map_args_t *ma = &sm->map_args;
56
Dave Barachb7b92992018-10-17 10:38:51 -040057 clib_memset (ma, 0, sizeof (*ma));
Dave Barach98cfc1a2016-07-18 14:23:36 -040058 ma->root_path = chroot_path;
59 ma->size = sm->size;
60 ma->uid = sm->uid;
61 ma->gid = sm->gid;
62 return ma;
63}
64
Dave Barach8a7fb0c2016-07-08 14:44:23 -040065static void
66get_string (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -070067{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040068 svmdb_client_t *c;
69 char *rv;
Dave Barach98cfc1a2016-07-18 14:23:36 -040070 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
Dave Barach98cfc1a2016-07-18 14:23:36 -040072 ma = map_arg_setup (chroot_path);
73
74 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
Dave Barach8a7fb0c2016-07-08 14:44:23 -040076 rv = svmdb_local_get_string_variable (c, (char *) vbl);
77
78 fformat (stdout, "%s\n", rv ? rv : "UNSET");
79 vec_free (rv);
80 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -070081}
82
Dave Barach8a7fb0c2016-07-08 14:44:23 -040083static void
84set_string (char *chroot_path, u8 * vbl, u8 * value)
Ed Warnickecb9cada2015-12-08 15:45:58 -070085{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040086 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -040087 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
Dave Barach98cfc1a2016-07-18 14:23:36 -040089 ma = map_arg_setup (chroot_path);
90
91 c = svmdb_map (ma);
Dave Barach8a7fb0c2016-07-08 14:44:23 -040092 svmdb_local_set_string_variable (c, (char *) vbl, (char *) value);
93 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -070094}
95
Dave Barach8a7fb0c2016-07-08 14:44:23 -040096static void
97unset_string (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -070098{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040099 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400100 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Dave Barach98cfc1a2016-07-18 14:23:36 -0400102 ma = map_arg_setup (chroot_path);
103
104 c = svmdb_map (ma);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400105 svmdb_local_unset_string_variable (c, (char *) vbl);
106 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107}
108
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400109static void
110dump_strings (char *chroot_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400112 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400113 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
Dave Barach98cfc1a2016-07-18 14:23:36 -0400115 ma = map_arg_setup (chroot_path);
116
117 c = svmdb_map (ma);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400118 svmdb_local_dump_strings (c);
119 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120}
121
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400122static void
Dave Barach23a74122016-11-24 16:34:20 -0500123serialize_strings (char *chroot_path, char *filename)
124{
125 svmdb_client_t *c;
126 svmdb_map_args_t *ma;
127
128 ma = map_arg_setup (chroot_path);
129
130 c = svmdb_map (ma);
131 (void) svmdb_local_serialize_strings (c, filename);
132 svmdb_unmap (c);
133}
134
135static void
136unserialize_strings (char *chroot_path, char *filename)
137{
138 svmdb_client_t *c;
139 svmdb_map_args_t *ma;
140
141 ma = map_arg_setup (chroot_path);
142
143 c = svmdb_map (ma);
144 (void) svmdb_local_unserialize_strings (c, filename);
145 svmdb_unmap (c);
146}
147
148static void
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400149test_vlib_vec_rate (char *chroot_path, f64 vr)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400151 svmdb_client_t *c;
152 f64 *tv = 0;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400153 svmdb_map_args_t *ma;
154
155 ma = map_arg_setup (chroot_path);
156
157 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400159 vec_add1 (tv, vr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400161 svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv,
162 sizeof (*tv));
163 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400165 vec_free (tv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166}
167
168
169
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400170static void
171test_vec (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400173 svmdb_client_t *c;
174 u64 *tv = 0;
175 int i;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400176 svmdb_map_args_t *ma;
177
178 ma = map_arg_setup (chroot_path);
179
180 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400182 /* my amp goes to 11 */
183 for (i = 0; i < 11; i++)
184 {
185 vec_add1 (tv, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 }
187
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400188 svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0]));
189 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400191 vec_free (tv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192}
193
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400194static void
195fake_install (char *chroot_path, u8 * add_value)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400197 svmdb_client_t *c;
198 u8 *v = 0;
199 u8 **values = 0;
200 u8 *oldvalue;
201 u8 *value;
202 int nitems = 0, i;
203 serialize_main_t m;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400204 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Dave Barach98cfc1a2016-07-18 14:23:36 -0400206 ma = map_arg_setup (chroot_path);
207
208 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400210 oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
211 if (oldvalue)
212 {
213 unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
214 nitems = unserialize_likely_small_unsigned_integer (&m);
215 for (i = 0; i < nitems; i++)
216 {
217 unserialize_cstring (&m, (char **) &value);
218 vec_add1 (values, value);
219 }
220 vec_free (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400222 nitems++;
223 value = format (0, "%s%c", add_value, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400225 vec_add1 (values, value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400227 fformat (stdout, "Resulting installed_sw vector:\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400229 serialize_open_vector (&m, v);
230 serialize_likely_small_unsigned_integer (&m, vec_len (values));
231 for (i = 0; i < vec_len (values); i++)
232 {
233 fformat (stdout, "%s\n", values[i]);
234 serialize_cstring (&m, (char *) values[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 }
236
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400237 v = serialize_close_vector (&m);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400239 svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
240 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400242 for (i = 0; i < vec_len (values); i++)
243 vec_free (values[i]);
244 vec_free (values);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245}
246
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400247static void
248sigaction_handler (int signum, siginfo_t * i, void *notused)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400250 u32 action, opaque;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400252 action = (u32) (uword) i->si_ptr;
253 action >>= 28;
254 opaque = (u32) (uword) i->si_ptr;
255 opaque &= ~(0xF0000000);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400257 clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258}
259
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400260static void
261test_reg (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400263 svmdb_client_t *c;
264 svmdb_notification_args_t args;
265 svmdb_notification_args_t *a = &args;
266 struct sigaction sa;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400267 svmdb_map_args_t *ma;
268
269 ma = map_arg_setup (chroot_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Dave Barachb7b92992018-10-17 10:38:51 -0400271 clib_memset (&sa, 0, sizeof (sa));
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400272 sa.sa_sigaction = sigaction_handler;
273 sa.sa_flags = SA_SIGINFO;
274 if (sigaction (SIGUSR2, &sa, 0) < 0)
275 {
276 clib_unix_warning ("sigaction");
277 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278 }
279
Dave Barachb7b92992018-10-17 10:38:51 -0400280 clib_memset (a, 0, sizeof (*a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
Dave Barach98cfc1a2016-07-18 14:23:36 -0400282 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400284 a->add_del = 1 /* add */ ;
285 a->nspace = SVMDB_NAMESPACE_STRING;
286 a->var = (char *) vbl;
287 a->elsize = 1;
288 a->signum = SIGUSR2;
289 a->action = SVMDB_ACTION_GET;
290 a->opaque = 0x0eadbeef;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400292 svmdb_local_add_del_notification (c, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400294 (void) svmdb_local_get_string_variable (c, (char *) vbl);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400296 a->add_del = 0; /* del */
297 svmdb_local_add_del_notification (c, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400299
300
301 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302}
303
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400304static void
305unset_vec (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400307 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400308 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309
Dave Barach98cfc1a2016-07-18 14:23:36 -0400310 ma = map_arg_setup (chroot_path);
311
312 c = svmdb_map (ma);
313
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400314 svmdb_local_unset_vec_variable (c, (char *) vbl);
315 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316}
317
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400318static void
319dump_vecs (char *chroot_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400321 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400322 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323
Dave Barach98cfc1a2016-07-18 14:23:36 -0400324 ma = map_arg_setup (chroot_path);
325
326 c = svmdb_map (ma);
327
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400328 svmdb_local_dump_vecs (c);
329 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330}
331
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400332static void
333crash_test (char *chroot_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400335 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400336 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337
Dave Barach98cfc1a2016-07-18 14:23:36 -0400338 ma = map_arg_setup (chroot_path);
339
340 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400342 clib_warning ("Grab region mutex and crash deliberately!");
343 c->db_rp->mutex_owner_pid = getpid ();
344 c->db_rp->mutex_owner_tag = -13;
345 pthread_mutex_lock (&c->db_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400347 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348}
349
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400350static void
351map_with_size (char *chroot_path, uword size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400353 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400354 svmdb_map_args_t *ma;
355
356 svmdbtool_main.size = size;
357 ma = map_arg_setup (chroot_path);
358
359 c = svmdb_map (ma);
360
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400361 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362}
363
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400364int
365main (int argc, char **argv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400367 unformat_input_t input;
368 int parsed = 0;
369 u8 *vbl = 0, *value = 0;
370 char *chroot_path = 0;
371 u8 *chroot_path_u8;
Dave Barach23a74122016-11-24 16:34:20 -0500372 u8 *filename;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400373 uword size;
374 f64 vr;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400375 int uid, gid, rv;
376 struct passwd _pw, *pw;
377 struct group _grp, *grp;
378 char *s, buf[128];
379
Guanghua Zhang60f16c22019-08-12 09:04:35 +0800380 clib_mem_init_thread_safe (0, 128 << 20);
381
Ed Warnicke853e7202016-08-12 11:42:26 -0700382 svmdbtool_main.uid = geteuid ();
383 svmdbtool_main.gid = getegid ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400385 unformat_init_command_line (&input, argv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400387 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
388 {
389 if (unformat (&input, "get-string %s", &vbl))
390 {
391 get_string (chroot_path, vbl);
392 vec_free (vbl);
393 parsed++;
394 }
395 else if (unformat (&input, "set-string %s %s", &vbl, &value))
396 {
397 set_string (chroot_path, vbl, value);
398 vec_free (vbl);
399 vec_free (value);
400 parsed++;
401 }
402 else if (unformat (&input, "unset-string %s", &vbl))
403 {
404 unset_string (chroot_path, vbl);
405 vec_free (vbl);
406 parsed++;
407 }
408 else if (unformat (&input, "dump-strings"))
409 {
410 dump_strings (chroot_path);
411 parsed++;
412 }
413 else if (unformat (&input, "unset-vec %s", &vbl))
414 {
415 unset_vec (chroot_path, vbl);
416 vec_free (vbl);
417 parsed++;
418 }
419 else if (unformat (&input, "dump-vecs"))
420 {
421 dump_vecs (chroot_path);
422 parsed++;
423 }
424 else if (unformat (&input, "test-vec %s", &vbl))
425 {
426 test_vec (chroot_path, vbl);
427 // vec_free(vbl);
428 parsed++;
429 }
430 else if (unformat (&input, "vlib-vec-rate %f", &vr))
431 {
432 test_vlib_vec_rate (chroot_path, vr);
433 parsed++;
434 }
435 else if (unformat (&input, "test-reg %s", &vbl))
436 {
437 test_reg (chroot_path, vbl);
438 parsed++;
439 }
440 else if (unformat (&input, "crash-test"))
441 {
442 crash_test (chroot_path);
443 }
444 else if (unformat (&input, "chroot %s", &chroot_path_u8))
445 {
446 chroot_path = (char *) chroot_path_u8;
447 }
448 else if (unformat (&input, "fake-install %s", &value))
449 {
450 fake_install (chroot_path, value);
451 parsed++;
452 }
453 else if (unformat (&input, "size %d", &size))
454 {
455 map_with_size (chroot_path, size);
456 parsed++;
457 }
Dave Barach98cfc1a2016-07-18 14:23:36 -0400458 else if (unformat (&input, "uid %d", &uid))
Ed Warnicke853e7202016-08-12 11:42:26 -0700459 svmdbtool_main.uid = uid;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400460 else if (unformat (&input, "gid %d", &gid))
Ed Warnicke853e7202016-08-12 11:42:26 -0700461 svmdbtool_main.gid = gid;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400462 else if (unformat (&input, "uid %s", &s))
Ed Warnicke853e7202016-08-12 11:42:26 -0700463 {
464 /* lookup the username */
465 pw = NULL;
466 rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
467 if (rv < 0)
468 {
469 fformat (stderr, "cannot fetch username %s", s);
470 exit (1);
471 }
472 if (pw == NULL)
473 {
474 fformat (stderr, "username %s does not exist", s);
475 exit (1);
476 }
477 vec_free (s);
478 svmdbtool_main.uid = pw->pw_uid;
479 }
Dave Barach98cfc1a2016-07-18 14:23:36 -0400480 else if (unformat (&input, "gid %s", &s))
Ed Warnicke853e7202016-08-12 11:42:26 -0700481 {
482 /* lookup the group name */
483 grp = NULL;
484 rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
485 if (rv != 0)
486 {
487 fformat (stderr, "cannot fetch group %s", s);
488 exit (1);
489 }
490 if (grp == NULL)
491 {
492 fformat (stderr, "group %s does not exist", s);
493 exit (1);
494 }
495 vec_free (s);
496 svmdbtool_main.gid = grp->gr_gid;
497 }
Dave Barach23a74122016-11-24 16:34:20 -0500498 else if (unformat (&input, "serialize-strings %s", &filename))
499 {
500 vec_add1 (filename, 0);
501 serialize_strings (chroot_path, (char *) filename);
502 parsed++;
503 }
504 else if (unformat (&input, "unserialize-strings %s", &filename))
505 {
506 vec_add1 (filename, 0);
507 unserialize_strings (chroot_path, (char *) filename);
508 parsed++;
509 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400510 else
511 {
512 break;
513 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 }
515
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400516 unformat_free (&input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400518 if (!parsed)
519 {
520 fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
521 argv[0]);
522 fformat (stdout, " unset-string <name> | dump-strings\n");
523 fformat (stdout, " test-vec <name> |\n");
524 fformat (stdout, " unset-vec <name> | dump-vecs\n");
Dave Barach98cfc1a2016-07-18 14:23:36 -0400525 fformat (stdout, " chroot <prefix> [uid <nnn-or-userid>]\n");
526 fformat (stdout, " [gid <nnn-or-group-name>]\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400528
529 exit (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530}
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400531
532/*
533 * fd.io coding-style-patch-verification: ON
534 *
535 * Local Variables:
536 * eval: (c-set-style "gnu")
537 * End:
538 */