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.
20 #include <sys/types.h>
26 #include "opkg_conf.h"
30 #include "sprintf_alloc.h"
31 #include "opkg_message.h"
32 #include "file_util.h"
33 #include "opkg_defines.h"
34 #include "libbb/libbb.h"
37 static char *lock_file
= NULL
;
39 static opkg_conf_t _conf
;
40 opkg_conf_t
*conf
= &_conf
;
45 opkg_option_t options
[] = {
46 {"cache", OPKG_OPT_TYPE_STRING
, &_conf
.cache
},
47 {"force_defaults", OPKG_OPT_TYPE_BOOL
, &_conf
.force_defaults
},
48 {"force_maintainer", OPKG_OPT_TYPE_BOOL
, &_conf
.force_maintainer
},
49 {"force_depends", OPKG_OPT_TYPE_BOOL
, &_conf
.force_depends
},
50 {"force_overwrite", OPKG_OPT_TYPE_BOOL
, &_conf
.force_overwrite
},
51 {"force_downgrade", OPKG_OPT_TYPE_BOOL
, &_conf
.force_downgrade
},
52 {"force_reinstall", OPKG_OPT_TYPE_BOOL
, &_conf
.force_reinstall
},
53 {"force_space", OPKG_OPT_TYPE_BOOL
, &_conf
.force_space
},
54 {"force_postinstall", OPKG_OPT_TYPE_BOOL
, &_conf
.force_postinstall
},
55 {"force_checksum", OPKG_OPT_TYPE_BOOL
, &_conf
.force_checksum
},
56 {"check_signature", OPKG_OPT_TYPE_BOOL
, &_conf
.check_signature
},
57 {"ftp_proxy", OPKG_OPT_TYPE_STRING
, &_conf
.ftp_proxy
},
58 {"http_proxy", OPKG_OPT_TYPE_STRING
, &_conf
.http_proxy
},
59 {"no_proxy", OPKG_OPT_TYPE_STRING
, &_conf
.no_proxy
},
60 {"test", OPKG_OPT_TYPE_BOOL
, &_conf
.noaction
},
61 {"noaction", OPKG_OPT_TYPE_BOOL
, &_conf
.noaction
},
62 {"download_only", OPKG_OPT_TYPE_BOOL
, &_conf
.download_only
},
63 {"nodeps", OPKG_OPT_TYPE_BOOL
, &_conf
.nodeps
},
64 {"nocase", OPKG_OPT_TYPE_BOOL
, &_conf
.nocase
},
65 {"offline_root", OPKG_OPT_TYPE_STRING
, &_conf
.offline_root
},
66 {"overlay_root", OPKG_OPT_TYPE_STRING
, &_conf
.overlay_root
},
67 {"proxy_passwd", OPKG_OPT_TYPE_STRING
, &_conf
.proxy_passwd
},
68 {"proxy_user", OPKG_OPT_TYPE_STRING
, &_conf
.proxy_user
},
69 {"query-all", OPKG_OPT_TYPE_BOOL
, &_conf
.query_all
},
70 {"size", OPKG_OPT_TYPE_BOOL
, &_conf
.size
},
71 {"tmp_dir", OPKG_OPT_TYPE_STRING
, &_conf
.tmp_dir
},
72 {"verbosity", OPKG_OPT_TYPE_INT
, &_conf
.verbosity
},
73 #if defined(HAVE_OPENSSL)
74 {"signature_ca_file", OPKG_OPT_TYPE_STRING
, &_conf
.signature_ca_file
},
75 {"signature_ca_path", OPKG_OPT_TYPE_STRING
, &_conf
.signature_ca_path
},
77 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
78 {"ssl_engine", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_engine
},
79 {"ssl_cert", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_cert
},
80 {"ssl_cert_type", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_cert_type
},
81 {"ssl_key", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_key
},
82 {"ssl_key_type", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_key_type
},
83 {"ssl_key_passwd", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_key_passwd
},
84 {"ssl_ca_file", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_ca_file
},
85 {"ssl_ca_path", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_ca_path
},
86 {"ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL
,
87 &_conf
.ssl_dont_verify_peer
},
92 static int resolve_pkg_dest_list(void)
94 nv_pair_list_elt_t
*iter
;
99 for (iter
= nv_pair_list_first(&conf
->tmp_dest_list
); iter
;
100 iter
= nv_pair_list_next(&conf
->tmp_dest_list
, iter
)) {
101 nv_pair
= (nv_pair_t
*) iter
->data
;
103 if (conf
->offline_root
) {
104 sprintf_alloc(&root_dir
, "%s%s", conf
->offline_root
,
107 root_dir
= xstrdup(nv_pair
->value
);
111 pkg_dest_list_append(&conf
->pkg_dest_list
, nv_pair
->name
,
112 root_dir
, conf
->lists_dir
);
115 if (conf
->default_dest
== NULL
)
116 conf
->default_dest
= dest
;
118 if (conf
->dest_str
&& !strcmp(dest
->name
, conf
->dest_str
)) {
119 conf
->default_dest
= dest
;
120 conf
->restrict_to_default_dest
= 1;
124 if (conf
->dest_str
&& !conf
->restrict_to_default_dest
) {
125 opkg_msg(ERROR
, "Unknown dest name: `%s'.\n", conf
->dest_str
);
132 static int opkg_conf_set_option(const char *name
, const char *value
)
136 while (options
[i
].name
) {
137 if (strcmp(options
[i
].name
, name
) == 0) {
138 switch (options
[i
].type
) {
139 case OPKG_OPT_TYPE_BOOL
:
140 if (*(int *)options
[i
].value
) {
142 "Duplicate boolean option %s, "
143 "leaving this option on.\n",
147 *((int *const)options
[i
].value
) = 1;
149 case OPKG_OPT_TYPE_INT
:
151 if (*(int *)options
[i
].value
) {
153 "Duplicate option %s, "
154 "using first seen value \"%d\".\n",
160 *((int *const)options
[i
].value
) =
165 "Option %s needs an argument\n",
169 case OPKG_OPT_TYPE_STRING
:
171 if (*(char **)options
[i
].value
) {
173 "Duplicate option %s, "
174 "using first seen value \"%s\".\n",
176 *((char **)options
[i
].
180 *((char **const)options
[i
].value
) =
185 "Option %s needs an argument\n",
194 opkg_msg(ERROR
, "Unrecognized option: %s=%s\n", name
, value
);
199 opkg_conf_parse_file(const char *filename
,
200 pkg_src_list_t
* pkg_src_list
,
201 pkg_src_list_t
* dist_src_list
)
206 regex_t valid_line_re
, comment_re
;
207 #define regmatch_size 14
208 regmatch_t regmatch
[regmatch_size
];
210 file
= fopen(filename
, "r");
212 opkg_perror(ERROR
, "Failed to open %s", filename
);
217 opkg_msg(INFO
, "Loading conf file %s.\n", filename
);
219 err
= xregcomp(&comment_re
,
220 "^[[:space:]]*(#.*|[[:space:]]*)$", REG_EXTENDED
);
224 err
= xregcomp(&valid_line_re
,
225 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
226 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
227 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
228 "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$",
235 char *type
, *name
, *value
, *extra
;
239 line
= file_read_line_alloc(file
);
243 if (regexec(&comment_re
, line
, 0, 0, 0) == 0)
246 if (regexec(&valid_line_re
, line
, regmatch_size
, regmatch
, 0) ==
248 opkg_msg(ERROR
, "%s:%d: Ignoring invalid line: `%s'\n",
249 filename
, line_num
, line
);
253 /* This has to be so ugly to deal with optional quotation marks */
254 if (regmatch
[2].rm_so
> 0) {
255 type
= xstrndup(line
+ regmatch
[2].rm_so
,
256 regmatch
[2].rm_eo
- regmatch
[2].rm_so
);
258 type
= xstrndup(line
+ regmatch
[3].rm_so
,
259 regmatch
[3].rm_eo
- regmatch
[3].rm_so
);
262 if (regmatch
[5].rm_so
> 0) {
263 name
= xstrndup(line
+ regmatch
[5].rm_so
,
264 regmatch
[5].rm_eo
- regmatch
[5].rm_so
);
266 name
= xstrndup(line
+ regmatch
[6].rm_so
,
267 regmatch
[6].rm_eo
- regmatch
[6].rm_so
);
270 if (regmatch
[8].rm_so
> 0) {
271 value
= xstrndup(line
+ regmatch
[8].rm_so
,
272 regmatch
[8].rm_eo
- regmatch
[8].rm_so
);
274 value
= xstrndup(line
+ regmatch
[9].rm_so
,
275 regmatch
[9].rm_eo
- regmatch
[9].rm_so
);
279 if (regmatch
[11].rm_so
> 0) {
280 if (regmatch
[13].rm_so
> 0
281 && regmatch
[13].rm_so
!= regmatch
[13].rm_eo
)
283 xstrndup(line
+ regmatch
[11].rm_so
,
287 extra
= xstrndup(line
+ regmatch
[11].rm_so
,
292 if (regmatch
[13].rm_so
!= regmatch
[13].rm_eo
293 && strncmp(type
, "dist", 4) != 0) {
295 "%s:%d: Ignoring config line with trailing garbage: `%s'\n",
296 filename
, line_num
, line
);
299 /* We use the conf->tmp_dest_list below instead of
300 conf->pkg_dest_list because we might encounter an
301 offline_root option later and that would invalidate the
302 directories we would have computed in
303 pkg_dest_list_init. (We do a similar thing with
304 tmp_src_nv_pair_list for sake of symmetry.) */
305 if (strcmp(type
, "option") == 0) {
306 opkg_conf_set_option(name
, value
);
307 } else if (strcmp(type
, "dist") == 0) {
308 if (!nv_pair_list_find
309 ((nv_pair_list_t
*) dist_src_list
, name
)) {
310 pkg_src_list_append(dist_src_list
, name
,
314 "Duplicate dist declaration (%s %s). "
315 "Skipping.\n", name
, value
);
317 } else if (strcmp(type
, "dist/gz") == 0) {
318 if (!nv_pair_list_find
319 ((nv_pair_list_t
*) dist_src_list
, name
)) {
320 pkg_src_list_append(dist_src_list
, name
,
324 "Duplicate dist declaration (%s %s). "
325 "Skipping.\n", name
, value
);
327 } else if (strcmp(type
, "src") == 0) {
328 if (!nv_pair_list_find
329 ((nv_pair_list_t
*) pkg_src_list
, name
)) {
330 pkg_src_list_append(pkg_src_list
, name
,
334 "Duplicate src declaration (%s %s). "
335 "Skipping.\n", name
, value
);
337 } else if (strcmp(type
, "src/gz") == 0) {
338 if (!nv_pair_list_find
339 ((nv_pair_list_t
*) pkg_src_list
, name
)) {
340 pkg_src_list_append(pkg_src_list
, name
,
344 "Duplicate src declaration (%s %s). "
345 "Skipping.\n", name
, value
);
347 } else if (strcmp(type
, "dest") == 0) {
348 nv_pair_list_append(&conf
->tmp_dest_list
, name
,
350 } else if (strcmp(type
, "lists_dir") == 0) {
351 conf
->lists_dir
= xstrdup(value
);
352 } else if (strcmp(type
, "arch") == 0) {
354 "Supported arch %s priority (%s)\n",
358 "No priority given for architecture %s,"
359 "defaulting to 10\n", name
);
360 value
= xstrdup("10");
362 nv_pair_list_append(&conf
->arch_list
, name
,
366 "%s:%d: Ignoring invalid line: `%s'\n",
367 filename
, line_num
, line
);
382 regfree(&valid_line_re
);
384 regfree(&comment_re
);
386 if (fclose(file
) == EOF
) {
387 opkg_perror(ERROR
, "Couldn't close %s", filename
);
394 int opkg_conf_write_status_files(void)
396 pkg_dest_list_elt_t
*iter
;
405 list_for_each_entry(iter
, &conf
->pkg_dest_list
.head
, node
) {
406 dest
= (pkg_dest_t
*) iter
->data
;
408 dest
->status_fp
= fopen(dest
->status_file_name
, "w");
409 if (dest
->status_fp
== NULL
&& errno
!= EROFS
) {
410 opkg_perror(ERROR
, "Can't open status file %s",
411 dest
->status_file_name
);
416 all
= pkg_vec_alloc();
417 pkg_hash_fetch_available(all
);
419 for (i
= 0; i
< all
->len
; i
++) {
421 /* We don't need most uninstalled packages in the status file */
422 if (pkg
->state_status
== SS_NOT_INSTALLED
423 && (pkg
->state_want
== SW_UNKNOWN
424 || (pkg
->state_want
== SW_DEINSTALL
425 && pkg
->state_flag
!= SF_HOLD
)
426 || pkg
->state_want
== SW_PURGE
)) {
429 if (pkg
->dest
== NULL
) {
431 "Internal error: package %s has a NULL dest\n",
435 if (pkg
->dest
->status_fp
)
436 pkg_print_status(pkg
, pkg
->dest
->status_fp
);
441 list_for_each_entry(iter
, &conf
->pkg_dest_list
.head
, node
) {
442 dest
= (pkg_dest_t
*) iter
->data
;
443 if (dest
->status_fp
&& fclose(dest
->status_fp
) == EOF
) {
444 opkg_perror(ERROR
, "Couldn't close %s",
445 dest
->status_file_name
);
453 char *root_filename_alloc(char *filename
)
456 sprintf_alloc(&root_filename
, "%s%s",
457 (conf
->offline_root
? conf
->offline_root
: ""), filename
);
458 return root_filename
;
461 static int glob_errfunc(const char *epath
, int eerrno
)
463 if (eerrno
== ENOENT
)
464 /* If leading dir does not exist, we get GLOB_NOMATCH. */
467 opkg_msg(ERROR
, "glob failed for %s: %s\n", epath
, strerror(eerrno
));
471 int opkg_conf_init(void)
473 pkg_src_list_init(&conf
->pkg_src_list
);
474 pkg_src_list_init(&conf
->dist_src_list
);
475 pkg_dest_list_init(&conf
->pkg_dest_list
);
476 pkg_dest_list_init(&conf
->tmp_dest_list
);
477 nv_pair_list_init(&conf
->arch_list
);
482 int opkg_conf_load(void)
485 char *tmp
, *tmp_dir_base
, **tmp_val
;
487 char *etc_opkg_conf_pattern
;
489 conf
->restrict_to_default_dest
= 0;
490 conf
->default_dest
= NULL
;
492 if (!conf
->offline_root
)
493 conf
->offline_root
= xstrdup(getenv("OFFLINE_ROOT"));
495 if (conf
->conf_file
) {
497 if (stat(conf
->conf_file
, &st
) == -1) {
498 opkg_perror(ERROR
, "Couldn't stat %s", conf
->conf_file
);
501 if (opkg_conf_parse_file(conf
->conf_file
,
503 &conf
->dist_src_list
))
507 if (conf
->offline_root
)
508 sprintf_alloc(&etc_opkg_conf_pattern
, "%s/etc/opkg/*.conf",
511 const char *conf_file_dir
= getenv("OPKG_CONF_DIR");
512 if (conf_file_dir
== NULL
)
513 conf_file_dir
= OPKG_CONF_DEFAULT_CONF_FILE_DIR
;
514 sprintf_alloc(&etc_opkg_conf_pattern
, "%s/*.conf",
518 memset(&globbuf
, 0, sizeof(globbuf
));
519 glob_ret
= glob(etc_opkg_conf_pattern
, 0, glob_errfunc
, &globbuf
);
520 if (glob_ret
&& glob_ret
!= GLOB_NOMATCH
) {
521 free(etc_opkg_conf_pattern
);
526 free(etc_opkg_conf_pattern
);
528 for (i
= 0; i
< globbuf
.gl_pathc
; i
++) {
529 if (globbuf
.gl_pathv
[i
])
530 if (conf
->conf_file
&&
531 !strcmp(conf
->conf_file
, globbuf
.gl_pathv
[i
]))
533 if (opkg_conf_parse_file(globbuf
.gl_pathv
[i
],
535 &conf
->dist_src_list
) < 0) {
543 if (conf
->offline_root
)
544 sprintf_alloc(&lock_file
, "%s/%s", conf
->offline_root
,
547 sprintf_alloc(&lock_file
, "%s", OPKGLOCKFILE
);
549 lock_fd
= creat(lock_file
, S_IRUSR
| S_IWUSR
| S_IRGRP
);
551 opkg_perror(ERROR
, "Could not create lock file %s", lock_file
);
555 if (lockf(lock_fd
, F_TLOCK
, (off_t
) 0) == -1) {
556 opkg_perror(ERROR
, "Could not lock %s", lock_file
);
557 if (close(lock_fd
) == -1)
558 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
565 tmp_dir_base
= conf
->tmp_dir
;
567 tmp_dir_base
= getenv("TMPDIR");
569 sprintf_alloc(&tmp
, "%s/%s",
570 tmp_dir_base
? tmp_dir_base
:
571 OPKG_CONF_DEFAULT_TMP_DIR_BASE
, OPKG_CONF_TMP_DIR_SUFFIX
);
574 conf
->tmp_dir
= mkdtemp(tmp
);
575 if (conf
->tmp_dir
== NULL
) {
576 opkg_perror(ERROR
, "Creating temp dir %s failed", tmp
);
581 hash_table_init("file-hash", &conf
->file_hash
,
582 OPKG_CONF_DEFAULT_HASH_LEN
);
583 hash_table_init("obs-file-hash", &conf
->obs_file_hash
,
584 OPKG_CONF_DEFAULT_HASH_LEN
/ 16);
586 if (conf
->lists_dir
== NULL
)
587 conf
->lists_dir
= xstrdup(OPKG_CONF_LISTS_DIR
);
589 if (conf
->offline_root
) {
590 sprintf_alloc(&tmp
, "%s/%s", conf
->offline_root
,
592 free(conf
->lists_dir
);
593 conf
->lists_dir
= tmp
;
596 /* if no architectures were defined, then default all, noarch, and host architecture */
597 if (nv_pair_list_empty(&conf
->arch_list
)) {
598 nv_pair_list_append(&conf
->arch_list
, "all", "1");
599 nv_pair_list_append(&conf
->arch_list
, "noarch", "1");
600 nv_pair_list_append(&conf
->arch_list
, HOST_CPU_STR
, "10");
603 /* Even if there is no conf file, we'll need at least one dest. */
604 if (nv_pair_list_empty(&conf
->tmp_dest_list
)) {
605 nv_pair_list_append(&conf
->tmp_dest_list
,
606 OPKG_CONF_DEFAULT_DEST_NAME
,
607 OPKG_CONF_DEFAULT_DEST_ROOT_DIR
);
610 if (resolve_pkg_dest_list())
613 nv_pair_list_deinit(&conf
->tmp_dest_list
);
618 free(conf
->lists_dir
);
621 hash_table_deinit(&conf
->file_hash
);
622 hash_table_deinit(&conf
->obs_file_hash
);
624 if (rmdir(conf
->tmp_dir
) == -1)
625 opkg_perror(ERROR
, "Couldn't remove dir %s", conf
->tmp_dir
);
627 if (lockf(lock_fd
, F_ULOCK
, (off_t
) 0) == -1)
628 opkg_perror(ERROR
, "Couldn't unlock %s", lock_file
);
630 if (close(lock_fd
) == -1)
631 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
633 if (unlink(lock_file
) == -1)
634 opkg_perror(ERROR
, "Couldn't unlink %s", lock_file
);
641 pkg_src_list_deinit(&conf
->pkg_src_list
);
642 pkg_src_list_deinit(&conf
->dist_src_list
);
643 pkg_dest_list_deinit(&conf
->pkg_dest_list
);
644 nv_pair_list_deinit(&conf
->arch_list
);
646 for (i
= 0; options
[i
].name
; i
++) {
647 if (options
[i
].type
== OPKG_OPT_TYPE_STRING
) {
648 tmp_val
= (char **)options
[i
].value
;
656 nv_pair_list_deinit(&conf
->tmp_dest_list
);
658 free(conf
->dest_str
);
660 free(conf
->conf_file
);
665 void opkg_conf_deinit(void)
674 free(conf
->lists_dir
);
677 free(conf
->dest_str
);
680 free(conf
->conf_file
);
682 pkg_src_list_deinit(&conf
->pkg_src_list
);
683 pkg_src_list_deinit(&conf
->dist_src_list
);
684 pkg_dest_list_deinit(&conf
->pkg_dest_list
);
685 nv_pair_list_deinit(&conf
->arch_list
);
687 for (i
= 0; options
[i
].name
; i
++) {
688 if (options
[i
].type
== OPKG_OPT_TYPE_STRING
) {
689 tmp
= (char **)options
[i
].value
;
697 if (conf
->verbosity
>= DEBUG
) {
698 hash_print_stats(&conf
->pkg_hash
);
699 hash_print_stats(&conf
->file_hash
);
700 hash_print_stats(&conf
->obs_file_hash
);
704 hash_table_deinit(&conf
->file_hash
);
705 hash_table_deinit(&conf
->obs_file_hash
);
708 if (lockf(lock_fd
, F_ULOCK
, (off_t
) 0) == -1)
709 opkg_perror(ERROR
, "Couldn't unlock %s", lock_file
);
711 if (close(lock_fd
) == -1)
712 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
718 if (unlink(lock_file
) == -1)
719 opkg_perror(ERROR
, "Couldn't unlink %s", lock_file
);