vat2: add plugin-path parameter
[vpp.git] / src / vat2 / plugin.c
1 /*
2  * Copyright (c) 2020 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 <dlfcn.h>
17 #include <dirent.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <vlib/vlib.h>
22 #include "vat2.h"
23
24 typedef struct
25 {
26   u8 *name;
27   u8 *filename;
28   struct stat file_info;
29   void *handle;
30 } plugin_info_t;
31
32 /* loaded plugin info */
33 plugin_info_t *plugin_info;
34
35 static int
36 load_one_plugin (plugin_info_t * pi)
37 {
38   void *handle, *register_handle;
39   clib_error_t *(*fp) (void);
40   clib_error_t *error;
41
42   handle = dlopen ((char *) pi->name, RTLD_LAZY);
43
44   /*
45    * Note: this can happen if the plugin has an undefined symbol reference,
46    * so print a warning. Otherwise, the poor slob won't know what happened.
47    * Ask me how I know that...
48    */
49   if (handle == 0)
50     {
51       clib_warning ("%s", dlerror ());
52       return -1;
53     }
54
55   pi->handle = handle;
56
57   register_handle = dlsym (pi->handle, "vat2_register_plugin");
58   if (register_handle == 0)
59     {
60       clib_warning ("%s: symbol vat2_register_plugin not found", pi->name);
61       dlclose (handle);
62       return -1;
63     }
64
65   fp = register_handle;
66
67   error = (*fp) ();
68
69   if (error)
70     {
71       clib_error_report (error);
72       dlclose (handle);
73       return -1;
74     }
75
76   return 0;
77 }
78
79 /* Takes a vector as argument */
80 static u8 **
81 split_plugin_path (u8 *plugin_path)
82 {
83   int i;
84   u8 **rv = 0;
85   u8 *path = (u8 *) plugin_path;
86   u8 *this = 0;
87
88   for (i = 0; i < vec_len (plugin_path); i++)
89     {
90       if (path[i] != ':')
91         {
92           vec_add1 (this, path[i]);
93           continue;
94         }
95       vec_add1 (this, 0);
96       vec_add1 (rv, this);
97       this = 0;
98     }
99   if (this)
100     {
101       vec_add1 (this, 0);
102       vec_add1 (rv, this);
103     }
104   return rv;
105 }
106
107 int
108 vat2_load_plugins (u8 *path, char *filter, int *loaded)
109 {
110   DIR *dp;
111   struct dirent *entry;
112   struct stat statb;
113   uword *p;
114   plugin_info_t *pi;
115   u8 **plugin_path;
116   int i;
117   int res = 0;
118   uword *plugin_by_name_hash = hash_create_string (0, sizeof (uword));
119
120   *loaded = 0;
121   plugin_path = split_plugin_path (path);
122
123   for (i = 0; i < vec_len (plugin_path); i++)
124     {
125       DBG ("Opening path: %s\n", plugin_path[i]);
126       dp = opendir ((char *) plugin_path[i]);
127
128       if (dp == 0)
129         continue;
130
131       while ((entry = readdir (dp)))
132         {
133           u8 *plugin_name;
134
135           if (filter)
136             {
137               int j;
138               for (j = 0; j < vec_len (filter); j++)
139                 if (entry->d_name[j] != filter[j])
140                   goto next;
141             }
142
143           plugin_name = format (0, "%s/%s%c", plugin_path[i],
144                                 entry->d_name, 0);
145
146           /* unreadable */
147           if (stat ((char *) plugin_name, &statb) < 0)
148             {
149             ignore:
150               vec_free (plugin_name);
151               continue;
152             }
153
154           /* a dir or other things which aren't plugins */
155           if (!S_ISREG (statb.st_mode))
156             goto ignore;
157
158           p = hash_get_mem (plugin_by_name_hash, plugin_name);
159           if (p == 0)
160             {
161               vec_add2 (plugin_info, pi, 1);
162               pi->name = plugin_name;
163               pi->file_info = statb;
164
165               if (load_one_plugin (pi))
166                 {
167                   res = -1;
168                   vec_free (plugin_name);
169                   vec_set_len (plugin_info, vec_len (plugin_info) - 1);
170                   continue;
171                 }
172               clib_memset (pi, 0, sizeof (*pi));
173               hash_set_mem (plugin_by_name_hash, plugin_name,
174                             pi - plugin_info);
175               *loaded = *loaded + 1;
176             }
177         next:
178           ;
179         }
180       closedir (dp);
181       vec_free (plugin_path[i]);
182     }
183   vec_free (plugin_path);
184   return res;
185 }
186
187 #define QUOTE_(x) #x
188 #define QUOTE(x) QUOTE_(x)
189
190 /*
191  * fd.io coding-style-patch-verification: ON
192  *
193  * Local Variables:
194  * eval: (c-set-style "gnu")
195  * End:
196  */