98f5e20e7b76d9bcc692914f8a15974b37b0fc7f
[platform/core/system/tizen-platform-wrapper.git] / src / static-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 "tzplatform_variables.h"
32 #include "tzplatform_config.h"
33
34 #include "shared-api.h"
35 #include "groups.h"
36
37 #include "signup.inc"
38
39 int tzplatform_getcount()
40 {
41     return _TZPLATFORM_VARIABLES_COUNT_;
42 }
43
44 const char* tzplatform_getname(enum tzplatform_variable id)
45 {
46     return _getname_tzplatform_(id, tizen_platform_config_signup);
47 }
48
49 enum tzplatform_variable tzplatform_getid(const char *name)
50 {
51     return _getid_tzplatform_(name, tizen_platform_config_signup);
52 }
53
54 const char* tzplatform_getenv(enum tzplatform_variable id) 
55 {
56     return _getenv_tzplatform_(id, tizen_platform_config_signup);
57 }
58
59 const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id)
60 {
61     return _context_getenv_tzplatform_(id, tizen_platform_config_signup, context);
62 }
63
64 int tzplatform_getenv_int(enum tzplatform_variable id)
65 {
66     return _getenv_int_tzplatform_(id, tizen_platform_config_signup);
67 }
68
69 int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id)
70 {
71     return _context_getenv_int_tzplatform_(id, tizen_platform_config_signup, context);
72 }
73
74 const char* tzplatform_mkstr(enum tzplatform_variable id, const char * str)
75 {
76     return _mkstr_tzplatform_(id, str, tizen_platform_config_signup);
77 }
78
79 const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str)
80 {
81     return _context_mkstr_tzplatform_(id, str, tizen_platform_config_signup, context);
82 }
83
84 const char* tzplatform_mkpath(enum tzplatform_variable id, const char * path)
85 {
86     return _mkpath_tzplatform_(id, path, tizen_platform_config_signup);
87 }
88
89 const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path)
90 {
91     return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, context);
92 }
93
94 const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path, const char* path2)
95 {
96     return _mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup);
97 }
98
99 const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2)
100 {
101     return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, context);
102 }
103
104 const char* tzplatform_mkpath4(enum tzplatform_variable id, const char * path, const char* path2, const char *path3)
105 {
106     return _mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup);
107 }
108
109 const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
110 {
111     return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, context);
112 }
113
114 uid_t tzplatform_getuid(enum tzplatform_variable id)
115 {
116     return _getuid_tzplatform_(id, tizen_platform_config_signup);
117 }
118
119 uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id)
120 {
121     return _context_getuid_tzplatform_(id, tizen_platform_config_signup, context);
122 }
123
124 gid_t tzplatform_getgid(enum tzplatform_variable id)
125 {
126     return _getgid_tzplatform_(id, tizen_platform_config_signup);
127 }
128
129 gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id)
130 {
131     return _context_getgid_tzplatform_(id, tizen_platform_config_signup, context);
132 }
133
134 int tzplatform_has_system_group(uid_t uid) 
135 {
136         return _has_specified_group_static_(uid, TZ_SYS_ADMIN_GROUP);
137 }
138
139 int tzplatform_has_users_group(uid_t uid)
140 {
141   return _has_specified_group_static_(uid, TZ_SYS_USER_GROUP);
142 }
143
144 #ifdef TEST
145 #include <stdlib.h>
146 #include <stdio.h>
147
148 int main() {
149     int i;
150     struct tzplatform_context *context;
151     enum tzplatform_variable id;
152     const char *name;
153     const char *value;
154     int xid;
155     uid_t uid;
156
157     i = 0;
158     while(i != tzplatform_getcount()) {
159         id = (enum tzplatform_variable)i;
160         name = tzplatform_getname(id);
161         value = tzplatform_getenv(id);
162         xid = (int)tzplatform_getid(name);
163         printf("%d=%d\t%s=%s\n",i,xid,name,value?value:"<null>");
164         i++;
165     }
166
167     printf("------------------------\n");
168     i = tzplatform_context_create(&context);
169     if (i) {
170         printf("error while creating context %d\n",i);
171         return 1;
172     }
173
174     uid = (uid_t)0;
175     i = tzplatform_context_set_user(context, uid);
176     if (i) {
177         printf("error %d while switching to user %d\n",i,(int)uid);
178         return 1;
179     }
180     i = 0;
181     while(i != tzplatform_getcount()) {
182         id = (enum tzplatform_variable)i;
183         name = tzplatform_getname(id);
184         value = tzplatform_context_getenv(context, id);
185         xid = (int)tzplatform_getid(name);
186         printf("%d=%d\t%s=%s\n",i,xid,name,value?value:"<null>");
187         i++;
188     }
189     tzplatform_context_destroy(context);
190
191     return 0;
192 }
193 #endif
194
195