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