Add uid/gid config parameters to shared-vm database map operator
[vpp.git] / vpp-api-test / vat / restart.c
1 /*
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 <time.h>
18 #include <sys/types.h>
19 #include <signal.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22
23 #include <vppinfra/clib.h>
24 #include <vppinfra/vec.h>
25 #include <vppinfra/hash.h>
26 #include <svmdb.h>
27 #include <vppinfra/format.h>
28 #include <vppinfra/error.h>
29 #include <vppinfra/time.h>
30 #include <vppinfra/macros.h>
31
32 int restart_main_fn (unformat_input_t * i)
33 {
34   int verbose = 0;
35   int old_pid;
36   int wait;
37   u8 * chroot_path = 0;
38   svmdb_client_t * svmdb_client;
39   volatile pid_t *pidp;
40   struct stat statb;
41   ino_t old_inode;
42   int sleeps;
43   svmdb_map_args_t _ma, *ma= &_ma;
44
45   struct timespec _req, *req = &_req;
46   struct timespec _rem, *rem = &_rem;
47
48   if (geteuid())
49     clib_error ("vpp_restart: must be root...");
50
51   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
52     {
53       if (unformat (i, "verbose") || unformat (i, "v"))
54         verbose = 1;
55       else if (unformat (i, "chroot %s", &chroot_path))
56         ;
57       else
58         {
59           clib_error ("unknown input `%U'", format_unformat_error, i);
60           return 1;
61         }
62     }
63
64   /* 
65    * Step 1: look up the current VPP pid in the shared-memory database 
66    */
67   memset (ma, 0, sizeof (*ma));
68   ma->root_path = (char *)chroot_path;
69
70   svmdb_client = svmdb_map (ma);
71
72   pidp = svmdb_local_get_variable_reference (svmdb_client,
73                                              SVMDB_NAMESPACE_VEC, 
74                                              "vpp_pid");
75   if (pidp == 0)
76     {
77       clib_error ("'vpp_pid' svm variable not found, vpp has never run?");
78       return 2;
79     }
80
81   /* Spin for up to 10 seconds for vpp to start */
82   for (wait = 0; wait < 1000; wait++)
83     {
84       req->tv_sec = 0;
85       req->tv_nsec = 10000*1000;  /* 10 ms */
86       while (nanosleep(req, rem) < 0)
87         *req = *rem;
88       
89       if (*pidp) 
90         goto found2;
91     }
92
93   clib_error ("VPP not runnning...");
94   return 3;
95   
96  found2:
97
98   old_pid = *pidp;
99   
100   /*
101    * Step 2: sanity check the pid we discovered
102    */
103   if (verbose)
104     fformat(stdout, "Sanity check current vpp pid %d\n", old_pid);
105
106   if (kill (old_pid, 0) < 0)
107     {
108       svmdb_unmap (svmdb_client);
109       clib_error ("vpp current pid %d not running...", old_pid);
110       return 2;
111     }
112   
113   if (verbose)
114     fformat(stdout, "Sanity check vpp pid %d OK\n", old_pid);
115
116   /* 
117    * Step 3: figure out the current vpp <--> client shared-VM file 
118    * inode number 
119    */
120   if (stat("/dev/shm/vpe-api", &statb) < 0)
121     {
122       clib_unix_error ("stat fail");
123       return 4;
124     }
125
126   old_inode = statb.st_ino;
127
128   if (verbose)
129     fformat(stdout, "Old inode %u\n", old_inode);
130
131   /* Note: restart wipes out the shared VM database*/
132   svmdb_unmap (svmdb_client);
133
134   /*
135    * Step 4: send SIGTERM to vpp.
136    * systemd et al. will restart vpp after wiping out the shared-VM
137    * database and (crucially) the shared API messaging segment
138    */
139
140   if (kill (old_pid, SIGTERM) < 0)
141     {
142       clib_unix_error ("SIGTERM fail");
143       return 3;
144     }
145
146   sleeps = 0;
147
148   /* 
149    * Step 5: wait up to 15 seconds for a new incarnation of 
150    * the shared-VM API segment to appear.
151    */
152   for (wait = 0; wait < 150; wait++)
153     {
154       if ((stat("/dev/shm/vpe-api", &statb) < 0)
155           || statb.st_ino == old_inode)
156         {
157           req->tv_sec = 0;
158           req->tv_nsec = 100000*1000;  /* 100 ms */
159           while (nanosleep(req, rem) < 0)
160             *req = *rem;
161           sleeps++;
162         }
163       else
164         goto new_inode;
165     }
166
167   clib_error ("Timeout waiting for new inode to appear...");
168   return 5;
169
170  new_inode:
171   if (verbose && sleeps > 0)
172     fformat(stdout, "Inode sleeps %d\n", sleeps);
173
174   if (verbose)
175     fformat(stdout, "New inode %u\n", statb.st_ino);
176
177   /* 
178    * Step 6: remap the SVM database 
179    */
180   svmdb_client = svmdb_map (ma);
181   
182   pidp = svmdb_local_get_variable_reference (svmdb_client,
183                                              SVMDB_NAMESPACE_VEC, 
184                                              "vpp_pid");
185   if (pidp == 0)
186     {
187       clib_error ("post_restart: 'vpp_pid' svm variable not found,"
188                   "vpp did not restart?");
189       return 2;
190     }
191
192   sleeps = 0;
193
194   /* 
195    * Step 7: wait for vpp to publish its new PID
196    */
197
198   /* Spin for up to 15 seconds */
199   for (wait = 0; wait < 150; wait++)
200     {
201       if (*pidp && (*pidp != old_pid))
202         goto restarted;
203       req->tv_sec = 0;
204       req->tv_nsec = 100000*1000;  /* 100 ms */
205       while (nanosleep(req, rem) < 0)
206         *req = *rem;
207       sleeps++;
208     }
209   
210   clib_error ("Timeout waiting for vpp to publish pid after restart...");
211   return 4;
212
213  restarted:  
214
215   /* Done... */
216   
217   if (verbose && sleeps)
218     fformat(stdout, "pid sleeps %d\n", sleeps);
219
220   if (verbose)
221     fformat (stdout, "New PID %d... Restarted...\n", *pidp);
222   
223   svmdb_unmap (svmdb_client);
224   return 0;
225 }
226
227 int main (int argc, char **argv)
228 {
229   unformat_input_t i;
230   int ret;
231
232   clib_mem_init (0, 64ULL<<20);
233
234   unformat_init_command_line (&i, argv);
235   ret = restart_main_fn (&i);
236   unformat_free (&i);
237   return ret;
238 }