/ libpkg / pkg_jobs.c
pkg_jobs.c
   1  /*-
   2   * Copyright (c) 2011-2026 Baptiste Daroussin <bapt@FreeBSD.org>
   3   * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
   4   * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
   5   * Copyright (c) 2013 Matthew Seaman <matthew@FreeBSD.org>
   6   * Copyright (c) 2013-2016 Vsevolod Stakhov <vsevolod@FreeBSD.org>
   7   * Copyright (c) 2023 Serenity Cyber Security, LLC
   8   *                    Author: Gleb Popov <arrowd@FreeBSD.org>
   9   *
  10   * Redistribution and use in source and binary forms, with or without
  11   * modification, are permitted provided that the following conditions
  12   * are met:
  13   * 1. Redistributions of source code must retain the above copyright
  14   *    notice, this list of conditions and the following disclaimer
  15   *    in this position and unchanged.
  16   * 2. Redistributions in binary form must reproduce the above copyright
  17   *    notice, this list of conditions and the following disclaimer in the
  18   *    documentation and/or other materials provided with the distribution.
  19   *
  20   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  21   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23   * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  24   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30   */
  31  
  32  #define dbg(x, ...) pkg_dbg(PKG_DBG_JOBS, x, __VA_ARGS__)
  33  
  34  #include <bsd_compat.h>
  35  
  36  #include <sys/param.h>
  37  #include <sys/mount.h>
  38  #if __has_include(<sys/sysctl.h>)
  39  #include <sys/sysctl.h>
  40  #endif
  41  #include <sys/types.h>
  42  #include <sys/statvfs.h>
  43  
  44  #include <archive.h>
  45  #include <archive_entry.h>
  46  #include <assert.h>
  47  #include <errno.h>
  48  #if __has_include(<libutil.h>)
  49  #include <libutil.h>
  50  #endif
  51  #include <search.h>
  52  #include <stdbool.h>
  53  #include <stdlib.h>
  54  #include <string.h>
  55  #include <sys/wait.h>
  56  #include <ctype.h>
  57  
  58  #include "pkg.h"
  59  #include "private/event.h"
  60  #include "private/pkg.h"
  61  #include "private/pkgdb.h"
  62  #include "private/pkg_jobs.h"
  63  
  64  extern struct pkg_ctx ctx;
  65  
  66  static int _pkg_is_installed(struct pkg_jobs *j, const char *pattern,
  67      match_t match);
  68  static int pkg_jobs_find_upgrade(struct pkg_jobs *j, const char *pattern, match_t m);
  69  static int pkg_jobs_fetch(struct pkg_jobs *j);
  70  static bool new_pkg_version(struct pkg_jobs *j);
  71  static int pkg_jobs_check_conflicts(struct pkg_jobs *j);
  72  struct pkg_jobs_locked {
  73  	int (*locked_pkg_cb)(struct pkg *, void *);
  74  	void *context;
  75  };
  76  static __thread struct pkg_jobs_locked *pkgs_job_lockedpkg;
  77  typedef vec_t(int64_t) candidates_t;
  78  
  79  #define IS_DELETE(j) ((j)->type == PKG_JOBS_DEINSTALL || (j)->type == PKG_JOBS_AUTOREMOVE)
  80  
  81  int
  82  pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
  83  {
  84  	assert(db != NULL);
  85  
  86  	*j = xcalloc(1, sizeof(struct pkg_jobs));
  87  	(*j)->universe = pkg_jobs_universe_new(*j);
  88  	(*j)->db = db;
  89  	(*j)->type = t;
  90  	(*j)->solved = false;
  91  	(*j)->pinning = true;
  92  	(*j)->flags = PKG_FLAG_NONE;
  93  	(*j)->conservative = pkg_object_bool(pkg_config_get("CONSERVATIVE_UPGRADE"));
  94  	(*j)->triggers.dfd = -1;
  95  
  96  	return (EPKG_OK);
  97  }
  98  
  99  void
 100  pkg_jobs_set_flags(struct pkg_jobs *j, pkg_flags flags)
 101  {
 102  	j->flags = flags;
 103  }
 104  
 105  int
 106  pkg_jobs_set_repository(struct pkg_jobs *j, const char *ident)
 107  {
 108  	c_charv_t idents = vec_init();
 109  	if (ident != NULL)
 110  		vec_push(&idents, ident);
 111  	return (pkg_jobs_set_repositories(j, &idents));
 112  }
 113  
 114  int
 115  pkg_jobs_set_repositories(struct pkg_jobs *j, c_charv_t *idents)
 116  {
 117  	int ret = EPKG_OK;
 118  	if (idents == NULL)
 119  		return (EPKG_OK);
 120  	for (size_t i = 0; i < idents->len; i++) {
 121  		if ((pkg_repo_find(idents->d[i])) == NULL) {
 122  			pkg_emit_error("Unknown repository: %s", idents->d[i]);
 123  			ret = EPKG_FATAL;
 124  		}
 125  	}
 126  	if (ret == EPKG_FATAL)
 127  		return (ret);
 128  
 129  	j->reponames = idents;
 130  
 131  	return (ret);
 132  }
 133  
 134  int
 135  pkg_jobs_set_destdir(struct pkg_jobs *j, const char *dir)
 136  {
 137  	if (dir == NULL)
 138  		return (EPKG_FATAL);
 139  
 140  	j->destdir = dir;
 141  
 142  	return (EPKG_OK);
 143  }
 144  
 145  const char*
 146  pkg_jobs_destdir(struct pkg_jobs *j)
 147  {
 148  	return (j->destdir);
 149  }
 150  
 151  static void
 152  pkg_jobs_pattern_free(struct job_pattern *jp)
 153  {
 154  	free(jp->pattern);
 155  	free(jp->path);
 156  	free(jp);
 157  }
 158  
 159  void
 160  pkg_jobs_request_free(struct pkg_job_request *req)
 161  {
 162  	struct pkg_job_request_item *it, *tmp;
 163  
 164  	if (req != NULL) {
 165  		DL_FOREACH_SAFE(req->item, it, tmp) {
 166  			free(it);
 167  		}
 168  
 169  		free(req);
 170  	}
 171  }
 172  
 173  void
 174  pkg_jobs_free(struct pkg_jobs *j)
 175  {
 176  	pkghash_it it;
 177  
 178  	if (j == NULL)
 179  		return;
 180  
 181  	it = pkghash_iterator(j->request_add);
 182  	while (pkghash_next(&it))
 183  		pkg_jobs_request_free(it.value);
 184  	pkghash_destroy(j->request_add);
 185  	j->request_add = NULL;
 186  
 187  	it = pkghash_iterator(j->request_delete);
 188  	while (pkghash_next(&it))
 189  		pkg_jobs_request_free(it.value);
 190  	pkghash_destroy(j->request_delete);
 191  	j->request_delete = NULL;
 192  
 193  	pkg_jobs_universe_free(j->universe);
 194  	vec_free_and_free(&j->jobs, free);
 195  	LL_FREE(j->patterns, pkg_jobs_pattern_free);
 196  	if (j->triggers.cleanup != NULL) {
 197  		vec_free_and_free(j->triggers.cleanup, trigger_free);
 198  		free(j->triggers.cleanup);
 199  	}
 200  	if (j->triggers.dfd != -1)
 201  		close(j->triggers.dfd);
 202  	if (j->triggers.schema != NULL)
 203  		ucl_object_unref(j->triggers.schema);
 204  	pkghash_destroy(j->orphaned);
 205  	pkghash_destroy(j->notorphaned);
 206  	vec_free_and_free(&j->system_shlibs, free);
 207  	free(j);
 208  }
 209  
 210  static bool
 211  pkg_jobs_maybe_match_url(struct job_pattern *jp, const char *pattern)
 212  {
 213  	char path[MAXPATHLEN];
 214  	const char *name;
 215  
 216  	if (strncmp(pattern, "http://", 7) != 0 &&
 217  	    strncmp(pattern, "https://", 8) != 0 &&
 218  	    strncmp(pattern, "file://", 7) != 0)
 219  		return (false);
 220  
 221  	if (!str_ends_with(pattern, ".pkg"))
 222  		return (false);
 223  
 224  	name = strrchr(pattern, '/');
 225  	if (name == NULL)
 226  		name = pattern;
 227  	else
 228  		name++;
 229  
 230  	snprintf(path, sizeof(path), "%s/%s.XXXXX",
 231  	    getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp", name);
 232  
 233  	if (pkg_fetch_file(NULL, pattern, path, 0, 0, 0) != EPKG_OK) {
 234  		pkg_emit_error("Failed to fetch package from '%s'", pattern);
 235  		return (false);
 236  	}
 237  
 238  	dbg(2, "Fetched URL to file: %s", path);
 239  	jp->flags |= PKG_PATTERN_FLAG_FILE;
 240  	jp->path = xstrdup(path);
 241  	/* Use just the filename (without .pkg extension) as the pattern */
 242  	int len = strlen(name) - 3; /* strip ".pkg" */
 243  	jp->pattern = xmalloc(len);
 244  	strlcpy(jp->pattern, name, len);
 245  
 246  	return (true);
 247  }
 248  
 249  static bool
 250  pkg_jobs_maybe_match_file(struct job_pattern *jp, const char *pattern)
 251  {
 252  	const char *dot_pos;
 253  	char *pkg_path;
 254  
 255  	assert(jp != NULL);
 256  	assert(pattern != NULL);
 257  
 258  	if (pkg_jobs_maybe_match_url(jp, pattern))
 259  		return (true);
 260  
 261  	dot_pos = strrchr(pattern, '.');
 262  	if (dot_pos != NULL) {
 263  		/*
 264  		 * Compare suffix with .txz or .tbz
 265  		 */
 266  		dot_pos ++;
 267  		if (STREQ(dot_pos, "pkg") ||
 268  		    STREQ(dot_pos, "tzst") ||
 269  		    STREQ(dot_pos, "txz") ||
 270  		    STREQ(dot_pos, "tbz") ||
 271  		    STREQ(dot_pos, "tgz") ||
 272  		    STREQ(dot_pos, "tar")) {
 273  			if ((pkg_path = realpath(pattern, NULL)) != NULL) {
 274  				/* Dot pos is one character after the dot */
 275  				int len = dot_pos - pattern;
 276  
 277  				dbg(2, "Adding file: %s", pattern);
 278  				jp->flags |= PKG_PATTERN_FLAG_FILE;
 279  				jp->path = pkg_path;
 280  				jp->pattern = xmalloc(len);
 281  				strlcpy(jp->pattern, pattern, len);
 282  
 283  				return (true);
 284  			}
 285  		}
 286  	}
 287  	else if (STREQ(pattern, "-")) {
 288  		/*
 289  		 * Read package from stdin
 290  		 */
 291  		jp->flags = PKG_PATTERN_FLAG_FILE;
 292  		jp->path = xstrdup(pattern);
 293  		jp->pattern = xstrdup(pattern);
 294  	}
 295  
 296  	return (false);
 297  }
 298  
 299  int
 300  pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc)
 301  {
 302  	struct job_pattern *jp;
 303  	int i = 0;
 304  
 305  	if (j->solved) {
 306  		pkg_emit_error("The job has already been solved. "
 307  		    "Unable to append new elements");
 308  		return (EPKG_FATAL);
 309  	}
 310  
 311  	for (i = 0; i < argc; i++) {
 312  		jp = xcalloc(1, sizeof(struct job_pattern));
 313  		if (j->type == PKG_JOBS_DEINSTALL ||
 314  		    !pkg_jobs_maybe_match_file(jp, argv[i])) {
 315  			jp->pattern = xstrdup(argv[i]);
 316  			jp->match = match;
 317  		}
 318  		LL_APPEND(j->patterns, jp);
 319  	}
 320  
 321  	if (argc == 0 && match == MATCH_ALL) {
 322  		jp = xcalloc(1, sizeof(struct job_pattern));
 323  		jp->pattern = NULL;
 324  		jp->match = match;
 325  		LL_APPEND(j->patterns, jp);
 326  	}
 327  
 328  	return (EPKG_OK);
 329  }
 330  
 331  bool
 332  pkg_jobs_iter(struct pkg_jobs *j, void **iter,
 333  				struct pkg **new, struct pkg **old,
 334  				int *type)
 335  {
 336  	struct pkg_solved *s;
 337  	struct {
 338  		pkg_solved_list *list;
 339  		size_t pos;
 340  	} *t;
 341  	t = *iter;
 342  	if (*iter == NULL) {
 343  		t = xcalloc(1, sizeof(*t));
 344  		*iter = t;
 345  	} else if (t->pos >= t->list->len) {
 346  			free(t);
 347  			return (false);
 348  	}
 349  
 350  	if (j->jobs.len == 0) {
 351  		free(t);
 352  		*iter = NULL;
 353  		return (false);
 354  	}
 355  	if (t->list == NULL) {
 356  		t->list = &j->jobs;
 357  		t->pos = 0;
 358  	}
 359  	s = t->list->d[t->pos++];
 360  	*new = s->items[0]->pkg;
 361  	*old = s->items[1] ? s->items[1]->pkg : NULL;
 362  	*type = s->type;
 363  	return (true);
 364  }
 365  
 366  static struct pkg_job_request_item*
 367  pkg_jobs_add_req_from_universe(pkghash **head, struct pkg_job_universe_item *un,
 368      bool local, bool automatic)
 369  {
 370  	struct pkg_job_request *req;
 371  	struct pkg_job_request_item *nit;
 372  	struct pkg_job_universe_item *uit;
 373  	bool new_req = false;
 374  
 375  	assert(un != NULL);
 376  	req = pkghash_get_value(*head, un->pkg->uid);
 377  
 378  	if (req == NULL) {
 379  		req = xcalloc(1, sizeof(*req));
 380  		new_req = true;
 381  		req->automatic = automatic;
 382  		dbg(4, "add new uid %s to the request", un->pkg->uid);
 383  	}
 384  	else {
 385  		if (req->item->unit == un) {
 386  			/* We have exactly the same request, skip it */
 387  			return (req->item);
 388  		}
 389  	}
 390  
 391  	DL_FOREACH(un, uit) {
 392  		if ((uit->pkg->type == PKG_INSTALLED && local) ||
 393  				(uit->pkg->type != PKG_INSTALLED && !local)) {
 394  			nit = xcalloc(1, sizeof(*nit));
 395  			nit->pkg = uit->pkg;
 396  			nit->unit = uit;
 397  			DL_APPEND(req->item, nit);
 398  		}
 399  	}
 400  
 401  	if (new_req) {
 402  		if (req->item != NULL) {
 403  			pkghash_safe_add(*head, un->pkg->uid, req, NULL);
 404  		}
 405  		else {
 406  			free(req);
 407  			return (NULL);
 408  		}
 409  	}
 410  
 411  	return (req->item);
 412  }
 413  
 414  static struct pkg_job_request_item*
 415  pkg_jobs_add_req(struct pkg_jobs *j, struct pkg *pkg)
 416  {
 417  	pkghash **head;
 418  	struct pkg_job_request *req;
 419  	struct pkg_job_request_item *nit;
 420  	struct pkg_job_universe_item *un;
 421  	int rc;
 422  
 423  	assert(pkg != NULL);
 424  
 425  	if (!IS_DELETE(j)) {
 426  		head = &j->request_add;
 427  		assert(pkg->type != PKG_INSTALLED);
 428  	}
 429  	else {
 430  		head = &j->request_delete;
 431  		assert(pkg->type == PKG_INSTALLED);
 432  	}
 433  
 434  	dbg(4, "add package %s-%s to the request", pkg->name,
 435  			pkg->version);
 436  	rc = pkg_jobs_universe_add_pkg(j->universe, pkg, &un);
 437  
 438  	if (rc == EPKG_END) {
 439  		/*
 440  		 * This means that we have a package in the universe with the same
 441  		 * digest. In turn, that means that two upgrade candidates are equal,
 442  		 * we thus won't do anything with this item, as it is definitely useless
 443  		 */
 444  		req = pkghash_get_value(*head, pkg->uid);
 445  		if (req != NULL) {
 446  			DL_FOREACH(req->item, nit) {
 447  				if (nit->unit == un)
 448  					return (nit);
 449  			}
 450  		}
 451  		else {
 452  			/*
 453  			 * We need to add request chain from the universe chain
 454  			 */
 455  			return (pkg_jobs_add_req_from_universe(head, un, IS_DELETE(j), false));
 456  		}
 457  
 458  		return (NULL);
 459  	}
 460  	else if (rc == EPKG_FATAL) {
 461  		/*
 462  		 * Something bad has happened
 463  		 */
 464  		return (NULL);
 465  	}
 466  
 467  	if (pkg->locked) {
 468  		pkg_emit_locked(pkg);
 469  		return (NULL);
 470  	}
 471  
 472  	req = pkghash_get_value(*head, pkg->uid);
 473  
 474  	nit = xcalloc(1, sizeof(*nit));
 475  	nit->pkg = pkg;
 476  	nit->unit = un;
 477  
 478  	if (req == NULL) {
 479  		/* Allocate new unique request item */
 480  		req = xcalloc(1, sizeof(*req));
 481  		pkghash_safe_add(*head, pkg->uid, req, NULL);
 482  	}
 483  
 484  	/* Append candidate to the list of candidates */
 485  	DL_APPEND(req->item, nit);
 486  
 487  	return (nit);
 488  }
 489  
 490  static bool
 491  append_to_del_request(struct pkg_jobs *j, pkgs_t *to_process, const char *uid, const char *reqname)
 492  {
 493  	struct pkg *p;
 494  
 495  	p = pkg_jobs_universe_get_local(j->universe, uid, 0);
 496  	if (p == NULL)
 497  		return (true);
 498  	if (p->locked) {
 499  		pkg_emit_error("%s is locked cannot delete %s", p->name,
 500  		   reqname);
 501  		return (false);
 502  	}
 503  	vec_push(to_process, p);
 504  	return (true);
 505  }
 506  
 507  static bool
 508  delete_process_provides(struct pkg_jobs *j, struct pkg *lp, const char *provide,
 509      struct pkgdb_it *(*provideq)(struct pkgdb *db, const char *req),
 510      struct pkgdb_it *(*requireq)(struct pkgdb *db, const char *req),
 511      pkgs_t *to_process)
 512  {
 513  	struct pkgdb_it *lit, *rit;
 514  	struct pkg *pkg;
 515  	struct pkg_job_request *req;
 516  	bool ret = true;
 517  
 518  	/* check for pkgbase shlibs and provides */
 519  	if (charv_search(&j->system_shlibs, provide) != NULL)
 520  		return (ret);
 521  	/* if something else to provide the same thing we can safely delete */
 522  	lit = provideq(j->db, provide);
 523  	if (lit == NULL)
 524  		return (ret);
 525  	pkg = NULL;
 526  	while (pkgdb_it_next(lit, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
 527  		/* skip myself */
 528  		if (STREQ(pkg->uid, lp->uid))
 529  			continue;
 530  		req = pkghash_get_value(j->request_delete, pkg->uid);
 531  		/*
 532  		 * skip already processed provides
 533  		 * if N packages providing the same "provide"
 534  		 * are in the request delete they needs to be
 535  		 * counted as to be removed and then if no
 536  		 * packages are left providing the provide are
 537  		 * left after the removal of those packages
 538  		 * cascade.
 539  		 */
 540  		if (req != NULL && req->processed)
 541  			continue;
 542  
 543  		pkgdb_it_free (lit);
 544  		return (ret);
 545  	}
 546  	pkgdb_it_free(lit);
 547  	rit = requireq(j->db, provide);
 548  	if (rit == NULL)
 549  		return (ret);
 550  
 551  	pkg = NULL;
 552  	while (pkgdb_it_next(rit, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
 553  		if (!append_to_del_request(j, to_process,
 554  		    pkg->uid, lp->name))
 555  			ret = false;
 556  	}
 557  	pkgdb_it_free(rit);
 558  	return (ret);
 559  }
 560  
 561  /*
 562   * For delete request we merely check rdeps and force flag
 563   */
 564  static int
 565  pkg_jobs_process_delete_request(struct pkg_jobs *j)
 566  {
 567  	bool force = j->flags & PKG_FLAG_FORCE;
 568  	struct pkg_job_request *req;
 569  	struct pkg_dep *d = NULL;
 570  	struct pkg *lp;
 571  	int rc = EPKG_OK;
 572  	pkgs_t to_process = vec_init();
 573  	pkghash_it it;
 574  
 575  	if (j->type == PKG_JOBS_DEINSTALL && force &&
 576  	    (j->flags & PKG_FLAG_RECURSIVE) == 0)
 577  		return (EPKG_OK);
 578  
 579  	/*
 580  	 * Need to add also all reverse deps here
 581  	 */
 582  	it = pkghash_iterator(j->request_delete);
 583  	while (pkghash_next(&it)) {
 584  		req = it.value;
 585  		if (req->processed)
 586  			continue;
 587  		req->processed = true;
 588  		lp = req->item->pkg;
 589  		d = NULL;
 590  		while (pkg_rdeps(lp, &d) == EPKG_OK) {
 591  			if (!append_to_del_request(j, &to_process, d->uid,
 592  			    lp->name))
 593  				rc = EPKG_FATAL;
 594  		}
 595  
 596  		vec_foreach(lp->provides, i) {
 597  			if (!delete_process_provides(j, lp, lp->provides.d[i],
 598  			    pkgdb_query_provide, pkgdb_query_require,
 599  			    &to_process))
 600  				rc = EPKG_FATAL;
 601  		}
 602  
 603  		vec_foreach(lp->shlibs_provided, i) {
 604  			if (!delete_process_provides(j, lp, lp->shlibs_provided.d[i],
 605  			    pkgdb_query_shlib_provide,
 606  			    pkgdb_query_shlib_require, &to_process))
 607  				rc = EPKG_FATAL;
 608  		}
 609  	}
 610  
 611  	if (rc == EPKG_FATAL)
 612  		return (rc);
 613  
 614  	vec_foreach(to_process, pit) {
 615  		lp = to_process.d[pit];
 616  		if (pkg_jobs_add_req(j, lp) == NULL) {
 617  			vec_free(&to_process);
 618  			return (EPKG_FATAL);
 619  		}
 620  	}
 621  
 622  	if (vec_len(&to_process) > 0)
 623  		rc = pkg_jobs_process_delete_request(j);
 624  	vec_free(&to_process);
 625  
 626  	return (rc);
 627  }
 628  
 629  static bool pkg_jobs_test_automatic(struct pkg_jobs *j, struct pkg *p);
 630  
 631  static bool
 632  _is_orphaned(struct pkg_jobs *j, const char *uid)
 633  {
 634  	struct pkg_job_universe_item *unit;
 635  	struct pkg *npkg;
 636  
 637  	if (pkghash_get(j->notorphaned, uid) != NULL)
 638  		return (false);
 639  	unit = pkg_jobs_universe_find(j->universe, uid);
 640  	if (unit != NULL) {
 641  		if (!unit->pkg->automatic || unit->pkg->vital)
 642  			return (false);
 643  		npkg = unit->pkg;
 644  	} else {
 645  		npkg = pkg_jobs_universe_get_local(j->universe, uid,
 646  		    PKG_LOAD_BASIC|PKG_LOAD_RDEPS|PKG_LOAD_ANNOTATIONS|
 647  		    PKG_LOAD_SHLIBS_REQUIRED|PKG_LOAD_REQUIRES);
 648  		if (npkg == NULL)
 649  			return (false);
 650  		if (!npkg->automatic || npkg->vital) {
 651  			pkg_free(npkg);
 652  			return (false);
 653  		}
 654  		if (pkg_jobs_universe_process(j->universe, npkg) != EPKG_OK)
 655  			return (false);
 656  	}
 657  
 658  	if (!pkg_jobs_test_automatic(j, npkg))
 659  		return (false);
 660  
 661  	return (true);
 662  }
 663  
 664  static bool
 665  is_orphaned(struct pkg_jobs *j, const char *uid)
 666  {
 667  	if (pkghash_get(j->orphaned, uid) != NULL)
 668  		return (true);
 669  	if (pkghash_get(j->notorphaned, uid) != NULL)
 670  		return (false);
 671  	/*
 672  	 * Optimistically mark as orphaned before evaluating to break
 673  	 * infinite recursion when packages form dependency cycles
 674  	 * (e.g. A depends on B explicitly while B requires a shlib
 675  	 * provided by A). If _is_orphaned() returns false, we correct
 676  	 * the entry below.
 677  	 */
 678  	pkghash_safe_add(j->orphaned, uid, NULL, NULL);
 679  	if (_is_orphaned(j, uid))
 680  		return (true);
 681  	pkghash_del(j->orphaned, uid);
 682  	pkghash_safe_add(j->notorphaned, uid, NULL, NULL);
 683  	return (false);
 684  }
 685  
 686  /**
 687   * Test whether package specified is automatic with all its rdeps
 688   * @param j
 689   * @param p
 690   * @return
 691   */
 692  static bool
 693  pkg_jobs_test_automatic(struct pkg_jobs *j, struct pkg *p)
 694  {
 695  	struct pkg_dep *d = NULL;
 696  	struct pkg *npkg = NULL;
 697  	struct pkgdb_it *it;
 698  
 699  	while (pkg_rdeps(p, &d) == EPKG_OK) {
 700  		if (!is_orphaned(j, d->uid))
 701  			return (false);
 702  	}
 703  
 704  	vec_foreach(p->provides, i) {
 705  		it = pkgdb_query_require(j->db, p->provides.d[i]);
 706  		if (it == NULL)
 707  			continue;
 708  		npkg = NULL;
 709  		while (pkgdb_it_next(it, &npkg, PKG_LOAD_BASIC) == EPKG_OK) {
 710  			if (!is_orphaned(j, npkg->uid)) {
 711  				pkgdb_it_free(it);
 712  				pkg_free(npkg);
 713  				return (false);
 714  			}
 715  		}
 716  		pkgdb_it_free(it);
 717  	}
 718  
 719  	vec_foreach(p->shlibs_provided, i) {
 720  		it = pkgdb_query_shlib_require(j->db, p->shlibs_provided.d[i]);
 721  		if (it == NULL)
 722  			continue;
 723  		npkg = NULL;
 724  		while (pkgdb_it_next(it, &npkg, PKG_LOAD_BASIC) == EPKG_OK) {
 725  			if (!is_orphaned(j, npkg->uid)) {
 726  				pkgdb_it_free(it);
 727  				pkg_free(npkg);
 728  				return (false);
 729  			}
 730  		}
 731  		pkgdb_it_free(it);
 732  	}
 733  	pkg_free(npkg);
 734  
 735  	return (true);
 736  }
 737  
 738  
 739  
 740  static bool
 741  new_pkg_version(struct pkg_jobs *j)
 742  {
 743  	struct pkg *p;
 744  	const char *uid = "pkg";
 745  	pkg_flags old_flags;
 746  	bool ret = false;
 747  	struct pkg_job_universe_item *nit, *cit;
 748  
 749  	/* Disable -f for pkg self-check, and restore at end. */
 750  	old_flags = j->flags;
 751  	j->flags &= ~(PKG_FLAG_FORCE|PKG_FLAG_RECURSIVE);
 752  
 753  	/* determine local pkgng */
 754  	p = pkg_jobs_universe_get_local(j->universe, uid, 0);
 755  
 756  	if (p == NULL) {
 757  		uid = "pkg-devel";
 758  		p = pkg_jobs_universe_get_local(j->universe, uid, 0);
 759  	}
 760  
 761  	/* you are using git version skip */
 762  	if (p == NULL) {
 763  		ret = false;
 764  		goto end;
 765  	}
 766  
 767  	/* Use maximum priority for pkg */
 768  	if (pkg_jobs_find_upgrade(j, uid, MATCH_INTERNAL) == EPKG_OK) {
 769  		/*
 770  		 * Now we can have *potential* upgrades, but we can have a situation,
 771  		 * when our upgrade candidate comes from another repo
 772  		 */
 773  		nit = pkg_jobs_universe_find(j->universe, uid);
 774  
 775  		if (nit) {
 776  			DL_FOREACH(nit, cit) {
 777  				if (pkg_version_change_between (cit->pkg, p) == PKG_UPGRADE) {
 778  					/* We really have newer version which is not installed */
 779  					ret = true;
 780  					break;
 781  				}
 782  			}
 783  		}
 784  	}
 785  
 786  end:
 787  	j->flags = old_flags;
 788  
 789  	return (ret);
 790  }
 791  
 792  static int
 793  pkg_jobs_process_remote_pkg(struct pkg_jobs *j, struct pkg *rp,
 794  	struct pkg_job_request_item **req, int with_version)
 795  {
 796  	struct pkg_job_universe_item *nit, *cur;
 797  	struct pkg_job_request_item *nrit = NULL;
 798  	struct pkg *lp = NULL;
 799  	struct pkg_dep *rdep = NULL;
 800  
 801  	if (rp->digest == NULL) {
 802  		if (pkg_checksum_calculate(rp, j->db, false, true, false) != EPKG_OK) {
 803  			return (EPKG_FATAL);
 804  		}
 805  	}
 806  	if (j->type != PKG_JOBS_FETCH) {
 807  		lp = pkg_jobs_universe_get_local(j->universe, rp->uid, 0);
 808  		if (lp && lp->locked) {
 809  			pkg_emit_locked(lp);
 810  			return (EPKG_LOCKED);
 811  		}
 812  	}
 813  
 814  	nit = pkg_jobs_universe_get_upgrade_candidates(j->universe, rp->uid, lp,
 815  		j->flags & PKG_FLAG_FORCE,
 816  		with_version != 0 ? rp->version : NULL);
 817  
 818  	if (nit != NULL) {
 819  		nrit = pkg_jobs_add_req_from_universe(&j->request_add, nit, false, false);
 820  
 821  		if (req != NULL)
 822  			*req = nrit;
 823  
 824  		if (j->flags & PKG_FLAG_UPGRADE_VULNERABLE) {
 825  			/* Set the proper reason */
 826  			DL_FOREACH(nit, cur) {
 827  				if (cur->pkg->type != PKG_INSTALLED) {
 828  					free(cur->pkg->reason);
 829  					xasprintf(&cur->pkg->reason, "vulnerability found");
 830  				}
 831  			}
 832  			/* Also process all rdeps recursively */
 833  			if (nrit != NULL) {
 834  				while (pkg_rdeps(nrit->pkg, &rdep) == EPKG_OK) {
 835  					lp = pkg_jobs_universe_get_local(j->universe, rdep->uid, 0);
 836  
 837  					if (lp) {
 838  						(void)pkg_jobs_process_remote_pkg(j, lp, NULL, 0);
 839  					}
 840  				}
 841  			}
 842  		}
 843  	}
 844  
 845  	if (nrit == NULL && lp)
 846  		return (EPKG_INSTALLED);
 847  
 848  	return (nrit != NULL ? EPKG_OK : EPKG_FATAL);
 849  }
 850  
 851  static int
 852  pkg_jobs_find_upgrade(struct pkg_jobs *j, const char *pattern, match_t m)
 853  {
 854  	struct pkg *p = NULL;
 855  	struct pkgdb_it *it;
 856  	bool checklocal, found = false;
 857  	int rc = EPKG_FATAL;
 858  	int with_version;
 859  	struct pkg_dep *rdep = NULL;
 860  	unsigned flags = PKG_LOAD_BASIC|PKG_LOAD_OPTIONS|PKG_LOAD_DEPS|
 861  			PKG_LOAD_REQUIRES|PKG_LOAD_PROVIDES|
 862  			PKG_LOAD_SHLIBS_REQUIRED|PKG_LOAD_SHLIBS_PROVIDED|
 863  			PKG_LOAD_ANNOTATIONS|PKG_LOAD_CONFLICTS;
 864  	struct pkg_job_universe_item *unit = NULL;
 865  
 866  	if ((it = pkgdb_repo_query2(j->db, pattern, m, j->reponames)) == NULL)
 867  		return (rc);
 868  
 869  	/*
 870  	 * MATCH_EXACT is handled at a higher level, so that we can complain if a
 871  	 * specific upgrade was requested without the package being locally installed.
 872  	 *
 873  	 * MATCH_ALL is a non-issue, because we will not get that from pkg-upgrade
 874  	 * anyways.
 875  
 876  	 * Pattern matches are the main target, as the above query may grab packages
 877  	 * that are not installed that we can ignore.
 878  	 */
 879  	checklocal = j->type == PKG_JOBS_UPGRADE && m != MATCH_EXACT && m != MATCH_ALL;
 880  	while (it != NULL && pkgdb_it_next(it, &p, flags) == EPKG_OK) {
 881  		if (checklocal &&
 882  		    _pkg_is_installed(j, p->name, MATCH_INTERNAL) != EPKG_OK)
 883  			continue;
 884  		if (pattern != NULL && *pattern != '@') {
 885  			with_version = strcmp(p->name, pattern);
 886  		} else {
 887  			with_version = 0;
 888  		}
 889  		rc = pkg_jobs_process_remote_pkg(j, p, NULL, with_version);
 890  		if (rc == EPKG_FATAL) {
 891  			break;
 892  		} else if (rc == EPKG_OK)
 893  			found = true;
 894  	}
 895  
 896  	pkgdb_it_free(it);
 897  
 898  	if (!found && rc != EPKG_INSTALLED && rc != EPKG_LOCKED) {
 899  		/*
 900  		 * Here we need to ensure that this package has no
 901  		 * reverse deps installed
 902  		 */
 903  		p = pkg_jobs_universe_get_local(j->universe, pattern,
 904  			PKG_LOAD_BASIC|PKG_LOAD_RDEPS);
 905  		if (p == NULL)
 906  			return (EPKG_FATAL);
 907  
 908  		while(pkg_rdeps(p, &rdep) == EPKG_OK) {
 909  			struct pkg *rdep_package;
 910  
 911  			rdep_package = pkg_jobs_universe_get_local(j->universe, rdep->uid,
 912  					PKG_LOAD_BASIC);
 913  			if (rdep_package != NULL)
 914  				return (EPKG_END);
 915  		}
 916  
 917  		dbg(2, "non-automatic package with pattern %s has not been found in "
 918  				"remote repo", pattern);
 919  		rc = pkg_jobs_universe_add_pkg(j->universe, p, &unit);
 920  	}
 921  
 922  	return (rc);
 923  }
 924  
 925  /*
 926   * Return EPKG_OK if a package with a name matching the pattern is installed
 927   * locally.
 928   */
 929  static int
 930  _pkg_is_installed(struct pkg_jobs *j, const char *pattern, match_t match)
 931  {
 932  	struct pkgdb_it *it;
 933  	struct pkg *pkg = NULL;
 934  	int ret;
 935  
 936  	it = pkgdb_query(j->db, pattern, match);
 937  	if (it != NULL) {
 938  		ret = pkgdb_it_next(it, &pkg,
 939  		    PKG_LOAD_BASIC | PKG_LOAD_ANNOTATIONS);
 940  		if (ret == EPKG_OK)
 941  			pkg_free(pkg);
 942  		else if (ret == EPKG_END)
 943  			ret = EPKG_NOTINSTALLED;
 944  		pkgdb_it_free(it);
 945  	} else {
 946  		ret = EPKG_FATAL;
 947  	}
 948  	return (ret);
 949  }
 950  
 951  static int
 952  pkg_jobs_find_remote_pattern(struct pkg_jobs *j, struct job_pattern *jp)
 953  {
 954  	int rc = EPKG_OK;
 955  	struct pkg *pkg = NULL;
 956  	struct pkg_job_request *req;
 957  
 958  	if (!(jp->flags & PKG_PATTERN_FLAG_FILE)) {
 959  		if (j->type == PKG_JOBS_UPGRADE &&
 960  		    (jp->match == MATCH_INTERNAL || jp->match == MATCH_EXACT)) {
 961  			/*
 962  			 * For upgrade patterns we must ensure that a local package is
 963  			 * installed as well.  This only works if we're operating on an
 964  			 * exact match, as we otherwise don't know exactly what packages
 965  			 * are in store for us.
 966  			 */
 967  			rc = _pkg_is_installed(j, jp->pattern, jp->match);
 968  			if (rc != EPKG_OK) {
 969  				pkg_emit_error(
 970  				    "%s is not installed, therefore upgrade is impossible",
 971  				    jp->pattern);
 972  				return (rc);
 973  			}
 974  		}
 975  		rc = pkg_jobs_find_upgrade(j, jp->pattern, jp->match);
 976  	} else if (pkg_open(&pkg, jp->path, PKG_OPEN_MANIFEST_ONLY) != EPKG_OK) {
 977  		rc = EPKG_FATAL;
 978  	} else if (pkg_validate(pkg, j->db) == EPKG_OK) {
 979  		if (j->type == PKG_JOBS_UPGRADE &&
 980  		    _pkg_is_installed(j, pkg->name, MATCH_INTERNAL) != EPKG_OK) {
 981  			pkg_emit_error(
 982  			    "%s is not installed, therefore upgrade is impossible",
 983  			    pkg->name);
 984  			return (EPKG_NOTINSTALLED);
 985  		}
 986  		pkg->type = PKG_FILE;
 987  		pkg_jobs_add_req(j, pkg);
 988  
 989  		req = pkghash_get_value(j->request_add, pkg->uid);
 990  		if (req != NULL)
 991  			req->item->jp = jp;
 992  	} else {
 993  		pkg_emit_error("cannot load %s: invalid format", jp->pattern);
 994  		rc = EPKG_FATAL;
 995  	}
 996  
 997  	return (rc);
 998  }
 999  
1000  bool
1001  pkg_jobs_need_upgrade(charv_t *system_shlibs, struct pkg *rp, struct pkg *lp)
1002  {
1003  	int ret, ret1, ret2;
1004  	struct pkg_option *lo = NULL, *ro = NULL;
1005  	struct pkg_dep *ld = NULL, *rd = NULL;
1006  	struct pkg_conflict *lc = NULL, *rc = NULL;
1007  
1008  	/* If no local package, then rp is obviously need to be added */
1009  	if (lp == NULL)
1010  		return true;
1011  
1012  	/* Do not upgrade locked packages */
1013  	if (lp->locked) {
1014  		pkg_emit_locked(lp);
1015  		return (false);
1016  	}
1017  
1018  	if (lp->digest != NULL && rp->digest != NULL &&
1019  	    STREQ(lp->digest, rp->digest)) {
1020  		/* Remote and local packages has the same digest, hence they are the same */
1021  		return (false);
1022  	}
1023  	/*
1024  	 * XXX: for a remote package we also need to check whether options
1025  	 * are compatible.
1026  	 */
1027  	ret = pkg_version_cmp(lp->version, rp->version);
1028  	if (ret > 0)
1029  		return (false);
1030  	else if (ret < 0)
1031  		return (true);
1032  
1033  	/* Compare archs */
1034  	if (!STREQ(lp->abi, rp->abi)) {
1035  		free(rp->reason);
1036  		xasprintf(&rp->reason, "ABI changed: '%s' -> '%s'",
1037  		    lp->abi, rp->abi);
1038  		return (true);
1039  	}
1040  
1041  	if (lp->vital != rp->vital) {
1042  		free(rp->reason);
1043  		xasprintf(&rp->reason, "Vital flag changed: '%s' -> '%s'",
1044  		    lp->vital ? "true" : "false", rp->vital ? "true" : "false");
1045  		return (true);
1046  	}
1047  
1048  	/* compare options */
1049  	for (;;) {
1050  		if (!pkg_object_bool(pkg_config_get("PKG_REINSTALL_ON_OPTIONS_CHANGE")))
1051  			break;
1052  		ret1 = pkg_options(rp, &ro);
1053  		ret2 = pkg_options(lp, &lo);
1054  		if (ret1 != ret2) {
1055  			free(rp->reason);
1056  			if (ro == NULL)
1057  				xasprintf(&rp->reason, "option removed: %s",
1058  				    lo->key);
1059  			else if (lo == NULL)
1060  				xasprintf(&rp->reason, "option added: %s",
1061  				    ro->key);
1062  			else
1063  				xasprintf(&rp->reason, "option changed: %s",
1064  				    ro->key);
1065  			assert(rp->reason != NULL);
1066  			return (true);
1067  		}
1068  		if (ret1 == EPKG_OK) {
1069  			if (!STREQ(lo->key, ro->key) ||
1070  			    !STREQ(lo->value, ro->value)) {
1071  				free(rp->reason);
1072  				xasprintf(&rp->reason, "options changed");
1073  				return (true);
1074  			}
1075  		}
1076  		else
1077  			break;
1078  	}
1079  
1080  	/* What about the direct deps */
1081  	for (;;) {
1082  		ret1 = pkg_deps(rp, &rd);
1083  		ret2 = pkg_deps(lp, &ld);
1084  		if (ret1 != ret2) {
1085  			free(rp->reason);
1086  			if (rd == NULL)
1087  				xasprintf(&rp->reason, "direct dependency removed: %s",
1088  				    ld->name);
1089  			else if (ld == NULL)
1090  				xasprintf(&rp->reason, "direct dependency added: %s",
1091  				    rd->name);
1092  			else
1093  				xasprintf(&rp->reason, "direct dependency changed: %s",
1094  				    rd->name);
1095  			assert (rp->reason != NULL);
1096  			return (true);
1097  		}
1098  		if (ret1 == EPKG_OK) {
1099  			if (!STREQ(rd->name, ld->name) ||
1100  			    !STREQ(rd->origin, ld->origin)) {
1101  				free(rp->reason);
1102  				xasprintf(&rp->reason, "direct dependency changed: %s",
1103  				    rd->name);
1104  				assert (rp->reason != NULL);
1105  				return (true);
1106  			}
1107  		}
1108  		else
1109  			break;
1110  	}
1111  
1112  	/* Conflicts */
1113  	for (;;) {
1114  		ret1 = pkg_conflicts(rp, &rc);
1115  		ret2 = pkg_conflicts(lp, &lc);
1116  		if (ret1 != ret2) {
1117  			free(rp->reason);
1118  			rp->reason = xstrdup("direct conflict changed");
1119  			return (true);
1120  		}
1121  		if (ret1 == EPKG_OK) {
1122  			if (!STREQ(rc->uid, lc->uid)) {
1123  				free(rp->reason);
1124  				rp->reason = xstrdup("direct conflict changed");
1125  				return (true);
1126  			}
1127  		}
1128  		else
1129  			break;
1130  	}
1131  
1132  	/* Provides */
1133  	if (vec_len(&rp->provides) != vec_len(&lp->provides)) {
1134  		free(rp->reason);
1135  		rp->reason = xstrdup("provides changed");
1136  		return (true);
1137  	}
1138  	pkg_lists_sort(lp);
1139  	pkg_lists_sort(rp);
1140  
1141  	vec_foreach(lp->provides, i) {
1142  		if (!STREQ(lp->provides.d[i], rp->provides.d[i])) {
1143  			free(rp->reason);
1144  			rp->reason = xstrdup("provides changed");
1145  			return (true);
1146  		}
1147  	}
1148  
1149  	/* Requires */
1150  	if (vec_len(&rp->requires) != vec_len(&lp->requires)) {
1151  		free(rp->reason);
1152  		rp->reason = xstrdup("requires changed");
1153  		return (true);
1154  	}
1155  	vec_foreach(lp->requires, i) {
1156  		if (!STREQ(lp->requires.d[i], rp->requires.d[i])) {
1157  			free(rp->reason);
1158  			rp->reason = xstrdup("requires changed");
1159  			return (true);
1160  		}
1161  	}
1162  
1163  	/* Finish by the shlibs */
1164  	if (vec_len(&rp->shlibs_provided) != vec_len(&lp->shlibs_provided)) {
1165  		free(rp->reason);
1166  		rp->reason = xstrdup("provided shared library changed");
1167  		return (true);
1168  	}
1169  	vec_foreach(lp->shlibs_provided, i) {
1170  		if (!STREQ(lp->shlibs_provided.d[i], rp->shlibs_provided.d[i])) {
1171  			free(rp->reason);
1172  			rp->reason = xstrdup("provided shared library changed");
1173  			return (true);
1174  		}
1175  	}
1176  
1177  	size_t cntr = vec_len(&rp->shlibs_required);
1178  	size_t cntl = vec_len(&lp->shlibs_required);
1179  	bool has_system_shlibs = vec_len(system_shlibs) > 0;
1180  
1181  	if (cntr != cntl && !has_system_shlibs) {
1182  		free(rp->reason);
1183  		rp->reason = xstrdup("required shared library changed");
1184  		return (true);
1185  	}
1186  
1187  	size_t i = 0, j = 0;
1188  	while (i < cntl || j < cntr) {
1189  		if (has_system_shlibs) {
1190  			while (i < cntl &&
1191  			    charv_search(system_shlibs,
1192  			    lp->shlibs_required.d[i]) != NULL)
1193  				i++;
1194  			while (j < cntr &&
1195  			    charv_search(system_shlibs,
1196  			    rp->shlibs_required.d[j]) != NULL)
1197  				j++;
1198  		}
1199  		if (i >= cntl && j >= cntr)
1200  			break;
1201  		if (i >= cntl || j >= cntr ||
1202  		    !STREQ(lp->shlibs_required.d[i],
1203  		    rp->shlibs_required.d[j])) {
1204  			free(rp->reason);
1205  			rp->reason =
1206  			    xstrdup("required shared library changed");
1207  			return (true);
1208  		}
1209  		i++;
1210  		j++;
1211  	}
1212  	return (false);
1213  }
1214  
1215  static void
1216  pkg_jobs_propagate_automatic(struct pkg_jobs *j)
1217  {
1218  	struct pkg_job_universe_item *unit, *cur, *local;
1219  	struct pkg_job_request *req;
1220  	bool automatic;
1221  	pkghash_it it;
1222  
1223  	it = pkghash_iterator(j->universe->items);
1224  	while (pkghash_next(&it)) {
1225  		unit = (struct pkg_job_universe_item *)it.value;
1226  		if (unit->next == NULL) {
1227  			/*
1228  			 * For packages that are alone in the installation list
1229  			 * we search them in the corresponding request
1230  			 */
1231  			req = pkghash_get_value(j->request_add, unit->pkg->uid);
1232  			if ((req == NULL || req->automatic) &&
1233  			    unit->pkg->type != PKG_INSTALLED) {
1234  				automatic = true;
1235  				dbg(2, "set automatic flag for %s", unit->pkg->uid);
1236  				unit->pkg->automatic = automatic;
1237  			}
1238  			else {
1239  				if (j->type == PKG_JOBS_INSTALL) {
1240  					unit->pkg->automatic = false;
1241  				}
1242  			}
1243  		}
1244  		else {
1245  			/*
1246  			 * For packages that are in the conflict chain we need to inherit
1247  			 * automatic flag from the local package
1248  			 */
1249  			local = NULL;
1250  			automatic = false;
1251  			LL_FOREACH(unit, cur) {
1252  				if (cur->pkg->type == PKG_INSTALLED) {
1253  					local = cur;
1254  					automatic = local->pkg->automatic;
1255  					break;
1256  				}
1257  			}
1258  			if (local != NULL) {
1259  				LL_FOREACH(unit, cur) {
1260  					/*
1261  					 * Propagate automatic from local package
1262  					 */
1263  					if (cur->pkg->type != PKG_INSTALLED) {
1264  						cur->pkg->automatic = automatic;
1265  					}
1266  				}
1267  			}
1268  			else {
1269  				/*
1270  				 * For packages that are not unique, we might still have
1271  				 * a situation when we need to set automatic for all
1272  				 * non-local packages
1273  				 *
1274  				 * See #1374
1275  				 */
1276  				req = pkghash_get_value(j->request_add, unit->pkg->uid);
1277  				if ((req == NULL || req->automatic)) {
1278  					automatic = true;
1279  					dbg(2, "set automatic flag for %s", unit->pkg->uid);
1280  					LL_FOREACH(unit, cur) {
1281  						cur->pkg->automatic = automatic;
1282  					}
1283  				}
1284  			}
1285  		}
1286  	}
1287  }
1288  
1289  static struct pkg_job_request *
1290  pkg_jobs_find_deinstall_request(struct pkg_job_universe_item *item,
1291  		struct pkg_jobs *j, int rec_level)
1292  {
1293  	struct pkg_job_request *found;
1294  	struct pkg_job_universe_item *dep_item;
1295  	struct pkg_dep *d = NULL;
1296  	struct pkg *pkg = item->pkg;
1297  
1298  	if (rec_level > 128) {
1299  		dbg(2, "cannot find deinstall request after 128 iterations for %s,"
1300  		    "circular dependency maybe", pkg->uid);
1301  		return (NULL);
1302  	}
1303  
1304  	found = pkghash_get_value(j->request_delete, pkg->uid);
1305  	if (found == NULL) {
1306  		while (pkg_deps(pkg, &d) == EPKG_OK) {
1307  			dep_item = pkg_jobs_universe_find(j->universe, d->uid);
1308  			if (dep_item) {
1309  				found = pkg_jobs_find_deinstall_request(dep_item, j, rec_level + 1);
1310  				if (found)
1311  					return (found);
1312  			}
1313  		}
1314  	}
1315  	else {
1316  		return (found);
1317  	}
1318  
1319  	return (NULL);
1320  }
1321  
1322  static void
1323  pkg_jobs_set_deinstall_reasons(struct pkg_jobs *j)
1324  {
1325  	struct pkg_solved *sit;
1326  	struct pkg_job_request *jreq;
1327  	struct pkg *req_pkg, *pkg;
1328  
1329  	vec_foreach(j->jobs, i) {
1330  		sit = j->jobs.d[i];
1331  		jreq = pkg_jobs_find_deinstall_request(sit->items[0], j, 0);
1332  		if (jreq != NULL && jreq->item->unit != sit->items[0]) {
1333  			req_pkg = jreq->item->pkg;
1334  			pkg = sit->items[0]->pkg;
1335  			/* Set the reason */
1336  			free(pkg->reason);
1337  			pkg_asprintf(&pkg->reason, "depends on %n-%v", req_pkg, req_pkg);
1338  		}
1339  	}
1340  }
1341  
1342  static int
1343  comp(const void *a, const void *b)
1344  {
1345  	const struct pkg *pa = a;
1346  	const struct pkg *pb = b;
1347  
1348  	return strcmp(pa->name, pb->name);
1349  }
1350  
1351  static int
1352  jobs_solve_deinstall(struct pkg_jobs *j)
1353  {
1354  	struct job_pattern *jp;
1355  	struct pkg *pkg = NULL;
1356  	struct pkgdb_it *it;
1357  	bool force = (j->flags & PKG_FLAG_FORCE);
1358  	LL_FOREACH(j->patterns, jp) {
1359  		if ((it = pkgdb_query(j->db, jp->pattern, jp->match)) == NULL)
1360  			return (EPKG_FATAL);
1361  
1362  		if (pkgdb_it_count(it) == 0) {
1363  			pkg_emit_notice("No packages matched for pattern '%s'\n", jp->pattern);
1364  		}
1365  
1366  		while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_RDEPS|
1367  		    PKG_LOAD_DEPS|PKG_LOAD_ANNOTATIONS|PKG_LOAD_PROVIDES|
1368  		    PKG_LOAD_SHLIBS_PROVIDED) == EPKG_OK) {
1369  			if(pkg->locked || (pkg->vital && !force)) {
1370  				if (tsearch(pkg, &j->lockedpkgs, comp) == NULL) {
1371  					pkgdb_it_free(it);
1372  					return (EPKG_FATAL);
1373  				}
1374  			}
1375  			else {
1376  				pkg_jobs_add_req(j, pkg);
1377  			}
1378  			pkg = NULL;
1379  		}
1380  		pkgdb_it_free(it);
1381  	}
1382  
1383  	j->solved = true;
1384  
1385  	return (pkg_jobs_process_delete_request(j));
1386  }
1387  
1388  static int
1389  jobs_solve_autoremove(struct pkg_jobs *j)
1390  {
1391  	struct pkg *pkg = NULL;
1392  	struct pkgdb_it *it;
1393  
1394  	if ((it = pkgdb_query_cond(j->db, " WHERE automatic=1 AND vital=0 AND locked=0", NULL, MATCH_ALL)) == NULL)
1395  		return (EPKG_FATAL);
1396  
1397  	while (pkgdb_it_next(it, &pkg,
1398  			PKG_LOAD_BASIC|PKG_LOAD_RDEPS|PKG_LOAD_DEPS|
1399  			PKG_LOAD_ANNOTATIONS|PKG_LOAD_PROVIDES|
1400  			PKG_LOAD_SHLIBS_PROVIDED)
1401  			== EPKG_OK) {
1402  		if (pkg_jobs_test_automatic(j, pkg)) {
1403  			assert(pkg_jobs_add_req(j, pkg));
1404  		}
1405  		pkg = NULL;
1406  	}
1407  	pkgdb_it_free(it);
1408  
1409  	j->solved = true;
1410  	pkg_jobs_process_delete_request(j);
1411  
1412  	return (EPKG_OK);
1413  }
1414  
1415  /*
1416   * Do we need to touch this package as part of an upgrade job?
1417   */
1418  static bool
1419  is_upgrade_candidate(struct pkg_jobs *j, struct pkg *pkg)
1420  {
1421  	struct pkgdb_it *it;
1422  	struct pkg *p = NULL;
1423  
1424  	/* A forced upgrade upgrades everything. */
1425  	if ((j->flags & PKG_FLAG_FORCE) != 0)
1426  		return (true);
1427  
1428  	/* If we have no digest, we need to check this package */
1429  	if (pkg->digest == NULL)
1430  		return (true);
1431  
1432  	/* Does a remote repo have a different version of this package? */
1433  	it = pkgdb_repo_query2(j->db, pkg->uid, MATCH_INTERNAL, j->reponames);
1434  	if (it != NULL) {
1435  		while (pkgdb_it_next(it, &p, PKG_LOAD_BASIC) == EPKG_OK) {
1436  			if (!STREQ(p->digest, pkg->digest)) {
1437  				/* Found an upgrade candidate. */
1438  				pkg_free(p);
1439  				pkgdb_it_free(it);
1440  				return (true);
1441  			}
1442  		}
1443  		pkgdb_it_free(it);
1444  		return (false);
1445  	}
1446  
1447  	return (true);
1448  }
1449  
1450  static candidates_t *
1451  pkg_jobs_find_upgrade_candidates(struct pkg_jobs *j)
1452  {
1453  	struct pkg *pkg = NULL;
1454  	struct pkgdb_it *it;
1455  	candidates_t *candidates;
1456  
1457  	if ((it = pkgdb_query(j->db, NULL, MATCH_ALL)) == NULL)
1458  		return (NULL);
1459  
1460  	candidates = xcalloc(1, sizeof(*candidates));
1461  	while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
1462  		if (is_upgrade_candidate(j, pkg))
1463  			vec_push(candidates, pkg->id);
1464  	}
1465  	pkgdb_it_free(it);
1466  
1467  	return (candidates);
1468  }
1469  
1470  static int
1471  jobs_solve_full_upgrade(struct pkg_jobs *j)
1472  {
1473  	struct pkg *pkg = NULL;
1474  	size_t jcount = 0;
1475  	size_t elt_num = 0;
1476  	char sqlbuf[256];
1477  	candidates_t *candidates;
1478  	struct pkg_job_request *req;
1479  	struct pkgdb_it *it;
1480  	pkghash_it hit;
1481  	unsigned flags = PKG_LOAD_BASIC|PKG_LOAD_OPTIONS|PKG_LOAD_DEPS|PKG_LOAD_REQUIRES|
1482  			PKG_LOAD_SHLIBS_REQUIRED|PKG_LOAD_ANNOTATIONS|PKG_LOAD_CONFLICTS;
1483  
1484  	assert(!j->solved);
1485  
1486  	candidates = pkg_jobs_find_upgrade_candidates(j);
1487  	if (candidates == NULL)
1488  		return (EPKG_FATAL);
1489  	jcount = candidates->len;
1490  
1491  	pkg_emit_progress_start("Checking for upgrades (%zd candidates)",
1492  			jcount);
1493  
1494  	vec_foreach(*candidates, i) {
1495  		pkg_emit_progress_tick(++elt_num, jcount);
1496  		sqlite3_snprintf(sizeof(sqlbuf), sqlbuf, " WHERE p.id=%" PRId64,
1497  		    candidates->d[i]);
1498  		if ((it = pkgdb_query_cond(j->db, sqlbuf, NULL, MATCH_ALL)) == NULL)
1499  			return (EPKG_FATAL);
1500  
1501  		pkg = NULL;
1502  		while (pkgdb_it_next(it, &pkg, flags) == EPKG_OK) {
1503  			/* Do not test we ignore what doesn't exists remotely */
1504  			pkg_jobs_find_upgrade(j, pkg->uid, MATCH_INTERNAL);
1505  		}
1506  		pkg_free(pkg);
1507  		pkgdb_it_free(it);
1508  	}
1509  	vec_free(candidates);
1510  	free(candidates);
1511  	pkg_emit_progress_tick(jcount, jcount);
1512  
1513  	pkg_emit_progress_start("Processing candidates (%zd candidates)",
1514  			jcount);
1515  	elt_num = 0;
1516  
1517  	hit = pkghash_iterator(j->request_add);
1518  	while (pkghash_next(&hit)) {
1519  		req = hit.value;
1520  		pkg_emit_progress_tick(++elt_num, jcount);
1521  		pkg_jobs_universe_process(j->universe, req->item->pkg);
1522  	}
1523  	pkg_emit_progress_tick(jcount, jcount);
1524  
1525  	pkg_jobs_universe_process_upgrade_chains(j);
1526  
1527  	return (EPKG_OK);
1528  }
1529  
1530  static int
1531  jobs_solve_partial_upgrade(struct pkg_jobs *j)
1532  {
1533  	struct job_pattern *jp;
1534  	struct pkg_job_request *req;
1535  	bool error_found = false;
1536  	int retcode;
1537  	pkghash_it it;
1538  
1539  	assert(!j->solved);
1540  
1541  	LL_FOREACH(j->patterns, jp) {
1542  		retcode = pkg_jobs_find_remote_pattern(j, jp);
1543  		if (retcode == EPKG_FATAL) {
1544  			pkg_emit_error("No packages available to %s matching '%s' "
1545  					"have been found in the "
1546  					"repositories",
1547  					(j->type == PKG_JOBS_UPGRADE) ? "upgrade" : "install",
1548  					jp->pattern);
1549  			/* delay the return to be sure we print a message for all issues */
1550  			if ((j->flags & PKG_FLAG_UPGRADE_VULNERABLE) == 0)
1551  				error_found = true;
1552  		}
1553  		if (retcode == EPKG_LOCKED) {
1554  			return (retcode);
1555  		}
1556  	}
1557  	if (error_found)
1558  		return (EPKG_FATAL);
1559  	/*
1560  	 * Here we have not selected the proper candidate among all
1561  	 * possible choices.
1562  	 * Hence, we want to perform this procedure now to ensure that
1563  	 * we are processing the correct packages.
1564  	 */
1565  	pkg_jobs_universe_process_upgrade_chains(j);
1566  	/*
1567  	 * Need to iterate request one more time to recurse depends
1568  	 */
1569  
1570  	it = pkghash_iterator(j->request_add);
1571  	while (pkghash_next(&it)) {
1572  		req = it.value;
1573  		retcode = pkg_jobs_universe_process(j->universe, req->item->pkg);
1574  		if (retcode != EPKG_OK)
1575  			return (retcode);
1576  	}
1577  	return (EPKG_OK);
1578  }
1579  
1580  static int
1581  jobs_solve_install_upgrade(struct pkg_jobs *j)
1582  {
1583  	struct pkg_job_request *req;
1584  	int retcode = 0;
1585  	pkghash_it it;
1586  
1587  	/* Check for new pkg. Skip for 'upgrade -F'. */
1588  	if ((j->flags & PKG_FLAG_SKIP_INSTALL) == 0 &&
1589  	    (j->flags & PKG_FLAG_DRY_RUN) == 0 &&
1590  	    (j->flags & PKG_FLAG_PKG_VERSION_TEST) == PKG_FLAG_PKG_VERSION_TEST)
1591  		if (new_pkg_version(j)) {
1592  			j->flags &= ~PKG_FLAG_PKG_VERSION_TEST;
1593  			j->conservative = false;
1594  			j->pinning = false;
1595  			pkg_emit_newpkgversion();
1596  			goto order;
1597  		}
1598  
1599  	if (j->patterns == NULL && j->type == PKG_JOBS_INSTALL) {
1600  		pkg_emit_error("no patterns are specified for install job");
1601  		return (EPKG_FATAL);
1602  	}
1603  
1604  	if (!j->solved) {
1605  		if (j->patterns == NULL) {
1606  			retcode = jobs_solve_full_upgrade(j);
1607  			if (retcode != EPKG_OK)
1608  				return (retcode);
1609  		} else {
1610  			retcode = jobs_solve_partial_upgrade(j);
1611  			if (retcode != EPKG_OK)
1612  				return (retcode);
1613  		}
1614  	}
1615  	else {
1616  		/*
1617  		 * If we have tried to solve request, then we just want to re-add all
1618  		 * request packages to the universe to find out any potential conflicts
1619  		 */
1620  		it = pkghash_iterator(j->request_add);
1621  		while (pkghash_next(&it)) {
1622  			req = it.value;
1623  			pkg_jobs_universe_process(j->universe, req->item->pkg);
1624  		}
1625  	}
1626  
1627  	if (pkg_conflicts_request_resolve(j) != EPKG_OK) {
1628  		pkg_emit_error("Cannot resolve conflicts in a request");
1629  		return (EPKG_FATAL);
1630  	}
1631  
1632  	pkg_jobs_propagate_automatic(j);
1633  
1634  order:
1635  
1636  	j->solved = true;
1637  
1638  	return (EPKG_OK);
1639  }
1640  
1641  static int
1642  jobs_solve_fetch(struct pkg_jobs *j)
1643  {
1644  	struct job_pattern *jp;
1645  	struct pkg *pkg = NULL;
1646  	struct pkgdb_it *it;
1647  	struct pkg_job_request *req;
1648  	pkghash_it hit;
1649  	pkg_error_t rc;
1650  
1651  	assert(!j->solved);
1652  
1653  	if ((j->flags & PKG_FLAG_UPGRADES_FOR_INSTALLED) == PKG_FLAG_UPGRADES_FOR_INSTALLED) {
1654  		if ((it = pkgdb_query(j->db, NULL, MATCH_ALL)) == NULL)
1655  			return (EPKG_FATAL);
1656  
1657  		while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
1658  			if(pkg->locked) {
1659  				pkg_emit_locked(pkg);
1660  			}
1661  			else {
1662  				/* Do not test we ignore what doesn't exists remotely */
1663  				pkg_jobs_find_upgrade(j, pkg->uid, MATCH_INTERNAL);
1664  			}
1665  			pkg = NULL;
1666  		}
1667  		pkgdb_it_free(it);
1668  	} else {
1669  		LL_FOREACH(j->patterns, jp) {
1670  			/* TODO: use repository priority here */
1671  			if (pkg_jobs_find_upgrade(j, jp->pattern, jp->match) == EPKG_FATAL)
1672  				pkg_emit_error("No packages matching '%s' have been found in the "
1673  						"repositories", jp->pattern);
1674  		}
1675  		hit = pkghash_iterator(j->request_add);
1676  		while (pkghash_next(&hit)) {
1677  			req = hit.value;
1678  			rc = pkg_jobs_universe_process(j->universe, req->item->pkg);
1679  			if (rc != EPKG_OK && rc != EPKG_END)
1680  				return (rc);
1681  		}
1682  	}
1683  
1684  	j->solved = true;
1685  
1686  	return (EPKG_OK);
1687  }
1688  
1689  static int
1690  solve_with_external_cudf_solver(struct pkg_jobs *j, const char *solver)
1691  {
1692  	int ret, pstatus;
1693  	FILE *spipe[2];
1694  	pid_t pchild;
1695  
1696  	pchild = process_spawn_pipe(spipe, solver);
1697  	if (pchild == -1)
1698  		return (EPKG_FATAL);
1699  
1700  	ret = pkg_jobs_cudf_emit_file(j, j->type, spipe[1]);
1701  	fclose(spipe[1]);
1702  
1703  	if (ret == EPKG_OK)
1704  		ret = pkg_jobs_cudf_parse_output(j, spipe[0]);
1705  
1706  	fclose(spipe[0]);
1707  	waitpid(pchild, &pstatus, WNOHANG);
1708  
1709  	return (ret);
1710  }
1711  
1712  static int
1713  solve_with_external_sat_solver(struct pkg_solve_problem *pb, const char *solver)
1714  {
1715  	int ret, pstatus;
1716  	FILE *spipe[2];
1717  	pid_t pchild;
1718  
1719  	pchild = process_spawn_pipe(spipe, solver);
1720  	if (pchild == -1)
1721  		return (EPKG_FATAL);
1722  
1723  	ret = pkg_solve_dimacs_export(pb, spipe[1]);
1724  	fclose(spipe[1]);
1725  
1726  	if (ret == EPKG_OK)
1727  		ret = pkg_solve_parse_sat_output(spipe[0], pb);
1728  
1729  	fclose(spipe[0]);
1730  	waitpid(pchild, &pstatus, WNOHANG);
1731  
1732  	return (ret);
1733  }
1734  
1735  static int
1736  solve_with_sat_solver(struct pkg_jobs *j)
1737  {
1738  	const char *sat_solver = pkg_object_string(pkg_config_get("SAT_SOLVER"));
1739  	struct pkg_solve_problem *problem;
1740  	const char *dotfile;
1741  	FILE *dot = NULL;
1742  	int ret;
1743  
1744  	pkg_jobs_universe_process_upgrade_chains(j);
1745  	problem = pkg_solve_jobs_to_sat(j);
1746  	if (problem == NULL) {
1747  		pkg_emit_error("cannot convert job to SAT problem");
1748  		j->solved = false;
1749  		return (EPKG_FATAL);
1750  	}
1751  
1752  	if (sat_solver != NULL)
1753  		return (solve_with_external_sat_solver(problem, sat_solver));
1754  
1755  	ret = pkg_solve_sat_problem(problem);
1756  	if (ret == EPKG_AGAIN) {
1757  		pkg_solve_problem_free(problem);
1758  		return (solve_with_sat_solver(j));
1759  	}
1760  
1761  	if (ret == EPKG_FATAL) {
1762  		pkg_emit_error("cannot solve job using SAT solver");
1763  		pkg_solve_problem_free(problem);
1764  		j->solved = false;
1765  	} else {
1766  		ret = pkg_solve_sat_to_jobs(problem);
1767  	}
1768  
1769  	if ((dotfile = pkg_object_string(pkg_config_get("DOT_FILE")))
1770  		!= NULL) {
1771  		dot = fopen(dotfile, "we");
1772  
1773  		if (dot == NULL) {
1774  			pkg_emit_errno("fopen", dotfile);
1775  		} else {
1776  			pkg_solve_dot_export(problem, dot);
1777  			fclose(dot);
1778  		}
1779  	}
1780  	pkg_solve_problem_free(problem);
1781  
1782  	return (ret);
1783  }
1784  
1785  static int
1786  pkg_jobs_run_solver(struct pkg_jobs *j)
1787  {
1788  	int ret;
1789  
1790  	pkgdb_begin_solver(j->db);
1791  
1792  	switch (j->type) {
1793  	case PKG_JOBS_AUTOREMOVE:
1794  		ret = jobs_solve_autoremove(j);
1795  		break;
1796  	case PKG_JOBS_DEINSTALL:
1797  		ret = jobs_solve_deinstall(j);
1798  		break;
1799  	case PKG_JOBS_UPGRADE:
1800  	case PKG_JOBS_INSTALL:
1801  		ret = jobs_solve_install_upgrade(j);
1802  		break;
1803  	case PKG_JOBS_FETCH:
1804  		ret = jobs_solve_fetch(j);
1805  		break;
1806  	default:
1807  		pkgdb_end_solver(j->db);
1808  		return (EPKG_FATAL);
1809  	}
1810  
1811  	if (ret == EPKG_OK) {
1812  		const char *cudf_solver;
1813  
1814  		cudf_solver = pkg_object_string(pkg_config_get("CUDF_SOLVER"));
1815  		if (cudf_solver != NULL) {
1816  			ret = solve_with_external_cudf_solver(j, cudf_solver);
1817  		} else {
1818  			ret = solve_with_sat_solver(j);
1819  		}
1820  	}
1821  
1822  	if (j->type == PKG_JOBS_DEINSTALL && j->solved)
1823  		pkg_jobs_set_deinstall_reasons(j);
1824  
1825  	pkgdb_end_solver(j->db);
1826  
1827  	return (ret);
1828  }
1829  
1830  int
1831  pkg_jobs_solve(struct pkg_jobs *j)
1832  {
1833  	int ret;
1834  
1835  	if (j->system_shlibs.len == 0) {
1836  		/* If /usr/bin/uname is in the pkg database, we are targeting
1837  		 * a pkgbase system and should rely on the pkgbase packages to
1838  		 * provide system shlibs. */
1839  		if (!pkgdb_file_exists(j->db, "/usr/bin/uname")) {
1840  			ret = scan_system_shlibs(&j->system_shlibs, ctx.pkg_rootdir);
1841  			if (ret == EPKG_NOCOMPAT32) {
1842  				j->ignore_compat32 = true;
1843  			} else if (ret != EPKG_OK) {
1844  				return (ret);
1845  			}
1846  		}
1847  	}
1848  
1849  	ret = pkg_jobs_run_solver(j);
1850  	if (ret != EPKG_OK)
1851  		return (ret);
1852  
1853  	/*
1854  	 * We can avoid asking the user for confirmation twice in the case of
1855  	 * conflicts if we can check for and solve conflicts without first
1856  	 * needing to fetch.
1857  	 */
1858  	vec_foreach(j->jobs, i) {
1859  		struct pkg *p;
1860  
1861  		p = ((struct pkg_solved *)j->jobs.d[i])->items[0]->pkg;
1862  		if (p->type != PKG_REMOTE)
1863  			continue;
1864  
1865  		if (pkgdb_ensure_loaded(j->db, p, PKG_LOAD_FILES|PKG_LOAD_DIRS)
1866  				== EPKG_FATAL) {
1867  			j->need_fetch = true;
1868  			break;
1869  		}
1870  	}
1871  
1872  	if (j->solved && !j->need_fetch && j->type != PKG_JOBS_FETCH) {
1873  		int rc;
1874  		bool has_conflicts = false;
1875  		do {
1876  			j->conflicts_registered = 0;
1877  			rc = pkg_jobs_check_conflicts(j);
1878  			if (rc == EPKG_CONFLICT) {
1879  				/* Cleanup results */
1880  				vec_free_and_free(&j->jobs, free);
1881  				has_conflicts = true;
1882  				pkg_jobs_solve(j);
1883  			}
1884  			else if (rc == EPKG_OK && !has_conflicts) {
1885  				break;
1886  			}
1887  		} while (j->conflicts_registered > 0);
1888  	}
1889  
1890  	return (ret);
1891  }
1892  
1893  int
1894  pkg_jobs_count(struct pkg_jobs *j)
1895  {
1896  	assert(j != NULL);
1897  
1898  	return (j->jobs.len);
1899  }
1900  
1901  int
1902  pkg_jobs_total(struct pkg_jobs *j)
1903  {
1904  	assert(j != NULL);
1905  
1906  	return (j->total);
1907  }
1908  
1909  pkg_jobs_t
1910  pkg_jobs_type(struct pkg_jobs *j)
1911  {
1912  	assert(j != NULL);
1913  
1914  	return (j->type);
1915  }
1916  
1917  static int
1918  pkg_jobs_handle_install(struct pkg_solved *ps, struct pkg_jobs *j)
1919  {
1920  	struct pkg *new, *old;
1921  	struct pkg_job_request *req;
1922  	char path[MAXPATHLEN], *target;
1923  	int flags = 0;
1924  	int retcode = EPKG_FATAL;
1925  
1926  	dbg(2, "begin %s", __func__);
1927  	/*
1928  	 * For a split upgrade, pass along the old package even though it's
1929  	 * already deleted, since we need it in order to merge configuration
1930  	 * file changes.
1931  	 */
1932  	new = ps->items[0]->pkg;
1933  	old = NULL;
1934  	if (ps->items[1] != NULL)
1935  		old = ps->items[1]->pkg;
1936  	else if (ps->type == PKG_SOLVED_UPGRADE_INSTALL)
1937  		old = ps->xlink->items[0]->pkg;
1938  
1939  	req = pkghash_get_value(j->request_add, new->uid);
1940  	if (req != NULL && req->item->jp != NULL &&
1941  			(req->item->jp->flags & PKG_PATTERN_FLAG_FILE)) {
1942  		/*
1943  		 * We have package as a file, set special repository name
1944  		 */
1945  		target = req->item->jp->path;
1946  		free(new->reponame);
1947  		new->reponame = xstrdup("local file");
1948  	}
1949  	else {
1950  		pkg_snprintf(path, sizeof(path), "%R", new);
1951  		if (*path != '/')
1952  			pkg_repo_cached_name(new, path, sizeof(path));
1953  		target = path;
1954  	}
1955  
1956  	if (old != NULL)
1957  		new->old_version = xstrdup(old->version);
1958  
1959  	if ((j->flags & PKG_FLAG_FORCE) == PKG_FLAG_FORCE)
1960  		flags |= PKG_ADD_FORCE;
1961  	if ((j->flags & PKG_FLAG_NOSCRIPT) == PKG_FLAG_NOSCRIPT)
1962  		flags |= PKG_ADD_NOSCRIPT;
1963  	if ((j->flags & PKG_FLAG_FORCE_MISSING) == PKG_FLAG_FORCE_MISSING)
1964  		flags |= PKG_ADD_FORCE_MISSING;
1965  	if ((j->flags & PKG_FLAG_REGISTER_ONLY) == PKG_FLAG_REGISTER_ONLY)
1966  		flags |= PKG_ADD_REGISTER_ONLY;
1967  	if (ps->type != PKG_SOLVED_INSTALL) {
1968  		flags |= PKG_ADD_UPGRADE;
1969  		if (ps->type == PKG_SOLVED_UPGRADE_INSTALL)
1970  			flags |= PKG_ADD_SPLITTED_UPGRADE;
1971  	}
1972  	if (new->automatic ||
1973  	    ((j->flags & PKG_FLAG_AUTOMATIC) == PKG_FLAG_AUTOMATIC &&
1974  	    ps->type == PKG_SOLVED_INSTALL))
1975  		flags |= PKG_ADD_AUTOMATIC;
1976  
1977  	// Treat installs where there is already a local package (e.g. a forced install)
1978  	// like an upgrade to handle config merging properly.
1979  	if (old == NULL) {
1980  		old = pkg_jobs_universe_get_local(j->universe, new->uid, 0);
1981  	}
1982  
1983  	if (new->type == PKG_GROUP_REMOTE)
1984  		retcode = pkg_add_group(new);
1985  	else if (old != NULL)
1986  		retcode = pkg_add_upgrade(j->db, target, flags, NULL, new, old, &j->triggers);
1987  	else
1988  		retcode = pkg_add_from_remote(j->db, target, flags, NULL, new, &j->triggers);
1989  
1990  	dbg(2, "end %s:", __func__);
1991  	return (retcode);
1992  }
1993  
1994  static int
1995  pkg_jobs_handle_delete(struct pkg_solved *ps, struct pkg_jobs *j)
1996  {
1997  	struct pkg *rpkg;
1998  	int flags;
1999  
2000  	rpkg = NULL;
2001  	flags = 0;
2002  	if ((j->flags & PKG_FLAG_NOSCRIPT) != 0)
2003  		flags |= PKG_DELETE_NOSCRIPT;
2004  	if ((j->flags & PKG_FLAG_KEEPFILES) != 0)
2005  		flags |= PKG_DELETE_KEEPFILES;
2006  	if (ps->type == PKG_SOLVED_UPGRADE_REMOVE) {
2007  		flags |= PKG_DELETE_UPGRADE;
2008  		rpkg = ps->xlink->items[0]->pkg;
2009  	}
2010  	return (pkg_delete(ps->items[0]->pkg, rpkg, j->db, flags,
2011  	    &j->triggers));
2012  }
2013  
2014  static int
2015  pkg_jobs_execute(struct pkg_jobs *j)
2016  {
2017  	dbg(1, "execute");
2018  	struct pkg *p;
2019  	int retcode = EPKG_FATAL;
2020  	pkg_plugin_hook_t pre, post;
2021  	size_t total_actions;
2022  	size_t current_action = 0;
2023  
2024  //j->triggers.cleanup = triggers_load(true);
2025  	if (j->type == PKG_JOBS_INSTALL) {
2026  		pre = PKG_PLUGIN_HOOK_PRE_INSTALL;
2027  		post = PKG_PLUGIN_HOOK_POST_INSTALL;
2028  	}
2029  	else if (j->type == PKG_JOBS_UPGRADE) {
2030  		pre = PKG_PLUGIN_HOOK_PRE_UPGRADE;
2031  		post = PKG_PLUGIN_HOOK_POST_UPGRADE;
2032  	}
2033  	else if (j->type == PKG_JOBS_AUTOREMOVE){
2034  		pre = PKG_PLUGIN_HOOK_PRE_AUTOREMOVE;
2035  		post = PKG_PLUGIN_HOOK_POST_AUTOREMOVE;
2036  	}
2037  	else {
2038  		pre = PKG_PLUGIN_HOOK_PRE_DEINSTALL;
2039  		post = PKG_PLUGIN_HOOK_POST_DEINSTALL;
2040  	}
2041  
2042  	if (j->flags & PKG_FLAG_SKIP_INSTALL)
2043  		return (EPKG_OK);
2044  
2045  	if (j->flags & PKG_FLAG_DRY_RUN)
2046  		return (EPKG_OK);
2047  
2048  	retcode = pkgdb_upgrade_lock(j->db, PKGDB_LOCK_ADVISORY,
2049  			PKGDB_LOCK_EXCLUSIVE);
2050  	if (retcode != EPKG_OK)
2051  		return (retcode);
2052  
2053  	pkg_plugins_hook_run(pre, j, j->db);
2054  
2055  	retcode = pkg_jobs_schedule(j);
2056  	if (retcode != EPKG_OK)
2057  		return (retcode);
2058  
2059  	total_actions = j->jobs.len;
2060  	vec_foreach(j->jobs, i) {
2061  		struct pkg_solved *ps = j->jobs.d[i];
2062  
2063  		pkg_emit_new_action(++current_action, total_actions);
2064  		switch (ps->type) {
2065  		case PKG_SOLVED_DELETE:
2066  			if ((j->flags & PKG_FLAG_FORCE) == 0) {
2067  				p = ps->items[0]->pkg;
2068  				if (p->vital) {
2069  					pkg_emit_error(
2070  					    "Cannot delete vital package: %s!", p->name);
2071  					pkg_emit_error(
2072  					    "If you are sure you want to remove %s", p->name);
2073  					pkg_emit_error(
2074  					    "unset the 'vital' flag with: pkg set -v 0 %s", p->name);
2075  					retcode = EPKG_FATAL;
2076  					goto cleanup;
2077  				}
2078  				if (STREQ(p->name, "pkg") ||
2079  				    STREQ(p->name, "pkg-devel")) {
2080  					if (j->patterns == NULL ||
2081  					    j->patterns->match == MATCH_ALL)
2082  						continue;
2083  					pkg_emit_error(
2084  					    "Cannot delete pkg itself without force flag");
2085  					retcode = EPKG_FATAL;
2086  					goto cleanup;
2087  				}
2088  			}
2089  			/* FALLTHROUGH */
2090  		case PKG_SOLVED_UPGRADE_REMOVE:
2091  			retcode = pkg_jobs_handle_delete(ps, j);
2092  			if (retcode != EPKG_OK)
2093  				goto cleanup;
2094  			break;
2095  		case PKG_SOLVED_INSTALL:
2096  		case PKG_SOLVED_UPGRADE_INSTALL:
2097  		case PKG_SOLVED_UPGRADE:
2098  			retcode = pkg_jobs_handle_install(ps, j);
2099  			if (retcode != EPKG_OK)
2100  				goto cleanup;
2101  			break;
2102  		case PKG_SOLVED_FETCH:
2103  			retcode = EPKG_FATAL;
2104  			pkg_emit_error("internal error: bad job type");
2105  			goto cleanup;
2106  		}
2107  
2108  	}
2109  
2110  	pkg_plugins_hook_run(post, j, j->db);
2111  	triggers_execute(&j->triggers);
2112  
2113  cleanup:
2114  	pkgdb_release_lock(j->db, PKGDB_LOCK_EXCLUSIVE);
2115  	dbg(1, "execute done");
2116  
2117  	return (retcode);
2118  }
2119  
2120  static void
2121  pkg_jobs_cancel(struct pkg_jobs *j)
2122  {
2123  	pkgdb_release_lock(j->db, PKGDB_LOCK_ADVISORY);
2124  }
2125  
2126  int
2127  pkg_jobs_apply(struct pkg_jobs *j)
2128  {
2129  	int rc;
2130  	bool has_conflicts = false;
2131  
2132  	if (!j->solved) {
2133  		pkg_emit_error("The jobs hasn't been solved");
2134  		return (EPKG_FATAL);
2135  	}
2136  
2137  	switch (j->type) {
2138  	case PKG_JOBS_INSTALL:
2139  	case PKG_JOBS_UPGRADE:
2140  	case PKG_JOBS_DEINSTALL:
2141  	case PKG_JOBS_AUTOREMOVE:
2142  		if (j->need_fetch) {
2143  			pkg_plugins_hook_run(PKG_PLUGIN_HOOK_PRE_FETCH, j, j->db);
2144  			rc = pkg_jobs_fetch(j);
2145  			pkg_plugins_hook_run(PKG_PLUGIN_HOOK_POST_FETCH, j, j->db);
2146  			if (rc == EPKG_OK) {
2147  				/* Check local conflicts in the first run */
2148  				if (j->solved == 1) {
2149  					do {
2150  						j->conflicts_registered = 0;
2151  						rc = pkg_jobs_check_conflicts(j);
2152  						if (rc == EPKG_CONFLICT) {
2153  							/* Cleanup results */
2154  							vec_free_and_free(&j->jobs, free);
2155  							has_conflicts = true;
2156  							rc = pkg_jobs_solve(j);
2157  						}
2158  						else if (rc == EPKG_OK && !has_conflicts) {
2159  							rc = pkg_jobs_execute(j);
2160  							break;
2161  						}
2162  					} while (j->conflicts_registered > 0);
2163  
2164  					if (has_conflicts) {
2165  						return (EPKG_CONFLICT);
2166  					}
2167  				}
2168  				else {
2169  					/* Not the first run, conflicts are resolved already */
2170  					rc = pkg_jobs_execute(j);
2171  				}
2172  			}
2173  			else if (rc == EPKG_CANCEL) {
2174  				pkg_jobs_cancel(j);
2175  			}
2176  		}
2177  		else {
2178  			rc = pkg_jobs_execute(j);
2179  		}
2180  
2181  		break;
2182  	case PKG_JOBS_FETCH:
2183  		pkg_plugins_hook_run(PKG_PLUGIN_HOOK_PRE_FETCH, j, j->db);
2184  		rc = pkg_jobs_fetch(j);
2185  		pkg_plugins_hook_run(PKG_PLUGIN_HOOK_POST_FETCH, j, j->db);
2186  		break;
2187  	default:
2188  		rc = EPKG_FATAL;
2189  		pkg_emit_error("bad jobs argument");
2190  		break;
2191  	}
2192  
2193  	return (rc);
2194  }
2195  
2196  
2197  static int
2198  pkg_jobs_fetch(struct pkg_jobs *j)
2199  {
2200  	struct pkg *p = NULL;
2201  	struct stat st;
2202  	struct statvfs fs;
2203  	int64_t dlsize = 0, fs_avail = -1;
2204  	const char *cachedir = NULL;
2205  	char cachedpath[MAXPATHLEN];
2206  	bool mirror = (j->flags & PKG_FLAG_FETCH_MIRROR) ? true : false;
2207  	bool symlink = (j->flags & PKG_FLAG_FETCH_SYMLINK) ? true : false;
2208  	int retcode;
2209  
2210  
2211  	if (j->destdir == NULL || !mirror)
2212  		cachedir = ctx.cachedir;
2213  	else
2214  		cachedir = j->destdir;
2215  
2216  	/* check for available size to fetch */
2217  	vec_foreach(j->jobs, i) {
2218  		struct pkg_solved *ps = j->jobs.d[i];
2219  		if (ps->type != PKG_SOLVED_DELETE && ps->type != PKG_SOLVED_UPGRADE_REMOVE) {
2220  			p = ps->items[0]->pkg;
2221  			if (p->type != PKG_REMOTE)
2222  				continue;
2223  
2224  			if (mirror) {
2225  				snprintf(cachedpath, sizeof(cachedpath),
2226  				   "%s/%s", cachedir, p->repopath);
2227  			}
2228  			else
2229  				pkg_repo_cached_name(p, cachedpath, sizeof(cachedpath));
2230  
2231  			if (stat(cachedpath, &st) == -1)
2232  				dlsize += p->pkgsize;
2233  			else
2234  				dlsize += p->pkgsize - st.st_size;
2235  		}
2236  	}
2237  
2238  	if (dlsize == 0)
2239  		return (EPKG_OK);
2240  
2241  	for (;;) {
2242  		if (statvfs(cachedir, &fs) == 0) break;
2243  		if (errno == EINTR) continue;
2244  		if (errno == ENOENT) {
2245  			if (pkg_mkdirs(cachedir) != EPKG_OK) return EPKG_FATAL;
2246  			continue;
2247  		}
2248  		pkg_emit_errno("statvfs", cachedir);
2249  		return EPKG_FATAL;
2250  	}
2251  	fs_avail = fs.f_frsize * (int64_t)fs.f_bavail;
2252  
2253  	if (fs_avail != -1 && dlsize > fs_avail) {
2254  		char dlsz[9], fsz[9];
2255  
2256  		humanize_number(dlsz, sizeof(dlsz), dlsize, "B",
2257  		    HN_AUTOSCALE, HN_IEC_PREFIXES);
2258  		humanize_number(fsz, sizeof(fsz), fs_avail, "B",
2259  		    HN_AUTOSCALE, HN_IEC_PREFIXES);
2260  		pkg_emit_error("Not enough space in %s, needed %s available %s",
2261  		    cachedir, dlsz, fsz);
2262  		return (EPKG_FATAL);
2263  	}
2264  
2265  	if ((j->flags & PKG_FLAG_DRY_RUN) == PKG_FLAG_DRY_RUN)
2266  		return (EPKG_OK); /* don't download anything */
2267  
2268  	/* Fetch */
2269  	vec_foreach(j->jobs, i) {
2270  		struct pkg_solved *ps = j->jobs.d[i];
2271  		if (ps->type != PKG_SOLVED_DELETE && ps->type != PKG_SOLVED_UPGRADE_REMOVE) {
2272  			p = ps->items[0]->pkg;
2273  			if (p->type != PKG_REMOTE)
2274  				continue;
2275  
2276  			if (mirror) {
2277  				retcode = pkg_repo_mirror_package(p, cachedir, symlink);
2278  				if (retcode != EPKG_OK)
2279  					return (retcode);
2280  			}
2281  			else {
2282  				retcode = pkg_repo_fetch_package(p);
2283  				if (retcode != EPKG_OK)
2284  					return (retcode);
2285  			}
2286  		}
2287  	}
2288  
2289  	return (EPKG_OK);
2290  }
2291  
2292  #ifdef HAVE_CHFLAGSAT
2293  #if defined(UF_NOUNLINK)
2294  #define NOCHANGESFLAGS	\
2295      (UF_IMMUTABLE | UF_APPEND | UF_NOUNLINK | \
2296       SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)
2297  #else
2298  #define NOCHANGESFLAGS	\
2299      (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
2300  #endif
2301  #define SYSTEM_FLAGS	(SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)
2302  #endif
2303  
2304  /*
2305   * Check chflags restrictions from jail or securelevel.
2306   * Returns:
2307   *   0  - all chflags operations allowed
2308   *   1  - jail restricts chflags (no flags allowed)
2309   *   2  - securelevel restricts system flags (SF_*)
2310   */
2311  static int
2312  pkg_jobs_chflags_restricted(void)
2313  {
2314  #ifdef HAVE_CHFLAGSAT
2315  #ifdef HAVE_LIBJAIL
2316  	int jailed = 0;
2317  	size_t len = sizeof(jailed);
2318  
2319  	if (sysctlbyname("security.jail.jailed", &jailed, &len,
2320  	    NULL, 0) != -1 && jailed == 1) {
2321  		int allowed = 0;
2322  		len = sizeof(allowed);
2323  		if (sysctlbyname("security.jail.chflags_allowed", &allowed,
2324  		    &len, NULL, 0) == -1 || allowed == 0)
2325  			return (1);
2326  	}
2327  #endif
2328  	int securelevel = -1;
2329  	size_t slen = sizeof(securelevel);
2330  	if (sysctlbyname("kern.securelevel", &securelevel, &slen,
2331  	    NULL, 0) != -1 && securelevel >= 1)
2332  		return (2);
2333  #endif
2334  	return (0);
2335  }
2336  
2337  static int
2338  pkg_jobs_check_chflags(struct pkg_jobs *j)
2339  {
2340  	struct pkg_file *f;
2341  	struct pkg_dir *d;
2342  	int restriction;
2343  	u_long mask;
2344  
2345  #ifdef HAVE_CHFLAGSAT
2346  	restriction = pkg_jobs_chflags_restricted();
2347  	if (restriction == 0)
2348  		return (EPKG_OK);
2349  
2350  	/* jail without chflags: no flags at all allowed */
2351  	/* securelevel >= 1: only system flags (SF_*) restricted */
2352  	mask = (restriction == 1) ? NOCHANGESFLAGS : SYSTEM_FLAGS;
2353  
2354  	vec_foreach(j->jobs, i) {
2355  		struct pkg_solved *ps = j->jobs.d[i];
2356  		struct pkg *p = ps->items[0]->pkg;
2357  
2358  		if (p->type != PKG_REMOTE)
2359  			pkgdb_ensure_loaded(j->db, p,
2360  			    PKG_LOAD_FILES|PKG_LOAD_DIRS);
2361  
2362  		f = NULL;
2363  		while (pkg_files(p, &f) == EPKG_OK) {
2364  			if (f->fflags & mask)
2365  				goto restricted;
2366  		}
2367  		d = NULL;
2368  		while (pkg_dirs(p, &d) == EPKG_OK) {
2369  			if (d->fflags & mask)
2370  				goto restricted;
2371  		}
2372  		continue;
2373  restricted:
2374  		if (restriction == 1)
2375  			pkg_emit_error("Package %s has files with flags "
2376  			    "that cannot be managed in this jail. "
2377  			    "Set allow.chflags in the jail configuration.",
2378  			    p->name);
2379  		else
2380  			pkg_emit_error("Package %s has files with system "
2381  			    "flags (schg, sunlnk, ...) that cannot be "
2382  			    "managed at securelevel %d. Lower the "
2383  			    "securelevel to -1 to allow this operation.",
2384  			    p->name, 1);
2385  		return (EPKG_FATAL);
2386  	}
2387  #endif
2388  	return (EPKG_OK);
2389  }
2390  
2391  static int
2392  pkg_jobs_check_conflicts(struct pkg_jobs *j)
2393  {
2394  	struct pkg *p = NULL;
2395  	int ret = EPKG_OK, res, added = 0;
2396  
2397  	pkg_emit_integritycheck_begin();
2398  	j->conflicts_registered = 0;
2399  
2400  	vec_foreach(j->jobs, i) {
2401  		struct pkg_solved *ps = j->jobs.d[i];
2402  		if (ps->type == PKG_SOLVED_DELETE || ps->type == PKG_SOLVED_UPGRADE_REMOVE) {
2403  			continue;
2404  		}
2405  		else {
2406  			p = ps->items[0]->pkg;
2407  
2408  			if (p->type == PKG_REMOTE)
2409  				pkgdb_ensure_loaded(j->db, p, PKG_LOAD_FILES|PKG_LOAD_DIRS);
2410  		}
2411  		if ((res = pkg_conflicts_append_chain(ps->items[0], j)) != EPKG_OK)
2412  			ret = res;
2413  		else
2414  			added ++;
2415  	}
2416  
2417  	dbg(1, "check integrity for %d items added", added);
2418  
2419  	pkg_emit_integritycheck_finished(j->conflicts_registered);
2420  	if (j->conflicts_registered > 0)
2421  		ret = EPKG_CONFLICT;
2422  
2423  	if (ret == EPKG_OK) {
2424  		res = pkg_jobs_check_chflags(j);
2425  		if (res != EPKG_OK)
2426  			ret = res;
2427  	}
2428  
2429  	return (ret);
2430  }
2431  
2432  bool
2433  pkg_jobs_has_lockedpkgs(struct pkg_jobs *j)
2434  {
2435  
2436  	return j->lockedpkgs ? true : false;
2437  }
2438  
2439  static void
2440  pkg_jobs_visit_lockedpkgs(const void * node, VISIT v, int i __unused)
2441  {
2442  
2443  	if (v == postorder || v == leaf) {
2444  		pkgs_job_lockedpkg->locked_pkg_cb(*(struct pkg **)node,
2445  		    pkgs_job_lockedpkg->context);
2446  	}
2447  }
2448  
2449  void
2450  pkg_jobs_iter_lockedpkgs(struct pkg_jobs *j, locked_pkgs_cb cb, void * ctx)
2451  {
2452  	struct pkg_jobs_locked foo;
2453  
2454  	foo.locked_pkg_cb = cb;
2455  	foo.context = ctx;
2456  	pkgs_job_lockedpkg = &foo;
2457  
2458  	twalk(j->lockedpkgs, pkg_jobs_visit_lockedpkgs);
2459  }