Release version 0.15.0
[platform/core/appfw/slp-pkgmgr.git] / client / src / pkgmgr_client_internal.c
1 /*
2  * slp-pkgmgr
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24
25
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdbool.h>
31 #include <ctype.h>
32 #include <xdgmime.h>
33
34 #include <unistd.h>
35 #include <dlfcn.h>
36 #include <sys/time.h>
37 #include <tzplatform_config.h>
38 #include <pkgmgr-info.h>
39
40 #include "package-manager.h"
41 #include "pkgmgr_client_debug.h"
42 #include "pkgmgr_client_internal.h"
43
44 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
45
46 #define IS_WHITESPACE(CHAR) \
47         ((CHAR == ' ' || CHAR == '\t' || CHAR == '\r' || CHAR == '\n') ? \
48         true : false)
49
50 void _app_str_trim(char *input)
51 {
52         char *trim_str = input;
53
54         if (input == NULL)
55                 return;
56
57         while (*input != 0) {
58                 if (!IS_WHITESPACE(*input)) {
59                         *trim_str = *input;
60                         trim_str++;
61                 }
62                 input++;
63         }
64
65         *trim_str = 0;
66         return;
67 }
68
69 static pkg_plugin_set *plugin_set_list[24] = { 0, };
70
71 pkg_plugin_set *_pkg_plugin_load_library(const char *pkg_type,
72                 const char *library_path)
73 {
74         void *library_handle;
75         int i;
76         bool (*on_load)(pkg_plugin_set *plugin);
77
78         if (library_path == NULL || pkg_type == NULL) {
79                 ERR("invalid parameter");
80                 return NULL;
81         }
82
83         for (i = 0; plugin_set_list[i]; i++) {
84                 if (strcmp(plugin_set_list[i]->pkg_type, pkg_type) == 0) {
85                         DBG("already loaded [%s]", library_path);
86                         return plugin_set_list[i];
87                 }
88         }
89
90         if ((library_handle = dlopen(library_path, RTLD_LAZY)) == NULL) {
91                 ERR("dlopen is failed library_path[%s]", library_path);
92                 return NULL;
93         }
94
95         if ((on_load = dlsym(library_handle, "pkg_plugin_on_load")) == NULL ||
96                         dlerror() != NULL) {
97                 ERR("can not find symbol");
98                 dlclose(library_handle);
99                 return NULL;
100         }
101
102         plugin_set_list[i] = (pkg_plugin_set *)calloc(1, sizeof(pkg_plugin_set));
103         if (plugin_set_list[i] == NULL) {
104                 ERR("malloc of the plugin_set_list element is failed");
105                 dlclose(library_handle);
106                 return NULL;
107         }
108
109         if (on_load(plugin_set_list[i]) != 0) {
110                 ERR("pkg_plugin_on_load failed");
111                 free(plugin_set_list[i]);
112                 dlclose(library_handle);
113                 plugin_set_list[i] = NULL;
114                 return NULL;
115         }
116
117         plugin_set_list[i]->plugin_handle = library_handle;
118         snprintf(plugin_set_list[i]->pkg_type,
119                         sizeof(plugin_set_list[i]->pkg_type), "%s", pkg_type);
120
121         DBG("library [%s] is loaded", library_path);
122
123         return plugin_set_list[i];
124 }
125
126 int _pkg_plugin_get_library_path(const char *pkg_type, char *library_path)
127 {
128         FILE *fp = NULL;
129         char buffer[1024] = { 0 };
130
131         if (pkg_type == NULL || library_path == NULL) {
132                 ERR("invalid argument\n");
133                 return -1;
134         }
135
136         fp = fopen(PKG_CONF_PATH, "r");
137         if (fp == NULL) {
138                 ERR("no matching backendlib\n");
139                 return PKGMGR_R_ERROR;
140         }
141
142         char *path = NULL;
143         while (fgets(buffer, 1024, fp) != NULL) {
144                 if (buffer[0] == '#')
145                         continue;
146
147                 _app_str_trim(buffer);
148
149                 if ((path = strstr(buffer, PKG_BACKENDLIB)) != NULL) {
150                         DBG("[%s]\n", buffer);
151                         DBG("[%s]\n", path);
152                         path = path + strlen(PKG_BACKENDLIB);
153                         DBG("[%s]\n", path);
154
155                         break;
156                 }
157
158                 memset(buffer, 0x00, 1024);
159         }
160
161         if (fp != NULL)
162                 fclose(fp);
163
164         if (path == NULL) {
165                 ERR("no matching backendlib\n");
166                 return PKGMGR_R_ERROR;
167         }
168
169         snprintf(library_path, 1024, "%slib%s.so", path, pkg_type);
170
171         return PKGMGR_R_OK;
172
173 }
174
175 pkg_plugin_set *_package_manager_load_library(const char *pkg_type)
176 {
177         char package_path[1024] = { 0, };
178         pkg_plugin_set *plugin_set;
179
180         if (pkg_type == NULL) {
181                 ERR("cannot load library - pkg_type is null");
182                 return NULL;
183         }
184
185         if (_pkg_plugin_get_library_path(pkg_type, package_path) !=
186                         PKGMGR_R_OK) {
187                 ERR("cannot find path");
188                 return NULL;
189         }
190
191         plugin_set = _pkg_plugin_load_library(pkg_type, package_path);
192         if (plugin_set == NULL) {
193                 ERR("cannot load library");
194                 return NULL;
195         }
196
197         return plugin_set;
198 }