• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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 #ifndef _NAMESPACE_H
17 #define _NAMESPACE_H
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #include <stdbool.h>
24 #include "strops.h"
25 
26 #define NS_DEFAULT_NAME "default"
27 struct dso;
28 /* define dso list */
29 typedef struct _dso_list_ {
30     size_t num;
31     size_t size;
32     struct dso **dsos;
33 } dsolist;
34 /* define namespace struct */
35 struct _ns_inherit_list_;
36 typedef struct _namespace_t_ {
37     char *ns_name;            /* namespace name */
38     char *env_paths;          /* value of LD_LIBRARY_PATH. splited by ':'. */
39     char *lib_paths;          /* library search paths splited by ':'. */
40 
41     char *asan_lib_paths;          /* when asan is enable, library search paths splited by ':'. */
42     strlist *permitted_paths;    /* when separated, permitted search paths splited by ':', including sub dirs. */
43     strlist *asan_permitted_paths;    /* when asan is enable and separated,the same as above.  */
44 
45     bool separated;           /* if separated */
46     strlist *allowed_libs;       /* when separated, allowed library names splited by ':'. */
47     dsolist *ns_dsos;         /* dso list in this namespace */
48     struct _ns_inherit_list_ *ns_inherits;   /* inherit list in this namespace */
49 } ns_t;
50 /* define namespace list */
51 typedef struct _namespaces_list_ {
52     size_t num;
53     size_t size;
54     ns_t **nss;
55 } nslist;
56 /* define namespace inherit */
57 typedef struct _namespace_inherit_ {
58     ns_t *inherited_ns;       /* inherited namespace */
59     strlist *shared_libs;        /* when inherited, shared library names splited by ':'. */
60 } ns_inherit;
61 /* define namespace inherit list */
62 typedef struct _ns_inherit_list_ {
63     size_t num;
64     size_t size;
65     ns_inherit **inherits;
66 } ns_inherit_list;
67 
68 /* init g_ns_list */
69 nslist *nslist_init();
70 
71 /* namespace funcs */
72 ns_t *ns_alloc();
73 void ns_free(ns_t *ns);
74 void ns_set_name(ns_t *ns, const char *name);
75 void ns_set_env_paths(ns_t *ns, const char *env_paths);
76 void ns_set_lib_paths(ns_t *ns, const char *lib_paths);
77 void ns_set_asan_lib_paths(ns_t *ns, const char *asan_lib_paths);
78 void ns_set_permitted_paths(ns_t *ns, const char *permitted_paths);
79 void ns_set_asan_permitted_paths(ns_t *ns, const char *asan_permitted_paths);
80 void ns_set_separated(ns_t *ns, bool separated);
81 void ns_set_allowed_libs(ns_t *ns, const char *allowed_libs);
82 void ns_add_dso(ns_t *ns, struct dso *dso);
83 void nslist_add_ns(ns_t *ns);
84 void ns_add_inherit(ns_t *ns,ns_t *inherited, const char *shared_libs);
85 
86 /* get default namespace */
87 ns_t *get_default_ns();
88 
89 /* check if library pathname is accessible in the namespace */
90 bool is_accessible(ns_t *ns, const char *lib_pathname, bool is_asan, bool check_inherited);
91 
92 /* check if asan_lib_paths or asan_permitted_paths pathname is accessible in the namespace */
93 bool check_asan_path(ns_t *ns, const char *lib_pathname);
94 
95 /* check if library is sharable in the inherited namespace */
96 bool is_sharable(ns_inherit *inherit, const char *lib_name);
97 
98 /* find namespace by name */
99 ns_t *find_ns_by_name(const char *ns_name);
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif