2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2009-2013 Juan Romero Pardines.
|
2012-11-02 19:34:25 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-12-14 08:01:24 +05:30
|
|
|
#include <sys/param.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2012-12-14 08:01:24 +05:30
|
|
|
#include <pthread.h>
|
|
|
|
#include <assert.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include "defs.h"
|
|
|
|
|
2012-12-14 08:01:24 +05:30
|
|
|
struct thread_data {
|
|
|
|
pthread_t thread;
|
|
|
|
struct xbps_handle *xhp;
|
|
|
|
unsigned int start;
|
|
|
|
unsigned int end;
|
|
|
|
int thread_num;
|
2012-11-02 19:34:25 +05:30
|
|
|
};
|
|
|
|
|
2012-12-14 08:01:24 +05:30
|
|
|
static void *
|
|
|
|
pkgdb_thread_worker(void *arg)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkgd;
|
|
|
|
xbps_array_t array;
|
|
|
|
xbps_object_t obj;
|
2012-12-14 08:01:24 +05:30
|
|
|
struct thread_data *thd = arg;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver;
|
|
|
|
char *pkgname;
|
2012-12-14 08:01:24 +05:30
|
|
|
unsigned int i;
|
|
|
|
int rv;
|
2012-11-17 22:13:54 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
array = xbps_dictionary_all_keys(thd->xhp->pkgdb);
|
2013-03-05 08:38:42 +05:30
|
|
|
assert(array);
|
|
|
|
|
2012-12-14 08:01:24 +05:30
|
|
|
/* process pkgs from start until end */
|
|
|
|
for (i = thd->start; i < thd->end; i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
obj = xbps_array_get(array, i);
|
|
|
|
pkgd = xbps_dictionary_get_keysym(thd->xhp->pkgdb, obj);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
|
2012-12-14 08:01:24 +05:30
|
|
|
if (thd->xhp->flags & XBPS_FLAG_VERBOSE)
|
|
|
|
printf("Checking %s ...\n", pkgver);
|
2012-11-17 22:13:54 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgname = xbps_pkg_name(pkgver);
|
|
|
|
assert(pkgname);
|
2012-12-14 08:01:24 +05:30
|
|
|
rv = check_pkg_integrity(thd->xhp, pkgd, pkgname);
|
2013-03-05 08:38:42 +05:30
|
|
|
free(pkgname);
|
2012-12-14 08:01:24 +05:30
|
|
|
if (rv != 0)
|
|
|
|
fprintf(stderr, "pkgdb[%d] failed for %s: %s\n",
|
|
|
|
thd->thread_num, pkgver, strerror(rv));
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(array);
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2012-12-14 08:01:24 +05:30
|
|
|
return NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
check_pkg_integrity_all(struct xbps_handle *xhp)
|
|
|
|
{
|
2012-12-14 08:01:24 +05:30
|
|
|
struct thread_data *thd;
|
|
|
|
unsigned int slicecount, pkgcount;
|
|
|
|
int rv, maxthreads, i;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
/* force an update to get total pkg count */
|
|
|
|
(void)xbps_pkgdb_update(xhp, false);
|
|
|
|
|
2012-12-14 08:01:24 +05:30
|
|
|
maxthreads = (int)sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
thd = calloc(maxthreads, sizeof(*thd));
|
|
|
|
assert(thd);
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
slicecount = xbps_dictionary_count(xhp->pkgdb) / maxthreads;
|
2012-12-14 08:01:24 +05:30
|
|
|
pkgcount = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < maxthreads; i++) {
|
|
|
|
thd[i].thread_num = i;
|
|
|
|
thd[i].xhp = xhp;
|
|
|
|
thd[i].start = pkgcount;
|
|
|
|
if (i + 1 >= maxthreads)
|
2013-06-20 13:56:12 +05:30
|
|
|
thd[i].end = xbps_dictionary_count(xhp->pkgdb);
|
2012-12-14 08:01:24 +05:30
|
|
|
else
|
|
|
|
thd[i].end = pkgcount + slicecount;
|
|
|
|
pthread_create(&thd[i].thread, NULL,
|
|
|
|
pkgdb_thread_worker, &thd[i]);
|
|
|
|
pkgcount += slicecount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for all threads */
|
|
|
|
for (i = 0; i < maxthreads; i++)
|
|
|
|
pthread_join(thd[i].thread, NULL);
|
|
|
|
|
2012-11-17 22:13:54 +05:30
|
|
|
if ((rv = xbps_pkgdb_update(xhp, true)) != 0) {
|
|
|
|
xbps_error_printf("failed to write pkgdb: %s\n",
|
|
|
|
strerror(rv));
|
|
|
|
return rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
check_pkg_integrity(struct xbps_handle *xhp,
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkgd,
|
2012-11-17 22:13:54 +05:30
|
|
|
const char *pkgname)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t opkgd, propsd;
|
2012-11-16 21:25:35 +05:30
|
|
|
const char *sha256;
|
|
|
|
char *buf;
|
2012-11-02 19:34:25 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2012-11-16 21:25:35 +05:30
|
|
|
propsd = opkgd = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
/* find real pkg by name */
|
2012-11-16 21:25:35 +05:30
|
|
|
opkgd = pkgd;
|
2012-11-30 22:10:52 +05:30
|
|
|
if (opkgd == NULL) {
|
|
|
|
if (((opkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) &&
|
|
|
|
((opkgd = xbps_pkgdb_get_virtualpkg(xhp, pkgname)) == NULL)) {
|
2012-11-02 19:34:25 +05:30
|
|
|
printf("Package %s is not installed.\n", pkgname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check for props.plist metadata file.
|
|
|
|
*/
|
2012-12-14 10:34:58 +05:30
|
|
|
buf = xbps_xasprintf("%s/.%s.plist", xhp->metadir, pkgname);
|
2013-06-20 13:56:12 +05:30
|
|
|
propsd = xbps_dictionary_internalize_from_file(buf);
|
2012-12-14 10:34:58 +05:30
|
|
|
free(buf);
|
2012-11-02 19:34:25 +05:30
|
|
|
if (propsd == NULL) {
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_error_printf("%s: unexistent metafile!\n", pkgname);
|
|
|
|
return EINVAL;
|
2013-06-20 13:56:12 +05:30
|
|
|
} else if (xbps_dictionary_count(propsd) == 0) {
|
2012-11-16 21:25:35 +05:30
|
|
|
xbps_error_printf("%s: incomplete metadata file.\n", pkgname);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(propsd);
|
2012-11-17 22:13:54 +05:30
|
|
|
return 1;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2012-11-30 22:10:52 +05:30
|
|
|
/*
|
|
|
|
* Check pkg metadata signature.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(opkgd, "metafile-sha256", &sha256);
|
2012-11-16 21:25:35 +05:30
|
|
|
if (sha256 != NULL) {
|
|
|
|
buf = xbps_xasprintf("%s/.%s.plist",
|
|
|
|
xhp->metadir, pkgname);
|
|
|
|
rv = xbps_file_hash_check(buf, sha256);
|
|
|
|
free(buf);
|
|
|
|
if (rv == ERANGE) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(propsd);
|
2012-11-16 21:25:35 +05:30
|
|
|
fprintf(stderr, "%s: metadata file has been "
|
|
|
|
"modified!\n", pkgname);
|
2012-11-17 22:13:54 +05:30
|
|
|
return 1;
|
2012-11-16 21:25:35 +05:30
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
2012-11-17 22:13:54 +05:30
|
|
|
#define RUN_PKG_CHECK(x, name, arg) \
|
2012-11-02 19:34:25 +05:30
|
|
|
do { \
|
2012-11-17 22:13:54 +05:30
|
|
|
rv = check_pkg_##name(x, pkgname, arg); \
|
2012-11-20 01:42:04 +05:30
|
|
|
if (rv == -1) { \
|
2012-11-02 19:34:25 +05:30
|
|
|
xbps_error_printf("%s: the %s test " \
|
|
|
|
"returned error!\n", pkgname, #name); \
|
2012-11-17 22:13:54 +05:30
|
|
|
return rv; \
|
2012-11-02 19:34:25 +05:30
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Execute pkg checks */
|
2012-11-17 22:13:54 +05:30
|
|
|
RUN_PKG_CHECK(xhp, files, propsd);
|
|
|
|
RUN_PKG_CHECK(xhp, symlinks, propsd);
|
|
|
|
RUN_PKG_CHECK(xhp, rundeps, propsd);
|
|
|
|
RUN_PKG_CHECK(xhp, unneeded, opkgd);
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_release(propsd);
|
2012-12-14 10:34:58 +05:30
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
#undef RUN_PKG_CHECK
|
|
|
|
|
2012-11-20 01:42:04 +05:30
|
|
|
if ((rv == 0) && (pkgd == NULL))
|
2012-11-17 22:55:18 +05:30
|
|
|
(void)xbps_pkgdb_update(xhp, true);
|
|
|
|
|
2012-11-17 22:13:54 +05:30
|
|
|
return 0;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|