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.
26 #include <sys/types.h>
30 #include <ctype.h> /* for isspace () */
33 #include <sys/types.h>
38 #include "rpm-installer-util.h"
42 #define LOG_TAG "rpminstaller"
46 #define RPM_INSTALLER_LOG_FILE "/tmp/rpm-installer"
51 * This is intended to be a faster splitter, it does not use dynamic
52 * memories. Input is changed to insert nulls at each token location.
54 int _ri_tok_split_string(char tok, char *input, char **list,
55 unsigned long listmax)
57 /* Strip any leading spaces */
59 char *stop = start + strlen(start);
60 for (; *start != 0 && isspace(*start) != 0; start++) ;
62 unsigned long count = 0;
65 /* Skip to the next Token */
66 for (; pos != stop && *pos != tok; pos++) ;
68 /* Back remove spaces */
71 end > start && (end[-1] == tok || isspace(end[-1]) != 0);
76 list[count++] = start;
77 if (count >= listmax) {
83 pos != stop && (*pos == tok || isspace(*pos) != 0
84 || *pos == 0); pos++) ;
93 void _d_msg_init(char *program)
99 char logfilename[64] = { 0, };
100 char buffer[256] = { 0 };
101 snprintf(logfilename, 64, "%s-%s", RPM_INSTALLER_LOG_FILE, program);
102 logfile = fopen(logfilename, "a+");
105 printf("Error opening log file\n");
107 snprintf(buffer, 64, "\nLog File %s Created", logfilename);
108 fwrite(buffer, sizeof(char), strlen(buffer), logfile);
109 snprintf(buffer, 64, "\nLog Started\n");
110 fwrite(buffer, sizeof(char), strlen(buffer), logfile);
126 void _print_msg(int type, int exetype, char *format, ...)
128 char buffer[1024] = { 0 };
129 char tbuffer[1024] = { 0 };
132 va_start(args, format);
133 nbuffer = vsnprintf(tbuffer, 1024, format, args);
138 LOG(LOG_ERROR, LOG_TAG, tbuffer);
141 LOG(LOG_WARN, LOG_TAG, tbuffer);
144 LOG(LOG_DEBUG, LOG_TAG, tbuffer);
152 if (DEBUG_ERR == (logging & type)) {
153 nbuffer = snprintf(buffer, 1024, "ERROR:%s", tbuffer);
154 vfprintf(stderr, format, args);
155 } else if (DEBUG_INFO == (logging & type)) {
156 nbuffer = snprintf(buffer, 1024, "INFO:%s", tbuffer);
157 vfprintf(stdout, format, args);
158 } else if (DEBUG_RESULT == (logging & type)) {
159 nbuffer = snprintf(buffer, 1024, "RESULT:%s", tbuffer);
160 vfprintf(stdout, format, args);
167 fwrite(buffer, sizeof(char), strlen(buffer), logfile);
168 #endif /*LOG_IN_FILE */
171 /* Like system(3), but with error messages printed if the fork fails
172 or if the child process dies due to an uncaught signal. Also, the
173 return value is a bit simpler:
175 -1 if there was any problem
176 Otherwise, the 8-bit return value of the program ala WEXITSTATUS
177 as defined in <sys/wait.h>.
179 int _ri_xsystem(const char *argv[])
186 perror("fork failed");
190 execvp(argv[0], (char *const *)argv);
197 _d_msg(DEBUG_INFO, "parent\n");
198 if (waitpid(pid, &status, 0) == -1) {
199 perror("waitpid failed");
203 if (WIFSIGNALED(status)) {
208 if (!WIFEXITED(status)) {
209 /* shouldn't happen */
210 perror("should not happen");
214 return WEXITSTATUS(status);
217 char *_ri_substring(const char *str, size_t begin, size_t len)
219 if (str == 0 || strlen(str) == 0 || strlen(str) < (begin + len))
221 return strndup(str + begin, len);
224 void _ri_error_no_to_string(int errnumber, char **errstr)
229 case RPM_INSTALLER_SUCCESS:
230 *errstr = RPM_INSTALLER_SUCCESS_STR;
232 case RPM_INSTALLER_ERR_WRONG_PARAM:
233 *errstr = RPM_INSTALLER_ERR_WRONG_PARAM_STR;
235 case RPM_INSTALLER_ERR_DBUS_PROBLEM:
236 *errstr = RPM_INSTALLER_ERR_DBUS_PROBLEM_STR;
238 case RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY:
239 *errstr = RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY_STR;
241 case RPM_INSTALLER_ERR_PACKAGE_EXIST:
242 *errstr = RPM_INSTALLER_ERR_PACKAGE_EXIST_STR;
244 case RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED:
245 *errstr = RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED_STR;
247 case RPM_INSTALLER_ERR_RESOURCE_BUSY:
248 *errstr = RPM_INSTALLER_ERR_RESOURCE_BUSY_STR;
250 case RPM_INSTALLER_ERR_UNKNOWN:
251 *errstr = RPM_INSTALLER_ERR_UNKNOWN_STR;
253 case RPM_INSTALLER_ERR_PKG_NOT_FOUND:
254 *errstr = RPM_INSTALLER_ERR_PKG_NOT_FOUND_STR;
256 case RPM_INSTALLER_ERR_NOT_SUPPOTED_VERSION:
257 *errstr = RPM_INSTALLER_ERR_NOT_SUPPOTED_VERSION_STR;
259 case RPM_INSTALLER_ERR_NO_RPM_FILE:
260 *errstr = RPM_INSTALLER_ERR_NO_RPM_FILE_STR;
262 case RPM_INSTALLER_ERR_DB_ACCESS_FAILED:
263 *errstr = RPM_INSTALLER_ERR_DB_ACCESS_FAILED_STR;
265 case RPM_INSTALLER_ERR_RPM_OPERATION_FAILED:
266 *errstr = RPM_INSTALLER_ERR_RPM_OPERATION_FAILED_STR;
268 case RPM_INSTALLER_ERR_PACKAGE_NOT_UPGRADED:
269 *errstr = RPM_INSTALLER_ERR_PACKAGE_NOT_UPGRADED_STR;
271 case RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS:
272 *errstr = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS_STR;
274 case RPM_INSTALLER_ERR_PACKAGE_INSTALLATION_DISABLED:
275 *errstr = RPM_INSTALLER_ERR_PACKAGE_INSTALLATION_DISABLED_STR;
277 case RPM_INSTALLER_ERR_PACKAGE_UNINSTALLATION_DISABLED:
278 *errstr = RPM_INSTALLER_ERR_PACKAGE_UNINSTALLATION_DISABLED_STR;
280 case RPM_INSTALLER_ERR_CLEAR_DATA_FAILED:
281 *errstr = RPM_INSTALLER_ERR_CLEAR_DATA_FAILED_STR;
283 case RPM_INSTALLER_ERR_INTERNAL:
284 *errstr = RPM_INSTALLER_ERR_INTERNAL_STR;
286 case RPM_INSTALLER_ERR_NO_MANIFEST:
287 *errstr = RPM_INSTALLER_ERR_NO_MANIFEST_STR;
289 case RPM_INSTALLER_ERR_INVALID_MANIFEST:
290 *errstr = RPM_INSTALLER_ERR_INVALID_MANIFEST_STR;
293 *errstr = RPM_INSTALLER_ERR_UNKNOWN_STR;
298 int _ri_string_to_error_no(char *errstr)
300 int errnumber = RPM_INSTALLER_ERR_UNKNOWN;
304 if (strcmp(errstr, RPM_INSTALLER_SUCCESS_STR) == 0)
305 errnumber = RPM_INSTALLER_SUCCESS;
306 else if (strcmp(errstr, RPM_INSTALLER_ERR_WRONG_PARAM_STR) == 0)
307 errnumber = RPM_INSTALLER_ERR_WRONG_PARAM;
308 else if (strcmp(errstr, RPM_INSTALLER_ERR_DBUS_PROBLEM_STR) == 0)
309 errnumber = RPM_INSTALLER_ERR_DBUS_PROBLEM;
310 else if (strcmp(errstr, RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY_STR) == 0)
311 errnumber = RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY;
312 else if (strcmp(errstr, RPM_INSTALLER_ERR_PACKAGE_EXIST_STR) == 0)
313 errnumber = RPM_INSTALLER_ERR_PACKAGE_EXIST;
314 else if (strcmp(errstr, RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED_STR)
316 errnumber = RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED;
317 else if (strcmp(errstr, RPM_INSTALLER_ERR_RESOURCE_BUSY_STR) == 0)
318 errnumber = RPM_INSTALLER_ERR_RESOURCE_BUSY;
319 else if (strcmp(errstr, RPM_INSTALLER_ERR_UNKNOWN_STR) == 0)
320 errnumber = RPM_INSTALLER_ERR_UNKNOWN;
321 else if (strcmp(errstr, RPM_INSTALLER_ERR_PKG_NOT_FOUND_STR) == 0)
322 errnumber = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
323 else if (strcmp(errstr, RPM_INSTALLER_ERR_NOT_SUPPOTED_VERSION_STR) ==
325 errnumber = RPM_INSTALLER_ERR_NOT_SUPPOTED_VERSION;
326 else if (strcmp(errstr, RPM_INSTALLER_ERR_NO_RPM_FILE_STR) == 0)
327 errnumber = RPM_INSTALLER_ERR_NO_RPM_FILE;
328 else if (strcmp(errstr, RPM_INSTALLER_ERR_DB_ACCESS_FAILED_STR) == 0)
329 errnumber = RPM_INSTALLER_ERR_DB_ACCESS_FAILED;
330 else if (strcmp(errstr, RPM_INSTALLER_ERR_RPM_OPERATION_FAILED_STR)
332 errnumber = RPM_INSTALLER_ERR_RPM_OPERATION_FAILED;
333 else if (strcmp(errstr, RPM_INSTALLER_ERR_PACKAGE_NOT_UPGRADED_STR) ==
335 errnumber = RPM_INSTALLER_ERR_PACKAGE_NOT_UPGRADED;
336 else if (strcmp(errstr, RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS_STR) ==
338 errnumber = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
339 else if (strcmp(errstr, RPM_INSTALLER_ERR_PACKAGE_INSTALLATION_DISABLED_STR) == 0)
340 errnumber = RPM_INSTALLER_ERR_PACKAGE_INSTALLATION_DISABLED;
341 else if (strcmp(errstr, RPM_INSTALLER_ERR_PACKAGE_UNINSTALLATION_DISABLED_STR) == 0)
342 errnumber = RPM_INSTALLER_ERR_PACKAGE_UNINSTALLATION_DISABLED;
343 else if (strcmp(errstr, RPM_INSTALLER_ERR_CLEAR_DATA_FAILED_STR) == 0)
344 errnumber = RPM_INSTALLER_ERR_CLEAR_DATA_FAILED;
345 else if (strcmp(errstr, RPM_INSTALLER_ERR_INTERNAL_STR) == 0)
346 errnumber = RPM_INSTALLER_ERR_INTERNAL;
347 else if (strcmp(errstr, RPM_INSTALLER_ERR_NO_MANIFEST_STR) == 0)
348 errnumber = RPM_INSTALLER_ERR_NO_MANIFEST;
349 else if (strcmp(errstr, RPM_INSTALLER_ERR_INVALID_MANIFEST_STR) == 0)
350 errnumber = RPM_INSTALLER_ERR_INVALID_MANIFEST;
352 _d_msg(DEBUG_ERR, "Unsupported Error\n");