4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include <sys/types.h>
34 #include <ctype.h> /* for isspace () */
37 #include <sys/types.h>
42 #include "native_installer_util.h"
46 #define LOG_TAG "nativeinstaller"
47 #define LOG_TAG_BACKEND "native-backend"
48 #define LOG_TAG_FRONTEND "native-frontend"
52 #define NATIVE_INSTALLER_LOG_FILE "/tmp/native-installer"
57 * This is intended to be a faster splitter, it does not use dynamic
58 * memories. Input is changed to insert nulls at each token location.
60 int _tok_split_string(char tok, char *input, char **list, unsigned long listmax)
62 /* Strip any leading spaces */
64 char *Stop = Start + strlen(Start);
65 for (; *Start != 0 && isspace(*Start) != 0; Start++) ;
67 unsigned long Count = 0;
70 /* Skip to the next Token */
71 for (; Pos != Stop && *Pos != tok; Pos++) ;
73 /* Back remove spaces */
76 End > Start && (End[-1] == tok || isspace(End[-1]) != 0);
81 list[Count++] = Start;
82 if (Count >= listmax) {
88 Pos != Stop && (*Pos == tok || isspace(*Pos) != 0
89 || *Pos == 0); Pos++) ;
98 void _d_msg_init(char *program)
104 char logfilename[64];
105 char buffer[256] = { 0 };
106 snprintf(logfilename, 64, "%s-%s", NATIVE_INSTALLER_LOG_FILE, program);
107 logfile = fopen(logfilename, "a+");
110 printf("Error opening log file\n");
112 snprintf(buffer, 64, "\nLog File %s Created", logfilename);
113 fwrite(buffer, sizeof(char), strlen(buffer), logfile);
114 snprintf(buffer, 64, "\nLog Started\n");
115 fwrite(buffer, sizeof(char), strlen(buffer), logfile);
131 void _print_msg(int type, int exetype, char *format, ...)
133 char buffer[1024] = { 0 };
134 char tbuffer[1024] = { 0 };
137 va_start(args, format);
138 nbuffer = vsnprintf(tbuffer, 1024, format, args);
143 LOG(LOG_ERROR, LOG_TAG, tbuffer);
146 LOG(LOG_WARN, LOG_TAG, tbuffer);
149 LOG(LOG_DEBUG, LOG_TAG, tbuffer);
157 if (DEBUG_ERR == (logging & type)) {
158 nbuffer = snprintf(buffer, 1024, "ERROR:%s", tbuffer);
159 vfprintf(stderr, format, args);
160 } else if (DEBUG_INFO == (logging & type)) {
161 nbuffer = snprintf(buffer, 1024, "INFO:%s", tbuffer);
162 vfprintf(stdout, format, args);
163 } else if (DEBUG_RESULT == (logging & type)) {
164 nbuffer = snprintf(buffer, 1024, "RESULT:%s", tbuffer);
165 vfprintf(stdout, format, args);
172 fwrite(buffer, sizeof(char), strlen(buffer), logfile);
173 #endif /*LOG_IN_FILE */
176 /* Like system(3), but with error messages printed if the fork fails
177 or if the child process dies due to an uncaught signal. Also, the
178 return value is a bit simpler:
180 -1 if there was any problem
181 Otherwise, the 8-bit return value of the program ala WEXITSTATUS
182 as defined in <sys/wait.h>.
184 int _xsystem(const char *argv[])
191 perror("fork failed");
195 execvp(argv[0], (char *const *)argv);
202 d_msg_backend(DEBUG_INFO, "parent\n");
203 if (waitpid(pid, &status, 0) == -1) {
204 perror("waitpid failed");
208 if (WIFSIGNALED(status)) {
213 if (!WIFEXITED(status)) {
214 /* shouldn't happen */
215 perror("should not happen");
219 return WEXITSTATUS(status);
222 char *_substring(const char *str, size_t begin, size_t len)
224 if (str == 0 || strlen(str) == 0 || strlen(str) < begin
225 || strlen(str) < (begin + len))
227 return strndup(str + begin, len);
230 void _error_no_to_string(int errnumber, char **errstr)
235 case NATIVEINSTALLER_SUCCESS:
236 *errstr = NATIVEINSTALLER_SUCCESS_STR;
238 case NATIVEINSTALLER_ERR_WRONG_PARAM:
239 *errstr = NATIVEINSTALLER_ERR_WRONG_PARAM_STR;
241 case NATIVEINSTALLER_ERR_DBUS_PROBLEM:
242 *errstr = NATIVEINSTALLER_ERR_DBUS_PROBLEM_STR;
244 case NATIVEINSTALLER_ERR_NOT_ENOUGH_MEMORY:
245 *errstr = NATIVEINSTALLER_ERR_NOT_ENOUGH_MEMORY_STR;
247 case NATIVEINSTALLER_ERR_NO_MANIFEST:
248 *errstr = NATIVEINSTALLER_ERR_NO_MANIFEST_STR;
250 case NATIVEINSTALLER_ERR_PACKAGE_EXIST:
251 *errstr = NATIVEINSTALLER_ERR_PACKAGE_EXIST_STR;
253 case NATIVEINSTALLER_ERR_PACKAGE_NOT_INSTALLED:
254 *errstr = NATIVEINSTALLER_ERR_PACKAGE_NOT_INSTALLED_STR;
256 case NATIVEINSTALLER_ERR_RESOURCE_BUSY:
257 *errstr = NATIVEINSTALLER_ERR_RESOURCE_BUSY_STR;
259 case NATIVEINSTALLER_ERR_UNKNOWN:
260 *errstr = NATIVEINSTALLER_ERR_UNKNOWN_STR;
262 case NATIVEINSTALLER_ERR_PKG_NOT_FOUND:
263 *errstr = NATIVEINSTALLER_ERR_PKG_NOT_FOUND_STR;
265 case NATIVEINSTALLER_ERR_NOT_SUPPOTED_VERSION:
266 *errstr = NATIVEINSTALLER_ERR_NOT_SUPPOTED_VERSION_STR;
268 case NATIVEINSTALLER_ERR_NO_DEB_FILE:
269 *errstr = NATIVEINSTALLER_ERR_NO_DEB_FILE_STR;
271 case NATIVEINSTALLER_ERR_DB_ACCESS_FAILED:
272 *errstr = NATIVEINSTALLER_ERR_DB_ACCESS_FAILED_STR;
274 case NATIVEINSTALLER_ERR_DPKG_OPERATION_FAILED:
275 *errstr = NATIVEINSTALLER_ERR_DPKG_OPERATION_FAILED_STR;
277 case NATIVEINSTALLER_PACKAGE_NOT_UPGRADED:
278 *errstr = NATIVEINSTALLER_PACKAGE_NOT_UPGRADED_STR;
280 case NATIVEINSTALLER_ERR_CLEAR_DATA_FAILED:
281 *errstr = NATIVEINSTALLER_ERR_CLEAR_DATA_FAILED_STR;
284 *errstr = NATIVEINSTALLER_ERR_UNKNOWN_STR;
289 int _string_to_error_no(char *errstr)
291 int errnumber = NATIVEINSTALLER_ERR_UNKNOWN;
295 if (strcmp(errstr, NATIVEINSTALLER_SUCCESS_STR) == 0)
296 errnumber = NATIVEINSTALLER_SUCCESS;
297 else if (strcmp(errstr, NATIVEINSTALLER_ERR_WRONG_PARAM_STR) == 0)
298 errnumber = NATIVEINSTALLER_ERR_WRONG_PARAM;
299 else if (strcmp(errstr, NATIVEINSTALLER_ERR_DBUS_PROBLEM_STR) == 0)
300 errnumber = NATIVEINSTALLER_ERR_DBUS_PROBLEM;
301 else if (strcmp(errstr, NATIVEINSTALLER_ERR_NOT_ENOUGH_MEMORY_STR) == 0)
302 errnumber = NATIVEINSTALLER_ERR_NOT_ENOUGH_MEMORY;
303 else if (strcmp(errstr, NATIVEINSTALLER_ERR_NO_MANIFEST_STR) == 0)
304 errnumber = NATIVEINSTALLER_ERR_NO_MANIFEST;
305 else if (strcmp(errstr, NATIVEINSTALLER_ERR_PACKAGE_EXIST_STR) == 0)
306 errnumber = NATIVEINSTALLER_ERR_PACKAGE_EXIST;
307 else if (strcmp(errstr, NATIVEINSTALLER_ERR_PACKAGE_NOT_INSTALLED_STR)
309 errnumber = NATIVEINSTALLER_ERR_PACKAGE_NOT_INSTALLED;
310 else if (strcmp(errstr, NATIVEINSTALLER_ERR_RESOURCE_BUSY_STR) == 0)
311 errnumber = NATIVEINSTALLER_ERR_RESOURCE_BUSY;
312 else if (strcmp(errstr, NATIVEINSTALLER_ERR_UNKNOWN_STR) == 0)
313 errnumber = NATIVEINSTALLER_ERR_UNKNOWN;
314 else if (strcmp(errstr, NATIVEINSTALLER_ERR_PKG_NOT_FOUND_STR) == 0)
315 errnumber = NATIVEINSTALLER_ERR_PKG_NOT_FOUND;
316 else if (strcmp(errstr, NATIVEINSTALLER_ERR_NOT_SUPPOTED_VERSION_STR) ==
318 errnumber = NATIVEINSTALLER_ERR_NOT_SUPPOTED_VERSION;
319 else if (strcmp(errstr, NATIVEINSTALLER_ERR_NO_DEB_FILE_STR) == 0)
320 errnumber = NATIVEINSTALLER_ERR_NO_DEB_FILE;
321 else if (strcmp(errstr, NATIVEINSTALLER_ERR_DB_ACCESS_FAILED_STR) == 0)
322 errnumber = NATIVEINSTALLER_ERR_DB_ACCESS_FAILED;
323 else if (strcmp(errstr, NATIVEINSTALLER_ERR_DPKG_OPERATION_FAILED_STR)
325 errnumber = NATIVEINSTALLER_ERR_DPKG_OPERATION_FAILED;
326 else if (strcmp(errstr, NATIVEINSTALLER_PACKAGE_NOT_UPGRADED_STR) == 0)
327 errnumber = NATIVEINSTALLER_PACKAGE_NOT_UPGRADED;
328 else if (strcmp(errstr, NATIVEINSTALLER_ERR_CLEAR_DATA_FAILED_STR) == 0)
329 errnumber = NATIVEINSTALLER_ERR_CLEAR_DATA_FAILED;