Add UID-based APIs:
[platform/core/system/tizen-platform-config.git] / src / global-api.c
1 /*
2  * Copyright (C) 2013-2014 Intel Corporation.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors:
19  *       José Bollo <jose.bollo@open.eurogiciel.org>
20  *       Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21  *       Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
22  *
23  */
24 #define _GNU_SOURCE
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <unistd.h>
31 #include <assert.h>
32 #include <pthread.h>
33 #include "tzplatform_variables.h"
34 #include "tzplatform_config.h"
35
36 #include "shared-api.h"
37 #include "isadmin.h"
38 #include "signup.inc"
39
40 #define TZ_UID_START    5000
41 #define TZ_UID_MAX      128
42 #define uid2idx(x)      ((x > TZ_UID_START) ? (x - TZ_UID_START) : x)
43 static struct tzplatform_context *_context[TZ_UID_MAX] = {0};
44
45 int init_internal_context(uid_t uid)
46 {
47         int ret;
48         int idx;
49         static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
50
51         if (uid <= TZ_UID_START)
52                 assert(uid == 0);
53
54         idx = uid2idx(uid);
55         assert(idx < TZ_UID_MAX);
56
57         if (_context[idx]) return 0;
58
59         pthread_mutex_lock(&mutex);
60
61         ret = tzplatform_context_create(&_context[idx]);
62         if (ret < 0) {
63                 pthread_mutex_unlock(&mutex);
64                 return ret;
65         }
66
67         ret = tzplatform_context_set_user(_context[idx], uid);
68         if (ret < 0) {
69                 tzplatform_context_destroy(_context[idx]);
70                 _context[idx] = NULL;
71
72                 pthread_mutex_unlock(&mutex);
73                 return ret;
74         }
75
76         pthread_mutex_unlock(&mutex);
77
78         return 0;
79 }
80
81 int tzplatform_getcount()
82 {
83         return _TZPLATFORM_VARIABLES_COUNT_;
84 }
85
86 const char* tzplatform_getname(enum tzplatform_variable id)
87 {
88         return _getname_tzplatform_(id, tizen_platform_config_signup);
89 }
90
91 enum tzplatform_variable tzplatform_getid(const char *name)
92 {
93         return _getid_tzplatform_(name, tizen_platform_config_signup);
94 }
95
96 const char* tzplatform_getenv(enum tzplatform_variable id)
97 {
98         return _getenv_tzplatform_(id, tizen_platform_config_signup);
99 }
100
101 const char* tzplatform_uid_getenv(uid_t uid, enum tzplatform_variable id)
102 {
103         int ret = init_internal_context(uid);
104         if (ret < 0)
105                 return NULL;
106
107         return _context_getenv_tzplatform_(id, tizen_platform_config_signup, _context[uid2idx(uid)]);
108 }
109
110 const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id)
111 {
112         return _context_getenv_tzplatform_(id, tizen_platform_config_signup, context);
113 }
114
115 int tzplatform_getenv_int(enum tzplatform_variable id)
116 {
117         return _getenv_int_tzplatform_(id, tizen_platform_config_signup);
118 }
119
120 int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id)
121 {
122         return _context_getenv_int_tzplatform_(id, tizen_platform_config_signup, context);
123 }
124
125 const char* tzplatform_mkstr(enum tzplatform_variable id, const char *str)
126 {
127         return _mkstr_tzplatform_(id, str, tizen_platform_config_signup);
128 }
129
130 const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str)
131 {
132         return _context_mkstr_tzplatform_(id, str, tizen_platform_config_signup, context);
133 }
134
135 const char* tzplatform_mkpath(enum tzplatform_variable id, const char *path)
136 {
137         return _mkpath_tzplatform_(id, path, tizen_platform_config_signup);
138 }
139
140 const char* tzplatform_uid_mkpath(uid_t uid, enum tzplatform_variable id, const char *path)
141 {
142         int ret = init_internal_context(uid);
143         if (ret < 0)
144                 return NULL;
145
146         return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, _context[uid2idx(uid)]);
147 }
148
149 const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path)
150 {
151         return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, context);
152 }
153
154 const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path, const char* path2)
155 {
156         return _mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup);
157 }
158
159 const char* tzplatform_uid_mkpath3(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2)
160 {
161         int ret = init_internal_context(uid);
162         if (ret < 0)
163                 return NULL;
164
165         return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, _context[uid2idx(uid)]);
166 }
167 const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2)
168 {
169         return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, context);
170 }
171
172 const char* tzplatform_mkpath4(enum tzplatform_variable id, const char * path, const char* path2, const char *path3)
173 {
174         return _mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup);
175 }
176
177 const char* tzplatform_uid_mkpath4(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
178 {
179         int ret = init_internal_context(uid);
180         if (ret < 0)
181                 return NULL;
182
183         return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, _context[uid2idx(uid)]);
184 }
185
186 const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
187 {
188         return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, context);
189 }
190
191 uid_t tzplatform_getuid(enum tzplatform_variable id)
192 {
193         return _getuid_tzplatform_(id, tizen_platform_config_signup);
194 }
195
196 uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id)
197 {
198         return _context_getuid_tzplatform_(id, tizen_platform_config_signup, context);
199 }
200
201 gid_t tzplatform_getgid(enum tzplatform_variable id)
202 {
203         return _getgid_tzplatform_(id, tizen_platform_config_signup);
204 }
205
206 gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id)
207 {
208         return _context_getgid_tzplatform_(id, tizen_platform_config_signup, context);
209 }
210
211 int tzplatform_has_system_group(uid_t uid)
212 {
213         return _has_system_group_static_(uid);
214 }
215
216 #ifdef TEST
217 #include <stdlib.h>
218 #include <stdio.h>
219
220 int main()
221 {
222         int i;
223         struct tzplatform_context *context;
224         enum tzplatform_variable id;
225         const char *name;
226         const char *value;
227         int xid;
228         uid_t uid;
229
230         i = 0;
231         while (i != tzplatform_getcount()) {
232                 id = (enum tzplatform_variable)i;
233                 name = tzplatform_getname(id);
234                 value = tzplatform_getenv(id);
235                 xid = (int)tzplatform_getid(name);
236                 printf("%d=%d\t%s=%s\n", i, xid, name, value ? value : "<null>");
237                 i++;
238         }
239
240         printf("------------------------\n");
241         i = tzplatform_context_create(&context);
242         if (i) {
243                 printf("error while creating context %d\n", i);
244                 return 1;
245         }
246
247         uid = (uid_t)0;
248         i = tzplatform_context_set_user(context, uid);
249         if (i) {
250                 printf("error %d while switching to user %d\n", i, (int)uid);
251                 return 1;
252         }
253         i = 0;
254         while (i != tzplatform_getcount()) {
255                 id = (enum tzplatform_variable)i;
256                 name = tzplatform_getname(id);
257                 value = tzplatform_context_getenv(context, id);
258                 xid = (int)tzplatform_getid(name);
259                 printf("%d=%d\t%s=%s\n", i, xid, name, value ? value : "<null>");
260                 i++;
261         }
262         tzplatform_context_destroy(context);
263
264         return 0;
265 }
266 #endif