Use private dbus connection for synchronous calls.
[platform/core/system/libstorage.git] / src / storage-inhouse.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <tzplatform_config.h>
25 #include <blkid.h>
26 #include <libsyscommon/libgdbus.h>
27 #include <libsyscommon/common.h>
28
29 #include "common.h"
30 #include "log.h"
31 #include "storage-internal.h"
32 #include "storage-external-dbus.h"
33
34 #define FORMAT_TIMEOUT  (120*1000)
35 #define USER_PARTITION "user"
36 #define CONTAINER_USER_PARTITION "contain-user"
37
38 /*
39         Get compat path from origin Multi-user path
40         from TZ_USER_CONTENT/.. to /opt/usr/media/..
41         Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization)
42
43         Why this API should be provided?
44         In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
45         However, although system daemon operates real path,
46                 system daemon needs to provide compat path to App if the real path is converted.
47
48         Usage:
49                 #include <storage-internal.h>
50
51                 char dest[100];
52                 if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0)
53                         // cannot convert. use src path
54                 else
55                         // can convert. use dest path
56  */
57 //LCOV_EXCL_START Untested function
58 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
59 {
60         int r = -1;
61         int str_len;
62         const char* str;
63
64         if (!compat || !origin) {
65                 _E("Invalid parameter");
66                 return -1;
67         }
68
69         if (getuid() <= USER_UID_START) {
70                 //LCOV_EXCL_START System Error
71                 _E("Only apps and user session daemons are allowed "
72                                 "to use storage_get_compat_internal_path()");
73                 return -1;
74                 //LCOV_EXCL_STOP
75         }
76
77         // this API works on place where compat path is bind-mounted
78         if (!is_compat_bind_mount()) {
79                 //LCOV_EXCL_START System Error
80                 _E("No compat bind mount");
81                 return -1;
82                 //LCOV_EXCL_STOP
83         }
84
85         str = tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT);
86         str_len = strlen(str);
87         if (strncmp(origin, str, str_len) != 0) {
88                 _E("Failed to match TZ_USER_CONTENT");
89                 return -1;
90         }
91
92         r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
93         if (r < 0) {
94                 //LCOV_EXCL_START System Error
95                 _E("Failed to create new path");
96                 return -1;
97                 //LCOV_EXCL_STOP
98         }
99
100         return 0;
101 }
102 //LCOV_EXCL_STOP
103
104 /*
105         Get Multi-user path from compat path
106         from /opt/usr/media/.. to TZ_USER_CONTENT/..
107         Input should be normalized path like /opt/usr/media (TODO: internal normalization)
108
109         Why this API should be provided?
110         In multi-user environment, each user has own compat content direcotry.(/opt/usr/media)
111         However, although some APIs send the compat path to system daemon,
112                 system daemon should access real path.
113
114         Usage:
115                 #include <storage-internal.h>
116
117                 char dest[100];
118                 if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0)
119                         // cannot convert. use src path
120                 else
121                         // can convert. use dest path
122 */
123 API int storage_get_origin_internal_path(const char* compat, int len, char* origin)
124 {
125         int r;
126         int compat_len;
127
128         if (!compat || !origin) {
129                 _E("Invalid parameter");
130                 return -1;
131         }
132
133         if (getuid() <= USER_UID_START) {
134                 //LCOV_EXCL_START System Error
135                 _E("Only apps and user session daemons are allowed "
136                                 "to use storage_get_origin_internal_path()");
137                 return -1;
138                 //LCOV_EXCL_STOP
139         }
140
141         // this API works on place where compat path is bind-mounted
142         if (!is_compat_bind_mount()) {
143                 //LCOV_EXCL_START System Error
144                 _E("no compat bind mount");
145                 return -1;
146                 //LCOV_EXCL_STOP
147         }
148
149         compat_len = strlen(COMPAT_DIR);
150         if (strncmp(compat, COMPAT_DIR, compat_len) != 0) {
151                 _E("failed to match COMPAT_DIR");
152                 return -1;
153         }
154
155         r = snprintf(origin, len, "%s%s", tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT), compat + compat_len);
156         if (r < 0) {
157                 //LCOV_EXCL_START System Error
158                 _E("failed to create new path");
159                 return -1;
160                 //LCOV_EXCL_STOP
161         }
162
163         return 0;
164 }
165
166 API int storage_get_primary_sdcard(int *storage_id, char **path)
167 {
168         GVariant *reply;
169         int ret, ret_dbus;
170         char *reply_mount_point = NULL;
171         int reply_id;
172
173         if (!storage_id || !path)
174                 return STORAGE_ERROR_INVALID_PARAMETER;
175
176         if (!storage_ext_is_supported())
177                 return STORAGE_ERROR_NOT_SUPPORTED;
178
179         dbus_handle_h dbus_handle = gdbus_get_connection(G_BUS_TYPE_SYSTEM, true);
180         if (dbus_handle == NULL) {
181                 _E("Failed to get dbus connection");
182                 return STORAGE_ERROR_OPERATION_FAILED;
183         }
184
185         ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
186                         STORAGE_EXT_BUS_NAME,
187                         STORAGE_EXT_PATH_MANAGER,
188                         STORAGE_EXT_IFACE_MANAGER,
189                         "GetMmcPrimary",
190                         NULL,
191                         &reply);
192
193         gdbus_free_connection(dbus_handle);
194
195         if (ret_dbus < 0) {
196                 //LCOV_EXCL_START System Error
197                 _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
198                 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
199                 //LCOV_EXCL_STOP
200         }
201
202         if (!g_variant_get_safe(reply, "(issssssisibii)",
203                         NULL, NULL, NULL,
204                         NULL, NULL,
205                         NULL, NULL,
206                         NULL, &reply_mount_point,
207                         NULL, NULL,
208                         NULL, &reply_id)) {
209                 g_variant_unref(reply);
210                 return STORAGE_ERROR_OPERATION_FAILED;
211         }
212
213         g_variant_unref(reply);
214
215         if (reply_id < 0) {
216                 ret = STORAGE_ERROR_NO_DEVICE;
217                 goto out;
218         }
219
220         *path = strdup(reply_mount_point);
221         if (*path == NULL) {
222                 ret = STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
223                 goto out;
224         }
225
226         *storage_id = reply_id;
227
228         ret = STORAGE_ERROR_NONE;
229 out:
230         g_free(reply_mount_point);
231
232         return ret;
233 }
234
235 API int storage_get_storage_level(const char *path, char **level)
236 {
237         int ret_level;
238
239         if (!level || !path)
240                 return STORAGE_ERROR_INVALID_PARAMETER;
241
242         ret_level = storage_ext_get_storage_level(path, level);
243         if (ret_level == -ENOMEM)
244                 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
245         else if (ret_level == -EINVAL)
246                 return STORAGE_ERROR_INVALID_PARAMETER;
247         else if (ret_level < 0)
248                 return STORAGE_ERROR_OPERATION_FAILED;
249
250         return STORAGE_ERROR_NONE;
251 }
252
253 //LCOV_EXCL_START Not called callback
254 static void mount_mmc_cb(GVariant *var, void *user_data, GError *err)
255 {
256         struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
257         int mmc_ret = -1;
258
259         _D("mount_mmc_cb called");
260
261         if (!var) {
262                 _E("no message [%s]", err->message);
263                 mmc_ret = -EBADMSG;
264                 goto exit;
265         }
266
267         g_variant_get(var, "(i)", &mmc_ret);
268
269         _I("Mount State : %d", mmc_ret);
270
271 exit:
272         if (var)
273                 g_variant_unref(var);
274         (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
275 }
276 //LCOV_EXCL_STOP
277
278 API int storage_request_mount_mmc(struct mmc_contents *mmc_data)
279 {
280         void (*mount_cb)(GVariant *, void *, GError *) = NULL;
281         void *data = NULL;
282         char *path;
283         int ret_val;
284         int id;
285
286         if (mmc_data && mmc_data->mmc_cb) {
287                 _I("Mount callback exists");
288                 mount_cb = mount_mmc_cb;
289                 data = mmc_data;
290         }
291
292         ret_val = storage_get_primary_sdcard(&id, &path);
293         if (ret_val != STORAGE_ERROR_NONE)
294                 return ret_val;
295 //LCOV_EXCL_START System Error
296         if (path)
297                 free(path);
298 //LCOV_EXCL_STOP
299
300         ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
301                         STORAGE_EXT_PATH_MANAGER,
302                         STORAGE_EXT_IFACE_MANAGER,
303                         "Mount",
304                         g_variant_new("(is)", id, ""),
305                         mount_cb,
306                         -1,
307                         data);
308
309         _I("Mount Request %s", ret_val == 0 ? "Success" : "Failed");
310
311         if (ret_val == -ENOMEM)
312                 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
313         if (ret_val < 0)
314                 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
315
316         return STORAGE_ERROR_NONE;
317 }
318
319 //LCOV_EXCL_START Not called callback
320 static void unmount_mmc_cb(GVariant *var, void *user_data, GError *err)
321 {
322         struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
323         int mmc_ret;
324
325         _D("unmount_mmc_cb called");
326
327         if (!var) {
328                 _E("no message [%s]", err->message);
329                 mmc_ret = -EBADMSG;
330                 goto exit;
331         }
332
333         g_variant_get(var, "(i)", &mmc_ret);
334
335         _I("Unmount State : %d", mmc_ret);
336
337 exit:
338         if (var)
339                 g_variant_unref(var);
340         (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
341 }
342 //LCOV_EXCL_STOP
343
344 API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
345 {
346         void (*unmount_cb)(GVariant *, void *, GError *) = NULL;
347         void *data = NULL;
348         char *path;
349         int ret_val;
350         int id;
351
352         if (option < 0 || option > 1)
353                 return STORAGE_ERROR_INVALID_PARAMETER;
354
355         if (mmc_data && mmc_data->mmc_cb) {
356                 _I("Unmount callback exists");
357                 unmount_cb = unmount_mmc_cb;
358                 data = mmc_data;
359         }
360
361         ret_val = storage_get_primary_sdcard(&id, &path);
362         if (ret_val != STORAGE_ERROR_NONE)
363                 return ret_val;
364 //LCOV_EXCL_START System Error
365         if (path)
366                 free(path);
367 //LCOV_EXCL_STOP
368
369         ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
370                         STORAGE_EXT_PATH_MANAGER,
371                         STORAGE_EXT_IFACE_MANAGER,
372                         "Unmount",
373                         g_variant_new("(ii)", id, option),
374                         unmount_cb,
375                         -1,
376                         data);
377
378         _I("Unmount Request %s", ret_val == 0 ? "Success" : "Failed");
379
380         if (ret_val == -ENOMEM)
381                 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
382         if (ret_val < 0)
383                 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
384
385         return STORAGE_ERROR_NONE;
386 }
387
388 //LCOV_EXCL_START Not called callback
389 static void format_mmc_cb(GVariant *var, void *user_data, GError *err)
390 {
391         struct mmc_contents *mmc_data = (struct mmc_contents*)user_data;
392         int mmc_ret;
393
394         _D("format_mmc_cb called");
395
396         if (!var) {
397                 _E("no message [%s]", err->message);
398                 mmc_ret = -EBADMSG;
399                 goto exit;
400         }
401
402         g_variant_get(var, "(i)", &mmc_ret);
403
404         _I("Format State : %d", mmc_ret);
405
406 exit:
407         if (var)
408                 g_variant_unref(var);
409         (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data);
410 }
411 //LCOV_EXCL_STOP
412
413 API int storage_request_format_mmc(struct mmc_contents *mmc_data)
414 {
415         return storage_format_mmc(mmc_data, 1);
416 }
417
418 API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
419 {
420         void (*format_cb)(GVariant *, void *, GError *) = NULL;
421         void *data = NULL;
422         char *path;
423         int ret_val;
424         int id;
425
426         if (option < 0 || option > 1)
427                 return STORAGE_ERROR_INVALID_PARAMETER;
428
429         if (mmc_data && mmc_data->mmc_cb) {
430                 _I("Format callback exists");
431                 format_cb = format_mmc_cb;
432                 data = mmc_data;
433         }
434
435         ret_val = storage_get_primary_sdcard(&id, &path);
436         if (ret_val != STORAGE_ERROR_NONE)
437                 return ret_val;
438 //LCOV_EXCL_START System Error
439         if (path)
440                 free(path);
441 //LCOV_EXCL_STOP
442
443         ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
444                         STORAGE_EXT_PATH_MANAGER,
445                         STORAGE_EXT_IFACE_MANAGER,
446                         "Format",
447                         g_variant_new("(ii)", id, option),
448                         format_cb,
449                         FORMAT_TIMEOUT,
450                         data);
451
452         _I("Format Request %s", ret_val == 0 ? "Success" : "Failed");
453
454         if (ret_val == -ENOMEM)
455                 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
456         if (ret_val < 0)
457                 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
458
459         return STORAGE_ERROR_NONE;
460 }
461
462 API int storage_is_mounted_opt_usr(storage_part_mount_e *mounted)
463 {
464         blkid_cache cache = NULL;
465         blkid_dev_iterate iter;
466         blkid_dev dev;
467         int ret_val;
468         bool found = false;
469         char *user_label = libsys_is_container()? CONTAINER_USER_PARTITION: USER_PARTITION;
470
471         if (!mounted)
472                 return STORAGE_ERROR_INVALID_PARAMETER;
473
474         _D("Find user partition label: %s", user_label);
475         ret_val = blkid_get_cache(&cache, NULL);
476         if (ret_val < 0) {
477                 _E("Failed to get cache"); //LCOV_EXCL_LINE
478                 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
479                 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
480         }
481
482         ret_val = blkid_probe_all(cache);
483         if (ret_val < 0) {
484                 _E("Failed to probe all block devices"); //LCOV_EXCL_LINE
485                 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
486                 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
487         }
488
489         iter = blkid_dev_iterate_begin(cache);
490         if (!iter) {
491                 _E("Failed to get iterate"); //LCOV_EXCL_LINE
492                 *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
493                 return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
494         }
495
496         ret_val = blkid_dev_set_search(iter, "LABEL", user_label);
497         if (blkid_dev_next(iter, &dev) == 0) {
498                 dev = blkid_verify(cache, dev);
499                 if (dev) {
500                         found = true;
501                         _D("Partition for user data is found(LABEL=%s)", user_label);
502                 }
503         }
504         blkid_dev_iterate_end(iter);
505
506         if (!found) {
507                 iter = blkid_dev_iterate_begin(cache);
508                 if (!iter) {
509                         _E("Failed to get iterate"); //LCOV_EXCL_LINE
510                         *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE
511                         return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
512                 }
513
514                 ret_val = blkid_dev_set_search(iter, "PARTLABEL", user_label);
515                 if (blkid_dev_next(iter, &dev) == 0) {
516                         dev = blkid_verify(cache, dev);
517                         if (dev) {
518                                 found = true;
519                                 _D("Partition for user data is found(PARTLABEL=%s)", user_label);
520                         }
521                 }
522                 blkid_dev_iterate_end(iter);
523         }
524
525         blkid_put_cache(cache);
526
527         if (found) {
528                 ret_val = mount_check(tzplatform_getenv(TZ_SYS_USER));
529                 if (ret_val)
530                         *mounted = STORAGE_PART_MOUNTED;
531                 else
532                         *mounted = STORAGE_PART_NOT_MOUNTED;
533         } else
534                 *mounted = STORAGE_PART_NOT_SUPPORTED;
535
536         return STORAGE_ERROR_NONE;
537 }