c11 safe string handling support
[vpp.git] / src / 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 <svm/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
33 restart_main_fn (unformat_input_t * i)
34 {
35   int verbose = 0;
36   int old_pid;
37   int wait;
38   u8 *chroot_path = 0;
39   svmdb_client_t *svmdb_client;
40   volatile pid_t *pidp;
41   struct stat statb;
42   ino_t old_inode;
43   int sleeps;
44   svmdb_map_args_t _ma, *ma = &_ma;
45
46   struct timespec _req, *req = &_req;
47   struct timespec _rem, *rem = &_rem;
48
49   if (geteuid ())
50     clib_error ("vpp_restart: must be root...");
51
52   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
53     {
54       if (unformat (i, "verbose") || unformat (i, "v"))
55         verbose = 1;
56       else if (unformat (i, "chroot %s", &chroot_path))
57         ;
58       else
59         {
60           clib_error ("unknown input `%U'", format_unformat_error, i);
61           return 1;
62         }
63     }
64
65   /*
66    * Step 1: look up the current VPP pid in the shared-memory database
67    */
68   clib_memset (ma, 0, sizeof (*ma));
69   ma->root_path = (char *) chroot_path;
70
71   svmdb_client = svmdb_map (ma);
72
73   pidp = svmdb_local_get_variable_reference (svmdb_client,
74                                              SVMDB_NAMESPACE_VEC, "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, "vpp_pid");
184   if (pidp == 0)
185     {
186       clib_error ("post_restart: 'vpp_pid' svm variable not found,"
187                   "vpp did not restart?");
188       return 2;
189     }
190
191   sleeps = 0;
192
193   /*
194    * Step 7: wait for vpp to publish its new PID
195    */
196
197   /* Spin for up to 15 seconds */
198   for (wait = 0; wait < 150; wait++)
199     {
200       if (*pidp && (*pidp != old_pid))
201         goto restarted;
202       req->tv_sec = 0;
203       req->tv_nsec = 100000 * 1000;     /* 100 ms */
204       while (nanosleep (req, rem) < 0)
205         *req = *rem;
206       sleeps++;
207     }
208
209   clib_error ("Timeout waiting for vpp to publish pid after restart...");
210   return 4;
211
212 restarted:
213
214   /* Done... */
215
216   if (verbose && sleeps)
217     fformat (stdout, "pid sleeps %d\n", sleeps);
218
219   if (verbose)
220     fformat (stdout, "New PID %d... Restarted...\n", *pidp);
221
222   svmdb_unmap (svmdb_client);
223   return 0;
224 }
225
226 int
227 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 }
239
240 /*
241  * fd.io coding-style-patch-verification: ON
242  *
243  * Local Variables:
244  * eval: (c-set-style "gnu")
245  * End:
246  */