1 /* opkg_conf.c - the opkg package management system
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
6 Copyright (C) 2001 University of Southern California
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
22 #include <sys/types.h>
28 #include "opkg_conf.h"
32 #include "sprintf_alloc.h"
33 #include "opkg_message.h"
34 #include "file_util.h"
35 #include "opkg_defines.h"
36 #include "libbb/libbb.h"
39 static char *lock_file = NULL;
41 static opkg_conf_t _conf;
42 opkg_conf_t *conf = &_conf;
47 opkg_option_t options[] = {
48 {"cache", OPKG_OPT_TYPE_STRING, &_conf.cache},
49 {"force_defaults", OPKG_OPT_TYPE_BOOL, &_conf.force_defaults},
50 {"force_maintainer", OPKG_OPT_TYPE_BOOL, &_conf.force_maintainer},
51 {"force_depends", OPKG_OPT_TYPE_BOOL, &_conf.force_depends},
52 {"force_overwrite", OPKG_OPT_TYPE_BOOL, &_conf.force_overwrite},
53 {"force_downgrade", OPKG_OPT_TYPE_BOOL, &_conf.force_downgrade},
54 {"force_reinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_reinstall},
55 {"force_space", OPKG_OPT_TYPE_BOOL, &_conf.force_space},
56 {"force_postinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_postinstall},
57 {"force_checksum", OPKG_OPT_TYPE_BOOL, &_conf.force_checksum},
58 {"check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature},
59 {"ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy},
60 {"http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy},
61 {"no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy},
62 {"test", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
63 {"noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
64 {"download_only", OPKG_OPT_TYPE_BOOL, &_conf.download_only},
65 {"nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps},
66 {"nocase", OPKG_OPT_TYPE_BOOL, &_conf.nocase},
67 {"offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root},
68 {"overlay_root", OPKG_OPT_TYPE_STRING, &_conf.overlay_root},
69 {"proxy_passwd", OPKG_OPT_TYPE_STRING, &_conf.proxy_passwd},
70 {"proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user},
71 {"query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all},
72 {"size", OPKG_OPT_TYPE_BOOL, &_conf.size},
73 {"tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir},
74 {"verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity},
75 #if defined(HAVE_OPENSSL)
76 {"signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file},
77 {"signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path},
79 #if defined(HAVE_PATHFINDER)
80 {"check_x509_path", OPKG_OPT_TYPE_BOOL, &_conf.check_x509_path},
82 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
83 {"ssl_engine", OPKG_OPT_TYPE_STRING, &_conf.ssl_engine},
84 {"ssl_cert", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert},
85 {"ssl_cert_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert_type},
86 {"ssl_key", OPKG_OPT_TYPE_STRING, &_conf.ssl_key},
87 {"ssl_key_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_type},
88 {"ssl_key_passwd", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_passwd},
89 {"ssl_ca_file", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_file},
90 {"ssl_ca_path", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_path},
91 {"ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL,
92 &_conf.ssl_dont_verify_peer},
97 static int resolve_pkg_dest_list(void)
99 nv_pair_list_elt_t *iter;
104 for (iter = nv_pair_list_first(&conf->tmp_dest_list); iter;
105 iter = nv_pair_list_next(&conf->tmp_dest_list, iter)) {
106 nv_pair = (nv_pair_t *) iter->data;
108 if (conf->offline_root) {
109 sprintf_alloc(&root_dir, "%s%s", conf->offline_root,
112 root_dir = xstrdup(nv_pair->value);
116 pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name,
117 root_dir, conf->lists_dir);
120 if (conf->default_dest == NULL)
121 conf->default_dest = dest;
123 if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) {
124 conf->default_dest = dest;
125 conf->restrict_to_default_dest = 1;
129 if (conf->dest_str && !conf->restrict_to_default_dest) {
130 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str);
137 static int opkg_conf_set_option(const char *name, const char *value)
141 while (options[i].name) {
142 if (strcmp(options[i].name, name) == 0) {
143 switch (options[i].type) {
144 case OPKG_OPT_TYPE_BOOL:
145 if (*(int *)options[i].value) {
147 "Duplicate boolean option %s, "
148 "leaving this option on.\n",
152 *((int *const)options[i].value) = 1;
154 case OPKG_OPT_TYPE_INT:
156 if (*(int *)options[i].value) {
158 "Duplicate option %s, "
159 "using first seen value \"%d\".\n",
165 *((int *const)options[i].value) =
170 "Option %s needs an argument\n",
174 case OPKG_OPT_TYPE_STRING:
176 if (*(char **)options[i].value) {
178 "Duplicate option %s, "
179 "using first seen value \"%s\".\n",
181 *((char **)options[i].
185 *((char **const)options[i].value) =
190 "Option %s needs an argument\n",
199 opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value);
204 opkg_conf_parse_file(const char *filename,
205 pkg_src_list_t * pkg_src_list,
206 pkg_src_list_t * dist_src_list)
211 regex_t valid_line_re, comment_re;
212 #define regmatch_size 14
213 regmatch_t regmatch[regmatch_size];
215 file = fopen(filename, "r");
217 opkg_perror(ERROR, "Failed to open %s", filename);
222 opkg_msg(INFO, "Loading conf file %s.\n", filename);
224 err = xregcomp(&comment_re,
225 "^[[:space:]]*(#.*|[[:space:]]*)$", REG_EXTENDED);
229 err = xregcomp(&valid_line_re,
230 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
231 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
232 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
233 "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$",
240 char *type, *name, *value, *extra;
244 line = file_read_line_alloc(file);
248 if (regexec(&comment_re, line, 0, 0, 0) == 0)
251 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) ==
253 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
254 filename, line_num, line);
258 /* This has to be so ugly to deal with optional quotation marks */
259 if (regmatch[2].rm_so > 0) {
260 type = xstrndup(line + regmatch[2].rm_so,
261 regmatch[2].rm_eo - regmatch[2].rm_so);
263 type = xstrndup(line + regmatch[3].rm_so,
264 regmatch[3].rm_eo - regmatch[3].rm_so);
267 if (regmatch[5].rm_so > 0) {
268 name = xstrndup(line + regmatch[5].rm_so,
269 regmatch[5].rm_eo - regmatch[5].rm_so);
271 name = xstrndup(line + regmatch[6].rm_so,
272 regmatch[6].rm_eo - regmatch[6].rm_so);
275 if (regmatch[8].rm_so > 0) {
276 value = xstrndup(line + regmatch[8].rm_so,
277 regmatch[8].rm_eo - regmatch[8].rm_so);
279 value = xstrndup(line + regmatch[9].rm_so,
280 regmatch[9].rm_eo - regmatch[9].rm_so);
284 if (regmatch[11].rm_so > 0) {
285 if (regmatch[13].rm_so > 0
286 && regmatch[13].rm_so != regmatch[13].rm_eo)
288 xstrndup(line + regmatch[11].rm_so,
292 extra = xstrndup(line + regmatch[11].rm_so,
297 if (regmatch[13].rm_so != regmatch[13].rm_eo
298 && strncmp(type, "dist", 4) != 0) {
300 "%s:%d: Ignoring config line with trailing garbage: `%s'\n",
301 filename, line_num, line);
304 /* We use the conf->tmp_dest_list below instead of
305 conf->pkg_dest_list because we might encounter an
306 offline_root option later and that would invalidate the
307 directories we would have computed in
308 pkg_dest_list_init. (We do a similar thing with
309 tmp_src_nv_pair_list for sake of symmetry.) */
310 if (strcmp(type, "option") == 0) {
311 opkg_conf_set_option(name, value);
312 } else if (strcmp(type, "dist") == 0) {
313 if (!nv_pair_list_find
314 ((nv_pair_list_t *) dist_src_list, name)) {
315 pkg_src_list_append(dist_src_list, name,
319 "Duplicate dist declaration (%s %s). "
320 "Skipping.\n", name, value);
322 } else if (strcmp(type, "dist/gz") == 0) {
323 if (!nv_pair_list_find
324 ((nv_pair_list_t *) dist_src_list, name)) {
325 pkg_src_list_append(dist_src_list, name,
329 "Duplicate dist declaration (%s %s). "
330 "Skipping.\n", name, value);
332 } else if (strcmp(type, "src") == 0) {
333 if (!nv_pair_list_find
334 ((nv_pair_list_t *) pkg_src_list, name)) {
335 pkg_src_list_append(pkg_src_list, name,
339 "Duplicate src declaration (%s %s). "
340 "Skipping.\n", name, value);
342 } else if (strcmp(type, "src/gz") == 0) {
343 if (!nv_pair_list_find
344 ((nv_pair_list_t *) pkg_src_list, name)) {
345 pkg_src_list_append(pkg_src_list, name,
349 "Duplicate src declaration (%s %s). "
350 "Skipping.\n", name, value);
352 } else if (strcmp(type, "dest") == 0) {
353 nv_pair_list_append(&conf->tmp_dest_list, name,
355 } else if (strcmp(type, "lists_dir") == 0) {
356 conf->lists_dir = xstrdup(value);
357 } else if (strcmp(type, "arch") == 0) {
359 "Supported arch %s priority (%s)\n",
363 "No priority given for architecture %s,"
364 "defaulting to 10\n", name);
365 value = xstrdup("10");
367 nv_pair_list_append(&conf->arch_list, name,
371 "%s:%d: Ignoring invalid line: `%s'\n",
372 filename, line_num, line);
387 regfree(&valid_line_re);
389 regfree(&comment_re);
391 if (fclose(file) == EOF) {
392 opkg_perror(ERROR, "Couldn't close %s", filename);
399 int opkg_conf_write_status_files(void)
401 pkg_dest_list_elt_t *iter;
410 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
411 dest = (pkg_dest_t *) iter->data;
413 dest->status_fp = fopen(dest->status_file_name, "w");
414 if (dest->status_fp == NULL && errno != EROFS) {
415 opkg_perror(ERROR, "Can't open status file %s",
416 dest->status_file_name);
421 all = pkg_vec_alloc();
422 pkg_hash_fetch_available(all);
424 for (i = 0; i < all->len; i++) {
426 /* We don't need most uninstalled packages in the status file */
427 if (pkg->state_status == SS_NOT_INSTALLED
428 && (pkg->state_want == SW_UNKNOWN
429 || (pkg->state_want == SW_DEINSTALL
430 && pkg->state_flag != SF_HOLD)
431 || pkg->state_want == SW_PURGE)) {
434 if (pkg->dest == NULL) {
436 "Internal error: package %s has a NULL dest\n",
440 if (pkg->dest->status_fp)
441 pkg_print_status(pkg, pkg->dest->status_fp);
446 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
447 dest = (pkg_dest_t *) iter->data;
448 if (dest->status_fp && fclose(dest->status_fp) == EOF) {
449 opkg_perror(ERROR, "Couldn't close %s",
450 dest->status_file_name);
458 char *root_filename_alloc(char *filename)
461 sprintf_alloc(&root_filename, "%s%s",
462 (conf->offline_root ? conf->offline_root : ""), filename);
463 return root_filename;
466 static int glob_errfunc(const char *epath, int eerrno)
468 if (eerrno == ENOENT)
469 /* If leading dir does not exist, we get GLOB_NOMATCH. */
472 opkg_msg(ERROR, "glob failed for %s: %s\n", epath, strerror(eerrno));
476 int opkg_conf_init(void)
478 pkg_src_list_init(&conf->pkg_src_list);
479 pkg_src_list_init(&conf->dist_src_list);
480 pkg_dest_list_init(&conf->pkg_dest_list);
481 pkg_dest_list_init(&conf->tmp_dest_list);
482 nv_pair_list_init(&conf->arch_list);
487 int opkg_conf_load(void)
490 char *tmp, *tmp_dir_base, **tmp_val;
492 char *etc_opkg_conf_pattern;
494 conf->restrict_to_default_dest = 0;
495 conf->default_dest = NULL;
496 #if defined(HAVE_PATHFINDER)
497 conf->check_x509_path = 1;
500 if (!conf->offline_root)
501 conf->offline_root = xstrdup(getenv("OFFLINE_ROOT"));
503 if (conf->conf_file) {
505 if (stat(conf->conf_file, &st) == -1) {
506 opkg_perror(ERROR, "Couldn't stat %s", conf->conf_file);
509 if (opkg_conf_parse_file(conf->conf_file,
511 &conf->dist_src_list))
515 if (conf->offline_root)
516 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf",
519 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
520 if (conf_file_dir == NULL)
521 conf_file_dir = OPKG_CONF_DEFAULT_CONF_FILE_DIR;
522 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf",
526 memset(&globbuf, 0, sizeof(globbuf));
527 glob_ret = glob(etc_opkg_conf_pattern, 0, glob_errfunc, &globbuf);
528 if (glob_ret && glob_ret != GLOB_NOMATCH) {
529 free(etc_opkg_conf_pattern);
534 free(etc_opkg_conf_pattern);
536 for (i = 0; i < globbuf.gl_pathc; i++) {
537 if (globbuf.gl_pathv[i])
538 if (conf->conf_file &&
539 !strcmp(conf->conf_file, globbuf.gl_pathv[i]))
541 if (opkg_conf_parse_file(globbuf.gl_pathv[i],
543 &conf->dist_src_list) < 0) {
551 if (conf->offline_root)
552 sprintf_alloc(&lock_file, "%s/%s", conf->offline_root,
555 sprintf_alloc(&lock_file, "%s", OPKGLOCKFILE);
557 lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
559 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
563 if (lockf(lock_fd, F_TLOCK, (off_t) 0) == -1) {
564 opkg_perror(ERROR, "Could not lock %s", lock_file);
565 if (close(lock_fd) == -1)
566 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
573 tmp_dir_base = conf->tmp_dir;
575 tmp_dir_base = getenv("TMPDIR");
577 sprintf_alloc(&tmp, "%s/%s",
578 tmp_dir_base ? tmp_dir_base :
579 OPKG_CONF_DEFAULT_TMP_DIR_BASE, OPKG_CONF_TMP_DIR_SUFFIX);
582 conf->tmp_dir = mkdtemp(tmp);
583 if (conf->tmp_dir == NULL) {
584 opkg_perror(ERROR, "Creating temp dir %s failed", tmp);
589 hash_table_init("file-hash", &conf->file_hash,
590 OPKG_CONF_DEFAULT_HASH_LEN);
591 hash_table_init("obs-file-hash", &conf->obs_file_hash,
592 OPKG_CONF_DEFAULT_HASH_LEN / 16);
594 if (conf->lists_dir == NULL)
595 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
597 if (conf->offline_root) {
598 sprintf_alloc(&tmp, "%s/%s", conf->offline_root,
600 free(conf->lists_dir);
601 conf->lists_dir = tmp;
604 /* if no architectures were defined, then default all, noarch, and host architecture */
605 if (nv_pair_list_empty(&conf->arch_list)) {
606 nv_pair_list_append(&conf->arch_list, "all", "1");
607 nv_pair_list_append(&conf->arch_list, "noarch", "1");
608 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
611 /* Even if there is no conf file, we'll need at least one dest. */
612 if (nv_pair_list_empty(&conf->tmp_dest_list)) {
613 nv_pair_list_append(&conf->tmp_dest_list,
614 OPKG_CONF_DEFAULT_DEST_NAME,
615 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
618 if (resolve_pkg_dest_list())
621 nv_pair_list_deinit(&conf->tmp_dest_list);
626 free(conf->lists_dir);
629 hash_table_deinit(&conf->file_hash);
630 hash_table_deinit(&conf->obs_file_hash);
632 if (rmdir(conf->tmp_dir) == -1)
633 opkg_perror(ERROR, "Couldn't remove dir %s", conf->tmp_dir);
635 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
636 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
638 if (close(lock_fd) == -1)
639 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
641 if (unlink(lock_file) == -1)
642 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
649 pkg_src_list_deinit(&conf->pkg_src_list);
650 pkg_src_list_deinit(&conf->dist_src_list);
651 pkg_dest_list_deinit(&conf->pkg_dest_list);
652 nv_pair_list_deinit(&conf->arch_list);
654 for (i = 0; options[i].name; i++) {
655 if (options[i].type == OPKG_OPT_TYPE_STRING) {
656 tmp_val = (char **)options[i].value;
664 nv_pair_list_deinit(&conf->tmp_dest_list);
666 free(conf->dest_str);
668 free(conf->conf_file);
673 void opkg_conf_deinit(void)
682 free(conf->lists_dir);
685 free(conf->dest_str);
688 free(conf->conf_file);
690 pkg_src_list_deinit(&conf->pkg_src_list);
691 pkg_src_list_deinit(&conf->dist_src_list);
692 pkg_dest_list_deinit(&conf->pkg_dest_list);
693 nv_pair_list_deinit(&conf->arch_list);
695 for (i = 0; options[i].name; i++) {
696 if (options[i].type == OPKG_OPT_TYPE_STRING) {
697 tmp = (char **)options[i].value;
705 if (conf->verbosity >= DEBUG) {
706 hash_print_stats(&conf->pkg_hash);
707 hash_print_stats(&conf->file_hash);
708 hash_print_stats(&conf->obs_file_hash);
712 hash_table_deinit(&conf->file_hash);
713 hash_table_deinit(&conf->obs_file_hash);
716 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
717 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
719 if (close(lock_fd) == -1)
720 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
726 if (unlink(lock_file) == -1)
727 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);