blob: d50ca7be95c845b1b5e3153d11a3be2eb9f8f61d [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>
36#include <vppinfra/mheap.h>
37#include <vppinfra/heap.h>
38#include <vppinfra/pool.h>
39#include <vppinfra/format.h>
40#include <vppinfra/serialize.h>
41#include "svmdb.h"
42
Dave Barach98cfc1a2016-07-18 14:23:36 -040043typedef struct
44{
45 svmdb_map_args_t map_args;
46 int uid, gid;
47 uword size;
48} svmdbtool_main_t;
49
50svmdbtool_main_t svmdbtool_main;
51
Ed Warnicke853e7202016-08-12 11:42:26 -070052static inline svmdb_map_args_t *
53map_arg_setup (char *chroot_path)
Dave Barach98cfc1a2016-07-18 14:23:36 -040054{
55 svmdbtool_main_t *sm = &svmdbtool_main;
56 svmdb_map_args_t *ma = &sm->map_args;
57
Dave Barachb7b92992018-10-17 10:38:51 -040058 clib_memset (ma, 0, sizeof (*ma));
Dave Barach98cfc1a2016-07-18 14:23:36 -040059 ma->root_path = chroot_path;
60 ma->size = sm->size;
61 ma->uid = sm->uid;
62 ma->gid = sm->gid;
63 return ma;
64}
65
Dave Barach8a7fb0c2016-07-08 14:44:23 -040066static void
67get_string (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040069 svmdb_client_t *c;
70 char *rv;
Dave Barach98cfc1a2016-07-18 14:23:36 -040071 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
Dave Barach98cfc1a2016-07-18 14:23:36 -040073 ma = map_arg_setup (chroot_path);
74
75 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
Dave Barach8a7fb0c2016-07-08 14:44:23 -040077 rv = svmdb_local_get_string_variable (c, (char *) vbl);
78
79 fformat (stdout, "%s\n", rv ? rv : "UNSET");
80 vec_free (rv);
81 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082}
83
Dave Barach8a7fb0c2016-07-08 14:44:23 -040084static void
85set_string (char *chroot_path, u8 * vbl, u8 * value)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040087 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -040088 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
Dave Barach98cfc1a2016-07-18 14:23:36 -040090 ma = map_arg_setup (chroot_path);
91
92 c = svmdb_map (ma);
Dave Barach8a7fb0c2016-07-08 14:44:23 -040093 svmdb_local_set_string_variable (c, (char *) vbl, (char *) value);
94 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -070095}
96
Dave Barach8a7fb0c2016-07-08 14:44:23 -040097static void
98unset_string (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400100 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400101 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Dave Barach98cfc1a2016-07-18 14:23:36 -0400103 ma = map_arg_setup (chroot_path);
104
105 c = svmdb_map (ma);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400106 svmdb_local_unset_string_variable (c, (char *) vbl);
107 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108}
109
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400110static void
111dump_strings (char *chroot_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400113 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400114 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
Dave Barach98cfc1a2016-07-18 14:23:36 -0400116 ma = map_arg_setup (chroot_path);
117
118 c = svmdb_map (ma);
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400119 svmdb_local_dump_strings (c);
120 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121}
122
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400123static void
Dave Barach23a74122016-11-24 16:34:20 -0500124serialize_strings (char *chroot_path, char *filename)
125{
126 svmdb_client_t *c;
127 svmdb_map_args_t *ma;
128
129 ma = map_arg_setup (chroot_path);
130
131 c = svmdb_map (ma);
132 (void) svmdb_local_serialize_strings (c, filename);
133 svmdb_unmap (c);
134}
135
136static void
137unserialize_strings (char *chroot_path, char *filename)
138{
139 svmdb_client_t *c;
140 svmdb_map_args_t *ma;
141
142 ma = map_arg_setup (chroot_path);
143
144 c = svmdb_map (ma);
145 (void) svmdb_local_unserialize_strings (c, filename);
146 svmdb_unmap (c);
147}
148
149static void
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400150test_vlib_vec_rate (char *chroot_path, f64 vr)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400152 svmdb_client_t *c;
153 f64 *tv = 0;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400154 svmdb_map_args_t *ma;
155
156 ma = map_arg_setup (chroot_path);
157
158 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400160 vec_add1 (tv, vr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400162 svmdb_local_set_vec_variable (c, "vlib_vector_rate", (char *) tv,
163 sizeof (*tv));
164 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400166 vec_free (tv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167}
168
169
170
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400171static void
172test_vec (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400174 svmdb_client_t *c;
175 u64 *tv = 0;
176 int i;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400177 svmdb_map_args_t *ma;
178
179 ma = map_arg_setup (chroot_path);
180
181 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400183 /* my amp goes to 11 */
184 for (i = 0; i < 11; i++)
185 {
186 vec_add1 (tv, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 }
188
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400189 svmdb_local_set_vec_variable (c, (char *) vbl, (char *) tv, sizeof (tv[0]));
190 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400192 vec_free (tv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193}
194
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400195static void
196fake_install (char *chroot_path, u8 * add_value)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400198 svmdb_client_t *c;
199 u8 *v = 0;
200 u8 **values = 0;
201 u8 *oldvalue;
202 u8 *value;
203 int nitems = 0, i;
204 serialize_main_t m;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400205 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206
Dave Barach98cfc1a2016-07-18 14:23:36 -0400207 ma = map_arg_setup (chroot_path);
208
209 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400211 oldvalue = svmdb_local_get_vec_variable (c, "installed_sw", 1);
212 if (oldvalue)
213 {
214 unserialize_open_data (&m, oldvalue, vec_len (oldvalue));
215 nitems = unserialize_likely_small_unsigned_integer (&m);
216 for (i = 0; i < nitems; i++)
217 {
218 unserialize_cstring (&m, (char **) &value);
219 vec_add1 (values, value);
220 }
221 vec_free (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400223 nitems++;
224 value = format (0, "%s%c", add_value, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400226 vec_add1 (values, value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400228 fformat (stdout, "Resulting installed_sw vector:\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400230 serialize_open_vector (&m, v);
231 serialize_likely_small_unsigned_integer (&m, vec_len (values));
232 for (i = 0; i < vec_len (values); i++)
233 {
234 fformat (stdout, "%s\n", values[i]);
235 serialize_cstring (&m, (char *) values[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236 }
237
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400238 v = serialize_close_vector (&m);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400240 svmdb_local_set_vec_variable (c, "installed_sw", v, sizeof (v[0]));
241 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400243 for (i = 0; i < vec_len (values); i++)
244 vec_free (values[i]);
245 vec_free (values);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246}
247
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400248static void
249sigaction_handler (int signum, siginfo_t * i, void *notused)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400251 u32 action, opaque;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400253 action = (u32) (uword) i->si_ptr;
254 action >>= 28;
255 opaque = (u32) (uword) i->si_ptr;
256 opaque &= ~(0xF0000000);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400258 clib_warning ("signal %d, action %d, opaque %x", signum, action, opaque);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259}
260
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400261static void
262test_reg (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400264 svmdb_client_t *c;
265 svmdb_notification_args_t args;
266 svmdb_notification_args_t *a = &args;
267 struct sigaction sa;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400268 svmdb_map_args_t *ma;
269
270 ma = map_arg_setup (chroot_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
Dave Barachb7b92992018-10-17 10:38:51 -0400272 clib_memset (&sa, 0, sizeof (sa));
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400273 sa.sa_sigaction = sigaction_handler;
274 sa.sa_flags = SA_SIGINFO;
275 if (sigaction (SIGUSR2, &sa, 0) < 0)
276 {
277 clib_unix_warning ("sigaction");
278 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 }
280
Dave Barachb7b92992018-10-17 10:38:51 -0400281 clib_memset (a, 0, sizeof (*a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Dave Barach98cfc1a2016-07-18 14:23:36 -0400283 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400285 a->add_del = 1 /* add */ ;
286 a->nspace = SVMDB_NAMESPACE_STRING;
287 a->var = (char *) vbl;
288 a->elsize = 1;
289 a->signum = SIGUSR2;
290 a->action = SVMDB_ACTION_GET;
291 a->opaque = 0x0eadbeef;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400293 svmdb_local_add_del_notification (c, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400295 (void) svmdb_local_get_string_variable (c, (char *) vbl);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400297 a->add_del = 0; /* del */
298 svmdb_local_add_del_notification (c, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400300
301
302 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303}
304
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400305static void
306unset_vec (char *chroot_path, u8 * vbl)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400308 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400309 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310
Dave Barach98cfc1a2016-07-18 14:23:36 -0400311 ma = map_arg_setup (chroot_path);
312
313 c = svmdb_map (ma);
314
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400315 svmdb_local_unset_vec_variable (c, (char *) vbl);
316 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317}
318
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400319static void
320dump_vecs (char *chroot_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400322 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400323 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Dave Barach98cfc1a2016-07-18 14:23:36 -0400325 ma = map_arg_setup (chroot_path);
326
327 c = svmdb_map (ma);
328
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400329 svmdb_local_dump_vecs (c);
330 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331}
332
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400333static void
334crash_test (char *chroot_path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400336 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400337 svmdb_map_args_t *ma;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
Dave Barach98cfc1a2016-07-18 14:23:36 -0400339 ma = map_arg_setup (chroot_path);
340
341 c = svmdb_map (ma);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400343 clib_warning ("Grab region mutex and crash deliberately!");
344 c->db_rp->mutex_owner_pid = getpid ();
345 c->db_rp->mutex_owner_tag = -13;
346 pthread_mutex_lock (&c->db_rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400348 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349}
350
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400351static void
352map_with_size (char *chroot_path, uword size)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400354 svmdb_client_t *c;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400355 svmdb_map_args_t *ma;
356
357 svmdbtool_main.size = size;
358 ma = map_arg_setup (chroot_path);
359
360 c = svmdb_map (ma);
361
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400362 svmdb_unmap (c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363}
364
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400365int
366main (int argc, char **argv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400368 unformat_input_t input;
369 int parsed = 0;
370 u8 *vbl = 0, *value = 0;
371 char *chroot_path = 0;
372 u8 *chroot_path_u8;
Dave Barach23a74122016-11-24 16:34:20 -0500373 u8 *filename;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400374 uword size;
375 f64 vr;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400376 int uid, gid, rv;
377 struct passwd _pw, *pw;
378 struct group _grp, *grp;
379 char *s, buf[128];
380
Ed Warnicke853e7202016-08-12 11:42:26 -0700381 svmdbtool_main.uid = geteuid ();
382 svmdbtool_main.gid = getegid ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400384 unformat_init_command_line (&input, argv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400386 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
387 {
388 if (unformat (&input, "get-string %s", &vbl))
389 {
390 get_string (chroot_path, vbl);
391 vec_free (vbl);
392 parsed++;
393 }
394 else if (unformat (&input, "set-string %s %s", &vbl, &value))
395 {
396 set_string (chroot_path, vbl, value);
397 vec_free (vbl);
398 vec_free (value);
399 parsed++;
400 }
401 else if (unformat (&input, "unset-string %s", &vbl))
402 {
403 unset_string (chroot_path, vbl);
404 vec_free (vbl);
405 parsed++;
406 }
407 else if (unformat (&input, "dump-strings"))
408 {
409 dump_strings (chroot_path);
410 parsed++;
411 }
412 else if (unformat (&input, "unset-vec %s", &vbl))
413 {
414 unset_vec (chroot_path, vbl);
415 vec_free (vbl);
416 parsed++;
417 }
418 else if (unformat (&input, "dump-vecs"))
419 {
420 dump_vecs (chroot_path);
421 parsed++;
422 }
423 else if (unformat (&input, "test-vec %s", &vbl))
424 {
425 test_vec (chroot_path, vbl);
426 // vec_free(vbl);
427 parsed++;
428 }
429 else if (unformat (&input, "vlib-vec-rate %f", &vr))
430 {
431 test_vlib_vec_rate (chroot_path, vr);
432 parsed++;
433 }
434 else if (unformat (&input, "test-reg %s", &vbl))
435 {
436 test_reg (chroot_path, vbl);
437 parsed++;
438 }
439 else if (unformat (&input, "crash-test"))
440 {
441 crash_test (chroot_path);
442 }
443 else if (unformat (&input, "chroot %s", &chroot_path_u8))
444 {
445 chroot_path = (char *) chroot_path_u8;
446 }
447 else if (unformat (&input, "fake-install %s", &value))
448 {
449 fake_install (chroot_path, value);
450 parsed++;
451 }
452 else if (unformat (&input, "size %d", &size))
453 {
454 map_with_size (chroot_path, size);
455 parsed++;
456 }
Dave Barach98cfc1a2016-07-18 14:23:36 -0400457 else if (unformat (&input, "uid %d", &uid))
Ed Warnicke853e7202016-08-12 11:42:26 -0700458 svmdbtool_main.uid = uid;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400459 else if (unformat (&input, "gid %d", &gid))
Ed Warnicke853e7202016-08-12 11:42:26 -0700460 svmdbtool_main.gid = gid;
Dave Barach98cfc1a2016-07-18 14:23:36 -0400461 else if (unformat (&input, "uid %s", &s))
Ed Warnicke853e7202016-08-12 11:42:26 -0700462 {
463 /* lookup the username */
464 pw = NULL;
465 rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
466 if (rv < 0)
467 {
468 fformat (stderr, "cannot fetch username %s", s);
469 exit (1);
470 }
471 if (pw == NULL)
472 {
473 fformat (stderr, "username %s does not exist", s);
474 exit (1);
475 }
476 vec_free (s);
477 svmdbtool_main.uid = pw->pw_uid;
478 }
Dave Barach98cfc1a2016-07-18 14:23:36 -0400479 else if (unformat (&input, "gid %s", &s))
Ed Warnicke853e7202016-08-12 11:42:26 -0700480 {
481 /* lookup the group name */
482 grp = NULL;
483 rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
484 if (rv != 0)
485 {
486 fformat (stderr, "cannot fetch group %s", s);
487 exit (1);
488 }
489 if (grp == NULL)
490 {
491 fformat (stderr, "group %s does not exist", s);
492 exit (1);
493 }
494 vec_free (s);
495 svmdbtool_main.gid = grp->gr_gid;
496 }
Dave Barach23a74122016-11-24 16:34:20 -0500497 else if (unformat (&input, "serialize-strings %s", &filename))
498 {
499 vec_add1 (filename, 0);
500 serialize_strings (chroot_path, (char *) filename);
501 parsed++;
502 }
503 else if (unformat (&input, "unserialize-strings %s", &filename))
504 {
505 vec_add1 (filename, 0);
506 unserialize_strings (chroot_path, (char *) filename);
507 parsed++;
508 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400509 else
510 {
511 break;
512 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513 }
514
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400515 unformat_free (&input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400517 if (!parsed)
518 {
519 fformat (stdout, "%s: get-string <name> | set-string <name> <value>\n",
520 argv[0]);
521 fformat (stdout, " unset-string <name> | dump-strings\n");
522 fformat (stdout, " test-vec <name> |\n");
523 fformat (stdout, " unset-vec <name> | dump-vecs\n");
Dave Barach98cfc1a2016-07-18 14:23:36 -0400524 fformat (stdout, " chroot <prefix> [uid <nnn-or-userid>]\n");
525 fformat (stdout, " [gid <nnn-or-group-name>]\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400527
528 exit (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529}
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400530
531/*
532 * fd.io coding-style-patch-verification: ON
533 *
534 * Local Variables:
535 * eval: (c-set-style "gnu")
536 * End:
537 */