Fix unlinking of /dev/shm files.
[vpp.git] / src / svm / ssvm.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 #include "ssvm.h"
16
17 int
18 ssvm_master_init (ssvm_private_t * ssvm, u32 master_index)
19 {
20   int ssvm_fd;
21   u8 *ssvm_filename;
22   u8 junk = 0;
23   int flags;
24   ssvm_shared_header_t *sh;
25   u64 ticks = clib_cpu_time_now ();
26   u64 randomize_baseva;
27   void *oldheap;
28
29   if (ssvm->ssvm_size == 0)
30     return SSVM_API_ERROR_NO_SIZE;
31
32   if (CLIB_DEBUG > 1)
33     clib_warning ("[%d] creating segment '%s'", getpid (), ssvm->name);
34
35   ssvm_filename = format (0, "/dev/shm/%s%c", ssvm->name, 0);
36
37   unlink ((char *) ssvm_filename);
38
39   vec_free (ssvm_filename);
40
41   ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR | O_CREAT | O_EXCL, 0777);
42
43   if (ssvm_fd < 0)
44     {
45       clib_unix_warning ("create segment '%s'", ssvm->name);
46       return SSVM_API_ERROR_CREATE_FAILURE;
47     }
48
49   if (lseek (ssvm_fd, ssvm->ssvm_size, SEEK_SET) < 0)
50     {
51       clib_unix_warning ("lseek");
52       close (ssvm_fd);
53       return SSVM_API_ERROR_SET_SIZE;
54     }
55
56   if (write (ssvm_fd, &junk, 1) != 1)
57     {
58       clib_unix_warning ("set ssvm size");
59       close (ssvm_fd);
60       return SSVM_API_ERROR_SET_SIZE;
61     }
62
63   flags = MAP_SHARED;
64   if (ssvm->requested_va)
65     flags |= MAP_FIXED;
66
67   randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
68
69   if (ssvm->requested_va)
70     ssvm->requested_va += randomize_baseva;
71
72   sh = ssvm->sh =
73     (ssvm_shared_header_t *) mmap ((void *) ssvm->requested_va,
74                                    ssvm->ssvm_size, PROT_READ | PROT_WRITE,
75                                    flags, ssvm_fd, 0);
76
77   if (ssvm->sh == MAP_FAILED)
78     {
79       clib_unix_warning ("mmap");
80       close (ssvm_fd);
81       return SSVM_API_ERROR_MMAP;
82     }
83
84   close (ssvm_fd);
85
86   ssvm->my_pid = getpid ();
87   sh->master_pid = ssvm->my_pid;
88   sh->ssvm_size = ssvm->ssvm_size;
89   sh->heap = mheap_alloc_with_flags
90     (((u8 *) sh) + MMAP_PAGESIZE, ssvm->ssvm_size - MMAP_PAGESIZE,
91      MHEAP_FLAG_DISABLE_VM | MHEAP_FLAG_THREAD_SAFE);
92
93   sh->ssvm_va = pointer_to_uword (sh);
94   sh->master_index = master_index;
95
96   oldheap = ssvm_push_heap (sh);
97   sh->name = format (0, "%s%c", ssvm->name, 0);
98   ssvm_pop_heap (oldheap);
99
100   ssvm->i_am_master = 1;
101
102   /* The application has to set set sh->ready... */
103   return 0;
104 }
105
106 int
107 ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds)
108 {
109   struct stat stat;
110   int ssvm_fd = -1;
111   ssvm_shared_header_t *sh;
112
113   ssvm->i_am_master = 0;
114
115   while (timeout_in_seconds-- > 0)
116     {
117       if (ssvm_fd < 0)
118         ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777);
119       if (ssvm_fd < 0)
120         {
121           sleep (1);
122           continue;
123         }
124       if (fstat (ssvm_fd, &stat) < 0)
125         {
126           sleep (1);
127           continue;
128         }
129
130       if (stat.st_size > 0)
131         goto map_it;
132     }
133   clib_warning ("slave timeout");
134   return SSVM_API_ERROR_SLAVE_TIMEOUT;
135
136 map_it:
137   sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
138                       ssvm_fd, 0);
139   if (sh == MAP_FAILED)
140     {
141       clib_unix_warning ("slave research mmap");
142       close (ssvm_fd);
143       return SSVM_API_ERROR_MMAP;
144     }
145
146   while (timeout_in_seconds-- > 0)
147     {
148       if (sh->ready)
149         goto re_map_it;
150     }
151   close (ssvm_fd);
152   munmap (sh, MMAP_PAGESIZE);
153   clib_warning ("slave timeout 2");
154   return SSVM_API_ERROR_SLAVE_TIMEOUT;
155
156 re_map_it:
157   ssvm->requested_va = (u64) sh->ssvm_va;
158   ssvm->ssvm_size = sh->ssvm_size;
159   munmap (sh, MMAP_PAGESIZE);
160
161   sh = ssvm->sh = (void *) mmap ((void *) ssvm->requested_va, ssvm->ssvm_size,
162                                  PROT_READ | PROT_WRITE,
163                                  MAP_SHARED | MAP_FIXED, ssvm_fd, 0);
164
165   if (sh == MAP_FAILED)
166     {
167       clib_unix_warning ("slave final mmap");
168       close (ssvm_fd);
169       return SSVM_API_ERROR_MMAP;
170     }
171   sh->slave_pid = getpid ();
172   return 0;
173 }
174
175 void
176 ssvm_delete (ssvm_private_t * ssvm)
177 {
178   u8 *fn;
179
180   fn = format (0, "/dev/shm/%s%c", ssvm->name, 0);
181
182   if (CLIB_DEBUG > 1)
183     clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (),
184                   ssvm->name, fn);
185
186   /* Throw away the backing file */
187   if (unlink ((char *) fn) < 0)
188     clib_unix_warning ("unlink segment '%s'", ssvm->name);
189
190   vec_free (fn);
191   vec_free (ssvm->name);
192
193   munmap ((void *) ssvm->requested_va, ssvm->ssvm_size);
194 }
195
196
197 /*
198  * fd.io coding-style-patch-verification: ON
199  *
200  * Local Variables:
201  * eval: (c-set-style "gnu")
202  * End:
203  */