From 17eca54b321e4eed63cd27bfa3dbf960b0c96901 Mon Sep 17 00:00:00 2001 From: Nagy Gabor Date: Sat, 1 Mar 2008 14:01:40 +0100 Subject: testpkg rework * mainly code cosmetics (indent fixes) * remove debug message "spam" * print also user friendly result Signed-off-by: Nagy Gabor [Dan: a few more whitespace/linebreak cleanups added] Signed-off-by: Dan McGee --- src/util/testpkg.c | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/util/testpkg.c b/src/util/testpkg.c index e32b013d..64056ce4 100644 --- a/src/util/testpkg.c +++ b/src/util/testpkg.c @@ -29,44 +29,58 @@ static void output_cb(pmloglevel_t level, char *fmt, va_list args) { - if(strlen(fmt)) { - switch(level) { - case PM_LOG_ERROR: printf("error: "); break; - case PM_LOG_WARNING: printf("warning: "); break; - default: break; - } - vprintf(fmt, args); - } + if(fmt[0] == '\0') { + return; + } + switch(level) { + case PM_LOG_ERROR: printf("error: "); break; + case PM_LOG_WARNING: printf("warning: "); break; + default: return; /* skip other messages */ + } + vprintf(fmt, args); } int main(int argc, char **argv) { - int retval = 1; /* default = false */ - pmpkg_t *pkg = NULL; + int retval = 1; /* default = false */ + pmpkg_t *pkg = NULL; - if(argc != 2) { + if(argc != 2) { fprintf(stderr, "usage: %s \n", BASENAME); return(1); } if(alpm_initialize() == -1) { - fprintf(stderr, "cannot initilize alpm: %s\n", alpm_strerrorlast()); - return(1); + fprintf(stderr, "cannot initialize alpm: %s\n", alpm_strerrorlast()); + return(1); } - /* let us get log messages from libalpm */ + /* let us get log messages from libalpm */ alpm_option_set_logcb(output_cb); if(alpm_pkg_load(argv[1], 1, &pkg) == -1 || pkg == NULL) { - retval = 1; + switch(pm_errno) { + case PM_ERR_PKG_OPEN: + printf("Cannot open the given file.\n"); + break; + case PM_ERR_LIBARCHIVE_ERROR: + case PM_ERR_PKG_INVALID: + printf("Package is invalid.\n"); + break; + default: + printf("libalpm error: %s\n", alpm_strerrorlast()); + break; + } + retval = 1; } else { alpm_pkg_free(pkg); - retval = 0; + printf("Package is valid.\n"); + retval = 0; } if(alpm_release() == -1) { fprintf(stderr, "error releasing alpm: %s\n", alpm_strerrorlast()); } - return(retval); + return(retval); } -- cgit v1.2.3 From 54af52f87d6d9547629bb336c365943ff9bb4876 Mon Sep 17 00:00:00 2001 From: Nagy Gabor Date: Fri, 29 Feb 2008 21:52:57 +0100 Subject: New alpm_version function Now pacman frontend uses this function instead of the compile-time libalpm version number. Signed-off-by: Nagy Gabor [Dan: fix one more spot where LIB_VERSION was used] Signed-off-by: Dan McGee (cherry picked from commit 49197b7492d61bf1fc6bef59a708f4f586f32edb) --- src/pacman/pacman.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 377ea3fe..d6997689 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -150,7 +150,7 @@ static void usage(int op, const char * const myname) static void version(void) { printf("\n"); - printf(" .--. Pacman v%s - libalpm v%s\n", PACKAGE_VERSION, LIB_VERSION); + printf(" .--. Pacman v%s - libalpm v%s\n", PACKAGE_VERSION, alpm_version()); printf("/ _.-' .-. .-. .-. Copyright (C) 2002-2008 Judd Vinet \n"); printf("\\ '-. '-' '-' '-'\n"); printf(" '--'\n"); @@ -183,8 +183,8 @@ static void setuseragent(void) struct utsname un; uname(&un); - snprintf(agent, 100, "pacman/" PACKAGE_VERSION " (%s %s) libalpm/" LIB_VERSION, - un.sysname, un.machine); + snprintf(agent, 100, "pacman/%s (%s %s) libalpm/%s", + PACKAGE_VERSION, un.sysname, un.machine, alpm_version()); setenv("HTTP_USER_AGENT", agent, 0); } -- cgit v1.2.3 From 2f9f48edddadffa2ac39ed7291f92be7be3b0fb5 Mon Sep 17 00:00:00 2001 From: Chantry Xavier Date: Sun, 9 Mar 2008 12:56:00 +0100 Subject: src/pacman/pacman.c : split cleanup function. This function was used in two different ways : - as a signal handler : the argument was the signal number - called manually for freeing the resources : the argument was the return value So the first part is now handler(int), and the second cleanup(int). Ref: http://www.archlinux.org/pipermail/pacman-dev/2008-March/011388.html Remaining problems : - the return values are messy. for example, 2 can mean both that it was interrupted (SIGINT == 2), or that --help or -V was used (returned by parseargs). - apparently signal is not portable and sigaction should be used instead Signed-off-by: Chantry Xavier --- src/pacman/pacman.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index d6997689..f46b71c1 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -188,11 +188,31 @@ static void setuseragent(void) setenv("HTTP_USER_AGENT", agent, 0); } +/** Free the resources. + * + * @param ret the return value + */ +static void cleanup(int ret) { + /* free alpm library resources */ + if(alpm_release() == -1) { + pm_printf(PM_LOG_ERROR, alpm_strerrorlast()); + } + + /* free memory */ + FREELIST(pm_targets); + if(config) { + config_free(config); + config = NULL; + } + + exit(ret); +} + /** Catches thrown signals. Performs necessary cleanup to ensure database is * in a consistant state. * @param signum the thrown signal */ -static void cleanup(int signum) +static void handler(int signum) { if(signum==SIGSEGV) { @@ -211,20 +231,7 @@ static void cleanup(int signum) /* output a newline to be sure we clear any line we may be on */ printf("\n"); } - - /* free alpm library resources */ - if(alpm_release() == -1) { - pm_printf(PM_LOG_ERROR, alpm_strerrorlast()); - } - - /* free memory */ - FREELIST(pm_targets); - if(config) { - config_free(config); - config = NULL; - } - - exit(signum); + cleanup(signum); } /** Sets all libalpm required paths in one go. Called after the command line @@ -756,9 +763,9 @@ int main(int argc, char *argv[]) #endif /* set signal handlers */ - signal(SIGINT, cleanup); - signal(SIGTERM, cleanup); - signal(SIGSEGV, cleanup); + signal(SIGINT, handler); + signal(SIGTERM, handler); + signal(SIGSEGV, handler); /* i18n init */ #if defined(ENABLE_NLS) -- cgit v1.2.3 From 51e0303e840c94e5943f30e311d053058f657327 Mon Sep 17 00:00:00 2001 From: Chantry Xavier Date: Sun, 9 Mar 2008 13:12:32 +0100 Subject: Use sigaction instead of signal. From signal man page : "The behavior of signal() varies across Unix versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead. See Portability below." The code was taken from there : http://www.gnu.org/software/libtool/manual/libc/Sigaction-Function-Example.html Signed-off-by: Chantry Xavier Signed-off-by: Dan McGee --- src/pacman/pacman.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index f46b71c1..c2b61fcd 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -752,6 +752,7 @@ static int parseconfig(const char *file) int main(int argc, char *argv[]) { int ret = 0; + struct sigaction new_action, old_action; #if defined(HAVE_GETEUID) /* geteuid undefined in CYGWIN */ uid_t myuid = geteuid(); @@ -762,10 +763,24 @@ int main(int argc, char *argv[]) mtrace(); #endif - /* set signal handlers */ - signal(SIGINT, handler); - signal(SIGTERM, handler); - signal(SIGSEGV, handler); + /* Set signal handlers */ + /* Set up the structure to specify the new action. */ + new_action.sa_handler = handler; + sigemptyset(&new_action.sa_mask); + new_action.sa_flags = 0; + + sigaction(SIGINT, NULL, &old_action); + if(old_action.sa_handler != SIG_IGN) { + sigaction(SIGINT, &new_action, NULL); + } + sigaction(SIGTERM, NULL, &old_action); + if(old_action.sa_handler != SIG_IGN) { + sigaction(SIGTERM, &new_action, NULL); + } + sigaction(SIGSEGV, NULL, &old_action); + if(old_action.sa_handler != SIG_IGN) { + sigaction(SIGSEGV, &new_action, NULL); + } /* i18n init */ #if defined(ENABLE_NLS) -- cgit v1.2.3