2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file systemservice_loader.c
20 * @brief This is the implementation for the osp-system-service-loader.
34 #define LOG_TAG "LOADER"
37 #define MAX_APP_EXECUTABLE_NAME 230
39 #define MIN(a,b) ((a) < (b) ? (a) : (b))
43 print_args(int argc, char* argv[])
50 LOGI("%dth arg : [%s]", i, p);
55 osp_do_pre_exe(const char* bin_path)
60 int (*do_pre_exec_fn)(const char*, const char*) = NULL;
62 handle = dlopen("libosp-env-config.so", RTLD_LAZY | RTLD_LOCAL);
65 LOGE("Failed to dlopen libosp-env-config.so (%s)", dlerror());
70 do_pre_exec_fn = (int (*)(const char*, const char*)) dlsym(handle, "do_pre_exec");
74 LOGE("Failed to dlsym() (%s)", errormsg);
80 ret = do_pre_exec_fn(NULL, bin_path);
83 LOGE("Failed to do_pre_exe() (%d)", ret);
85 LOGI("do_pre_exec() ok");
93 get_appid_executable_name_from_path(const char path[], char appid[], char executable_name[])
95 // path is ".../[appid]/bin/[executable_name]"
97 // Calculate the header
98 const char* p = strrchr(path, '/');
101 LOGI("Improper path %s", path);
105 const int path_len = strlen(path);
106 const int exec_len = strlen(p);
107 if (exec_len <= 0 || exec_len > PATH_MAX || path_len <= 0 || path_len > PATH_MAX)
109 LOGI("Improper path %s", path);
113 strncpy(executable_name, p + 1, exec_len);
116 LOGI("Exec is %s", executable_name);
119 if (path_len < /* '/' */ 1 + 10 + strlen("/bin/") + exec_len)
121 LOGI("Improper path %s", path);
125 strncpy(appid, p - strlen("bin/") - 10, 10);
127 LOGI("PackageId is %s", appid);
134 main(int argc, char* argv[])
136 void* real_handle = NULL;
138 char appid[MAX_APPID];
139 char executable_name[MAX_APP_EXECUTABLE_NAME];
141 void (*pAppInfoInit)(const char*, const char*, int, char* [], int) = NULL;
142 int (*pRealMain)(int, char* []) = NULL;
144 memset(appid, 0, sizeof(appid));
145 memset(executable_name, 0, sizeof(executable_name));
147 LOGI("Initializing.");
148 LOGI("argc %d, argv 0x%x.", argc, argv);
149 print_args(argc, argv);
151 // convert package path to appId
152 get_appid_executable_name_from_path(argv[0], appid, executable_name);
153 LOGI("Osp appId %s.", appid);
159 osp_do_pre_exe(argv[0]);
161 // do not adjust privilege : root for system services
168 snprintf(buffer, 1024, "%s.exe", argv[0]);
171 real_handle = dlopen(buffer, RTLD_LAZY);
174 LOGE("Failed to open %s : %s.", buffer, dlerror());
178 pAppInfoInit = (void (*)(const char*, const char*, int, char*[], int)) dlsym(real_handle, "InitAppInfo");
179 errormsg = dlerror();
180 if (errormsg != NULL)
182 LOGE("Failed to find InitAppInfo() : %s.", errormsg);
183 dlclose(real_handle);
184 fprintf(stderr, "executable does not have proper osp library dependency.\n");
188 pRealMain = (int (*)(int, char*[])) dlsym(real_handle, "OspMain");
189 errormsg = dlerror();
190 if (errormsg != NULL)
192 LOGE("Failed to find OspMain() : %s.", errormsg);
193 dlclose(real_handle);
197 // actual initialization
198 (*pAppInfoInit)(appid, executable_name, argc, argv, -1);
199 (*pRealMain)(argc, argv);
201 LOGI("Osp application terminates.");
203 //dlclose(real_handle);
204 LOGI("Osp cleanup finished for %s.", argv[0]);