3fb299e982c38e19b466b635019925dc7878dd47
[platform/core/context/context-common.git] / src / server / ServerUtil.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <dirent.h>
21 #include <systemd/sd-login.h>
22 #include <ScopeMutex.h>
23 #include <ServerUtil.h>
24
25 #define ROOT_UID        0
26
27 using namespace ctx;
28
29 static GMutex __pathMutex;
30 static GDBusConnection* __dbusConnection = NULL;
31
32 EXPORT_API bool util::is_normal_user(uid_t uid)
33 {
34         if (uid == ROOT_UID)
35                 return false;
36
37         return true;
38 }
39
40 EXPORT_API bool util::is_container_user(uid_t uid)
41 {
42         return false;
43 }
44
45 EXPORT_API bool util::is_system(uid_t uid)
46 {
47         return (!is_normal_user(uid) && !is_container_user(uid));
48 }
49
50 EXPORT_API std::string util::get_system_path(enum tzplatform_variable id, const std::string& path)
51 {
52         ScopeMutex sm(&__pathMutex);
53
54         const char* rawPath = tzplatform_mkpath(id, path.c_str());
55         IF_FAIL_RETURN_TAG(rawPath, EMPTY_STR, _E, "Path creation failed");
56
57         return std::string(rawPath);
58 }
59
60 EXPORT_API std::string util::get_user_path(uid_t uid, enum tzplatform_variable id, const std::string& path)
61 {
62         IF_FAIL_RETURN_TAG(!util::is_system(uid), EMPTY_STR, _E, "Invalid UID");
63
64         ScopeMutex sm(&__pathMutex);
65
66         tzplatform_context* context = NULL;
67         tzplatform_context_create(&context);
68         IF_FAIL_RETURN_TAG(context, EMPTY_STR, _E, "tzplatform_context_create() failed");
69
70         if (tzplatform_context_set_user(context, uid) != E_NONE) {
71                 _E("tzplatform_context_set_user() failed");
72                 tzplatform_context_destroy(context);
73                 return EMPTY_STR;
74         }
75
76         const char* rawPath = tzplatform_context_mkpath(context, id, path.c_str());
77
78         if (!rawPath) {
79                 _E("Path creation failed");
80                 tzplatform_context_destroy(context);
81                 return EMPTY_STR;
82         }
83
84         std::string outPath(rawPath);
85
86         tzplatform_context_destroy(context);
87
88         return outPath;
89 }
90
91 EXPORT_API std::vector<std::string> util::get_files(const std::string& dirPath, bool symlink)
92 {
93         static GMutex dirMutex;
94
95         DIR* dir = NULL;
96         struct dirent* entry = NULL;
97         struct stat fileStat;
98         std::string filePath;
99         std::vector<std::string> fileNames;
100
101         ScopeMutex sm(&dirMutex);
102
103         dir = opendir(dirPath.c_str());
104         IF_FAIL_RETURN_TAG(dir, fileNames, _E, "Failed to open: %s", dirPath.c_str());
105
106         while (true) {
107                 entry = readdir(dir);
108                 if (!entry)
109                         break;
110
111                 filePath = dirPath + "/" + entry->d_name;
112                 if (symlink) {
113                         if (stat(filePath.c_str(), &fileStat) != 0)
114                                 continue;
115                 } else {
116                         if (lstat(filePath.c_str(), &fileStat) != 0)
117                                 continue;
118                 }
119
120                 if (!S_ISREG(fileStat.st_mode))
121                         continue;
122
123                 fileNames.push_back(entry->d_name);
124         }
125
126         closedir(dir);
127         return fileNames;
128 }
129
130 EXPORT_API void util::set_dbus_connection(GDBusConnection* conn)
131 {
132         __dbusConnection = conn;
133 }
134
135 EXPORT_API GDBusConnection* util::get_dbus_connection()
136 {
137         return __dbusConnection;
138 }