1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sched.h>
#include <sys/mount.h>
#include <tzplatform_config.h>
// For compatibility, Using hard-coded path
#define LEGACY_CONTENTS_DIR "/opt/usr/media"
#define LEGACY_APPS_DIR "/opt/usr/apps"
#define LAZYMOUNT_LIB "/usr/lib/liblazymount.so.0"
#define CONTAINER_LIB "/usr/lib/security/pam_krate.so"
#define LOAD_SYMBOL(handle, sym, name) \
do{ \
sym = dlsym(handle, name); \
if (!sym) { \
fprintf(stderr, "dlsym %s error\n", name); \
dlclose(handle); \
return -1; \
} \
} while (0);
static void *container_handle = NULL;
static int normal_user_preprocess(char *username)
{
int r;
r = unshare(CLONE_NEWNS);
if (r < 0) {
fprintf(stderr,"unshare failed\n");
return r;
}
r = mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL);
if (r < 0) {
fprintf(stderr,"Failed to change the propagation type of root to SLAVE\n");
return r;
}
return 0;
}
static int normal_user_postprocess(char *username)
{
int r;
r = mount(tzplatform_getenv(TZ_USER_CONTENT),
LEGACY_CONTENTS_DIR, NULL, MS_BIND, NULL);
if (r < 0) {
fprintf(stderr, "user content bind mount failed - %d\n",errno);
return r;
}
r = mount(tzplatform_getenv(TZ_USER_APP),
LEGACY_APPS_DIR, NULL, MS_BIND, NULL);
if (r < 0) {
fprintf(stderr, "user app bind mount failed - %d\n",errno);
return r;
}
return 0;
}
static int container_open(void)
{
if (container_handle)
return 0;
container_handle = dlopen(CONTAINER_LIB, RTLD_LAZY);
if (!container_handle) {
fprintf(stderr, "container module dlopen error\n");
return -1;
}
return 0;
}
static int container_preprocess(char *username)
{
int r;
int (*handle_preprocess)(char *);
r = container_open();
if (r < 0)
return r;
LOAD_SYMBOL(container_handle, handle_preprocess, "container_preprocess");
r = handle_preprocess(username);
if (r < 0) {
fprintf(stderr, "container module preprocess error\n");
return r;
}
return 0;
}
static int container_postprocess(char *username)
{
int r;
int (*handle_postprocess)(char *);
r = container_open();
if (r < 0)
return r;
LOAD_SYMBOL(container_handle, handle_postprocess, "container_postprocess");
r = handle_postprocess(username);
if (r < 0) {
fprintf(stderr, "container module postprocess error\n");
return r;
}
return 0;
}
static int wait_condition(void)
{
int r;
void *h;
int (*wait_mount_user)(void);
r = access(LAZYMOUNT_LIB,F_OK);
if (r < 0){
fprintf(stderr, "cannot find lazymount module - No support lazymount\n");
return 0;
}
h = dlopen(LAZYMOUNT_LIB, RTLD_LAZY);
if (!h) {
fprintf(stderr, "lazymount module dlopen error\n");
return -1;
}
LOAD_SYMBOL(h, wait_mount_user, "wait_mount_user");
r = wait_mount_user();
if (r < 0) {
fprintf(stderr, "wait_mout_user failed\n");
dlclose(h);
return r;
}
dlclose(h);
return 0;
}
int main(int argc, char *argv[])
{
int r = 0;
int support_container = 0;
if (argc < 2) {
fprintf(stderr, "require user argument\n");
return -1;
}
/* pre-processing */
r = normal_user_preprocess(argv[1]);
if (r < 0) {
fprintf(stderr, "normal user preprocess failed\n");
return r;
}
/* If container supports below funcs, below line should be enabled. */
support_container = (access(CONTAINER_LIB, F_OK) == 0) ? 1 : 0;
if (support_container) {
r = container_preprocess(argv[1]);
if (r < 0) {
fprintf(stderr, "container preprocess failed\n");
return r;
}
}
/* TODO: fork & exec */
r = system("/usr/lib/systemd/systemd --user &");
if (r < 0) {
fprintf(stderr, "systemd user execution failed\n");
return r;
}
/* sync-style since there is no need to process other signal */
wait_condition();
/* post-processing */
r = normal_user_postprocess(argv[1]);
if (r < 0) {
fprintf(stderr, "normal user postprocess failed\n");
return r;
}
if (support_container) {
r = container_postprocess(argv[1]);
if (r < 0) {
fprintf(stderr, "container postprocess failed\n");
return r;
}
}
return 0;
}
|