1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
|
From: Dmitry Yu Okunev <dyokunev@ut.mephi.ru>
Date: Wed, 4 Jan 2017 21:42:01 +0900
Subject: lsb header support
Description: This patch adds support for the sysv-rc LSB headers
This is the take 2 of the LSB headers patch. The first version was using a
perl script, which isn't good enough (because of /usr/bin/perl, we need to
have /usr mounted early, which isn't possible). This rewrite in C fixes the
issue.
Contributor: Benda Xu <heroxbd@gentoo.org>
---
sh/gendepends.sh.in | 15 +-
sh/openrc-run.sh.in | 50 ++--
src/librc/librc-depend.c | 11 +-
src/librc/librc.c | 19 --
src/lsb2rcconf/LICENSE | 10 +
src/lsb2rcconf/main.c | 658 ++++++++++++++++++++++++++++++++++++++++++++
src/lsb2rcconf/meson.build | 3 +
src/lsb2rcconf/tests/run.sh | 3 +
src/meson.build | 1 +
src/shared/misc.c | 2 +-
10 files changed, 714 insertions(+), 58 deletions(-)
create mode 100644 src/lsb2rcconf/LICENSE
create mode 100644 src/lsb2rcconf/main.c
create mode 100644 src/lsb2rcconf/meson.build
create mode 100644 src/lsb2rcconf/tests/run.sh
diff --git a/sh/gendepends.sh.in b/sh/gendepends.sh.in
index 4e32f24..4caed2b 100644
--- a/sh/gendepends.sh.in
+++ b/sh/gendepends.sh.in
@@ -69,9 +69,13 @@ do
cd "$_dir"
for RC_SERVICE in *; do
[ -x "$RC_SERVICE" -a -f "$RC_SERVICE" ] || continue
+ # follow insserv to ignore alternatives generated by dpkg.
+ case "$RC_SERVICE" in *.dpkg*) continue ;; esac
- # Only generate dependencies for OpenRC scripts
+ lsbh=$(lsb2rcconf "$RC_SERVICE")
+ # Only generate dependencies for OpenRC and LSB scripts
read one two three <"$RC_SERVICE"
+ [ -z "${lsbh}" ] && \
case "$one" in
\#*/openrc-run) ;;
\#*/runscript) ;;
@@ -118,10 +122,13 @@ do
done
fi
- if . "$_dir/$RC_SVCNAME"; then
- echo "$RC_SVCNAME" >&3
- _depend
+ if [ -n "${lsbh}" ]; then
+ eval "${lsbh}"
+ else
+ . "$_dir/$RC_SVCNAME"
fi
+ echo "$RC_SVCNAME" >&3
+ _depend
)
done
done
diff --git a/sh/openrc-run.sh.in b/sh/openrc-run.sh.in
index 9939adf..25d1d2a 100644
--- a/sh/openrc-run.sh.in
+++ b/sh/openrc-run.sh.in
@@ -269,7 +269,27 @@ sourcex "@LIBEXECDIR@/sh/start-stop-daemon.sh"
sourcex "@LIBEXECDIR@/sh/supervise-daemon.sh"
# Load our script
-sourcex "$RC_SERVICE"
+lsbh=$(lsb2rcconf "$RC_SERVICE")
+if [ -n "${lsbh}" ]; then
+ eval "${lsbh}"
+else
+ sourcex "$RC_SERVICE"
+
+ eval "printf '%s\n' $required_dirs" | while read _d; do
+ if [ -n "$_d" ] && [ ! -d "$_d" ]; then
+ eerror "$RC_SVCNAME: \`$_d' is not a directory"
+ exit 1
+ fi
+ done
+ [ $? -ne 0 ] && exit 1
+ unset _d
+
+ if [ -n "$opts" ]; then
+ ewarn "Use of the opts variable is deprecated and will be"
+ ewarn "removed in the future."
+ ewarn "Please use extra_commands, extra_started_commands or extra_stopped_commands."
+ fi
+fi
# Set verbose mode
if yesno "${rc_verbose:-$RC_VERBOSE}"; then
@@ -303,30 +323,6 @@ for _cmd; do
fi
done
-eval "printf '%s\n' $required_dirs" | while read _d; do
- if [ -n "$_d" ] && [ ! -d "$_d" ]; then
- eerror "$RC_SVCNAME: \`$_d' is not a directory"
- exit 1
- fi
-done
-[ $? -ne 0 ] && exit 1
-unset _d
-
-eval "printf '%s\n' $required_files" | while read _f; do
- if [ -n "$_f" ] && [ ! -r "$_f" ]; then
- eerror "$RC_SVCNAME: \`$_f' is not readable"
- exit 1
- fi
-done
-[ $? -ne 0 ] && exit 1
-unset _f
-
-if [ -n "$opts" ]; then
- ewarn "Use of the opts variable is deprecated and will be"
- ewarn "removed in the future."
- ewarn "Please use extra_commands, extra_started_commands or extra_stopped_commands."
-fi
-
while [ -n "$1" ]; do
# Special case depend
if [ "$1" = depend ]; then
@@ -338,6 +334,10 @@ while [ -n "$1" ]; do
_depend
cd /
continue
+ elif [ -n "${lsbh}" ]; then
+ . "${RC_SERVICE}"
+ shift
+ continue
fi
# See if we have the required function and run it
for _cmd in describe start stop status ${extra_commands:-$opts} \
diff --git a/src/librc/librc-depend.c b/src/librc/librc-depend.c
index 60be21e..e28b841 100644
--- a/src/librc/librc-depend.c
+++ b/src/librc/librc-depend.c
@@ -780,7 +780,7 @@ rc_deptree_update(void)
char *line = NULL;
size_t size;
char *depend, *depends, *service, *type;
- size_t i, l;
+ size_t i;
bool retval = true;
const char *sys = rc_sys();
int serrno;
@@ -835,14 +836,6 @@ rc_deptree_update(void)
if (strcmp(depend, service) == 0)
continue;
- /* .sh files are not init scripts */
- l = strlen(depend);
- if (l > 2 &&
- depend[l - 3] == '.' &&
- depend[l - 2] == 's' &&
- depend[l - 1] == 'h')
- continue;
-
/* Remove our dependency if instructed */
if (depend[0] == '!') {
rc_stringlist_delete(deptype->services, depend + 1);
diff --git a/src/librc/librc.c b/src/librc/librc.c
index acb448a..4f7d92d 100644
--- a/src/librc/librc.c
+++ b/src/librc/librc.c
@@ -93,7 +93,6 @@ ls_dir(int targetfd, const char *dir, int options)
struct dirent *d;
RC_STRINGLIST *list = NULL;
struct stat buf;
- size_t l;
list = rc_stringlist_new();
@@ -109,13 +108,6 @@ ls_dir(int targetfd, const char *dir, int options)
* runlevel, but could have been removed. */
if (fstatat(dirfd(dp), d->d_name, &buf, 0) != 0)
continue;
-
- /* .sh files are not init scripts */
- l = strlen(d->d_name);
- if (l > 2 && d->d_name[l - 3] == '.' &&
- d->d_name[l - 2] == 's' &&
- d->d_name[l - 1] == 'h')
- continue;
}
if (options & LS_DIR) {
if (fstatat(dirfd(dp), d->d_name, &buf, 0) != 0 || !S_ISDIR(buf.st_mode))
@@ -767,7 +759,6 @@ rc_service_exists(const char *service)
{
char *file;
bool retval = false;
- size_t len;
struct stat buf;
if (!service) {
@@ -775,16 +766,6 @@ rc_service_exists(const char *service)
return false;
}
- len = strlen(service);
-
- /* .sh files are not init scripts */
- if (len > 2 && service[len - 3] == '.' &&
- service[len - 2] == 's' &&
- service[len - 1] == 'h') {
- errno = EINVAL;
- return false;
- }
-
if (!(file = rc_service_resolve(service))) {
errno = ENOENT;
return false;
diff --git a/src/lsb2rcconf/LICENSE b/src/lsb2rcconf/LICENSE
new file mode 100644
index 0000000..8f6bdb2
--- /dev/null
+++ b/src/lsb2rcconf/LICENSE
@@ -0,0 +1,10 @@
+Copyright (c) 2014, Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/src/lsb2rcconf/main.c b/src/lsb2rcconf/main.c
new file mode 100644
index 0000000..db029ae
--- /dev/null
+++ b/src/lsb2rcconf/main.c
@@ -0,0 +1,658 @@
+/*
+ lsb2rcconf - prints configuration for openrc's rc.conf based on LSB
+
+Copyright (c) 2014-2016, Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+this list of conditions and the following disclaimer in the documentation
+and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ */
+
+/* If you have any question, I'll recommend you to ask irc.freenode.net#openrc */
+
+
+
+/* === includes === */
+
+/* #define _GNU_SOURCE / * hsearch_r() */
+
+#ifdef _GNU_SOURCE
+#define hsearch_data_t struct hsearch_data
+#endif
+
+#include <stdio.h> /* fprintf(), perror() */
+#include <stdlib.h> /* exit() */
+#include <string.h> /* strcmp(), strerror() */
+#include <errno.h> /* ENVAL */
+#include <libgen.h> /* basename() */
+#include <search.h> /* hsearch_r() */
+#include <sys/types.h> /* regexec() */
+#include <regex.h> /* regexec() */
+#include <unistd.h> /* access() */
+#include <dirent.h> /* scandir() */
+
+/* #include "configuration.h"
+ * #include "xmalloc.h"
+ * These includes are replaced for "openrc" Debian-package by: */
+#include "../shared/helpers.h"
+
+#define SERVICENAME_ALL "_all"
+
+/* === portability hacks === */
+
+#ifndef _GNU_SOURCE
+/* hsearch_r() fallbacks to tsearch() :( */
+
+#define hsearch_data_t void *
+
+#define hsearch_r(...) hsearch_r_2_tsearch(__VA_ARGS__)
+int _hsearch_r_2_tsearch_compare(const ENTRY *a, const ENTRY *b)
+{
+ return strcmp(a->key, b->key);
+}
+static inline int hsearch_r_2_tsearch(ENTRY item, ACTION action, ENTRY **retval, hsearch_data_t *htab)
+{
+ ENTRY **tret = NULL;
+
+ switch (action) {
+ case FIND: {
+ tret = tfind (&item, htab, (int (*)(const void *, const void *))_hsearch_r_2_tsearch_compare);
+ break;
+ }
+ case ENTER: {
+ ENTRY *item_dup_p = xmalloc(sizeof(item));
+ item_dup_p->key = xstrdup(item.key);
+ item_dup_p->data = item.data;
+ tret = tsearch(item_dup_p, htab, (int (*)(const void *, const void *))_hsearch_r_2_tsearch_compare);
+ break;
+ }
+ }
+
+ if (tret == NULL) {
+ *retval = NULL;
+ return 0;
+ }
+ *retval = *tret;
+
+ return 0;
+}
+
+#define hcreate_r(...) hcreate_r_2_nop(__VA_ARGS__)
+static inline int hcreate_r_2_nop(size_t nel, hsearch_data_t *htab)
+{
+ return 1;
+}
+
+#endif /* ifndef _GNU_SOURCE */
+
+
+
+/* === code self === */
+
+enum lsb_parse_state {
+ LP_STARTED = 0,
+ LP_PARSING_LSB,
+ LP_COMPLETE,
+};
+typedef enum lsb_parse_state lsb_parse_state_t;
+
+extern const char *lsb_v2s(const char *const lsb_virtual);
+
+#define PATH_INSSERV "/etc/insserv.conf"
+
+#define HT_SIZE_VSRV (1<<8)
+
+/* virtual to value */
+hsearch_data_t ht_lsb_v2s = {0};
+/* value to virtual */
+hsearch_data_t ht_lsb_s2v = {0};
+
+char *description = NULL;
+char *service_me;
+
+typedef void (*services_foreach_funct_t)(const char *const service, void *arg);
+
+static inline int services_foreach(const char *const _services, services_foreach_funct_t funct, void *arg)
+{
+ if (_services == NULL) {
+ fprintf(stderr, "Internal error (#0)\n");
+ exit(-1);
+ }
+
+ char *services = xstrdup(_services);
+ char *strtok_saveptr = NULL;
+ char *service = strtok_r(services, " \t", &strtok_saveptr);
+ do {
+ funct(service, arg);
+ } while ((service = strtok_r(NULL, " \t", &strtok_saveptr)));
+ /* free(services); */
+
+ return 0;
+}
+
+#define MAX_need (1<<16)
+#define MAX_use MAX_need
+#define MAX_provide MAX_need
+#define MAX_before MAX_need
+
+struct relation_arg {
+ char **relation;
+ int *relation_count_p;
+ int relation_max;
+ hsearch_data_t *relation_ht_p;
+ char relation_has_all; /* did we met "$all" virtual services or not */
+};
+
+#define RELATION(relation_name)\
+ char *relation_name[MAX_ ## relation_name + 1] = {NULL};\
+ int relation_name ## _count = 0;\
+ hsearch_data_t relation_name ## _ht = {0};\
+ struct relation_arg relation_name ## _arg = \
+ {relation_name, &relation_name ## _count, MAX_ ## relation_name, &relation_name ##_ht};
+
+RELATION(need);
+RELATION(use);
+RELATION(provide);
+RELATION(before);
+
+
+void relation_add_oneservice(char *service, struct relation_arg *arg_p)
+{
+ switch (*service) {
+ case '+': {
+ service++;
+ if (arg_p->relation == need) /* Moving optional services from need to use */
+ arg_p = &use_arg;
+ }
+ default: {
+ if (*(arg_p->relation_count_p))
+ if (!memcmp(arg_p->relation[0], "*", 2))
+ return;
+
+ if (!strcmp(service, "*")) {
+ arg_p->relation[0] = xstrdup("*");
+ arg_p->relation[1] = NULL;
+ *(arg_p->relation_count_p) = 1;
+ return;
+ }
+
+ ENTRY entry = {service, NULL}, *entry_res_ptr;
+ hsearch_r(entry, FIND, &entry_res_ptr, arg_p->relation_ht_p);
+ if (entry_res_ptr != NULL)
+ return;
+
+ arg_p->relation[(*arg_p->relation_count_p)++] = service;
+ hsearch_r(entry, ENTER, &entry_res_ptr, arg_p->relation_ht_p);
+ }
+ }
+}
+
+void relation_add(const char *const _service, struct relation_arg *arg_p)
+{
+ if (!strcmp(_service, service_me)) {
+ return;
+ }
+
+ char *service_buf = xstrdup(_service), *service = service_buf;
+
+ if (*arg_p->relation_count_p >= arg_p->relation_max) {
+ fprintf(stderr, "Too many records.\n");
+ exit(EOVERFLOW);
+ }
+ ENTRY entry = {service, NULL}, *entry_res_ptr;
+
+ hsearch_r(entry, FIND, &entry_res_ptr, arg_p->relation_ht_p);
+
+ if (entry_res_ptr == NULL) {
+ switch (*service) {
+ case '$': {
+ service++;
+
+ if (!arg_p->relation_has_all)
+ arg_p->relation_has_all = (strcmp(service, "all") == 0);
+
+ const char *const services = lsb_v2s(service);
+ if (services != NULL) {
+ services_foreach(services, (services_foreach_funct_t)relation_add_oneservice, arg_p);
+ }
+ break;
+ }
+ default: {
+ relation_add_oneservice(service, arg_p);
+ }
+ }
+ }
+}
+
+#define RELATION_ADD(_relation, _services)\
+{\
+ char *services = xstrdup(_services);\
+ services_foreach(services, (services_foreach_funct_t)relation_add, &_relation ## _arg);\
+ free(services);\
+}
+
+static inline void NEED(const char *const _services)
+{
+ RELATION_ADD(need, _services);
+}
+static inline void USE(const char *const _services)
+{
+ RELATION_ADD(use, _services);
+}
+static inline void PROVIDE(const char *const _services)
+{
+ RELATION_ADD(provide, _services);
+}
+static inline void BEFORE(const char *const _services)
+{
+ RELATION_ADD(before, _services);
+}
+
+void syntax()
+{
+ fprintf(stderr, "lsb2rcconf /path/to/init/script\n");
+ exit(EINVAL);
+}
+
+static inline void lsb_x2x_add(char *key, char *data, hsearch_data_t *ht)
+{
+ ENTRY entry = {key, data}, *entry_res_ptr;
+
+ hsearch_r(entry, ENTER, &entry_res_ptr, ht);
+}
+
+void lsb_s2v_add(char *key, char *data)
+{
+ return lsb_x2x_add(key, data, &ht_lsb_s2v);
+}
+
+void lsb_v2s_add(char *key, char *data)
+{
+ return lsb_x2x_add(key, data, &ht_lsb_v2s);
+}
+
+static const int xregcomp(regex_t *preg, const char *regex, int cflags)
+{
+ int r;
+ if ((r=regcomp(preg, regex, cflags))) {
+ char buf[BUFSIZ];
+ regerror(r, preg, buf, BUFSIZ);
+ fprintf(stderr, "Error: Cannot compile regex: %i: %s\n", r, buf);
+ exit(r);
+ }
+
+ return r;
+}
+
+struct services_unrolled_buffer {
+ char start[BUFSIZ];
+ char *ptr;
+ char *end;
+};
+typedef struct services_unrolled_buffer services_unrolled_buffer_t;
+
+void parse_insserv_parse_service(char *service, services_unrolled_buffer_t *buf)
+{
+ const char *services;
+ switch (*service) {
+ case '$':
+ service++;
+ services = lsb_v2s(service);
+ break;
+ default:
+ services = service;
+ break;
+ }
+ if (services == NULL)
+ return;
+
+ size_t len = strlen(services);
+
+ if (&buf->ptr[len] >= buf->end) {
+ fprintf(stderr, "Error: Too long field value.\n");
+ exit(EOVERFLOW);
+ }
+
+ memcpy(buf->ptr, services, len);
+ buf->ptr = &buf->ptr[len];
+ *(buf->ptr++) = ' ';
+}
+
+/* Forward declaration */
+void parse_insserv_file(const char *restrict file);
+
+void parse_insserv()
+{
+ parse_insserv_file(PATH_INSSERV);
+
+ struct dirent** namelist;
+ int n;
+
+ n = scandir(PATH_INSSERV".d", &namelist, NULL, alphasort);
+ if (n == -1) {
+ perror("scandir");
+ exit(EXIT_FAILURE);
+ }
+
+ while (n--) {
+ char buf[PATH_MAX+1];
+ int r;
+
+ r = snprintf(&buf[0], sizeof(buf), "%s/%s", PATH_INSSERV".d", namelist[n]->d_name);
+ if (r >= (int)sizeof(buf) || r < 0) {
+ fprintf(stderr, "snprintf(): %s\n", strerror(errno));
+ exit(errno);
+ }
+ parse_insserv_file(buf);
+
+ free(namelist[n]);
+ }
+ free(namelist);
+}
+
+void parse_insserv_file(const char *restrict file)
+{
+ FILE *file_insserv = fopen(file, "r");
+ if (file_insserv == NULL) {
+ fprintf(stderr, "Error: Unable to read \"%s\": %i: %s\n", file, errno, strerror(errno));
+ exit(errno);
+ }
+
+ regex_t regex;
+
+ xregcomp(®ex, "^(\\$\\S+)\\s*(\\S?.*)$", REG_EXTENDED);
+
+ size_t line_len;
+ size_t line_size = 0;
+ char *line_ptr = NULL;
+ while ((line_len = getline(&line_ptr, &line_size, file_insserv))!=-1) {
+ regmatch_t matches[4] = {{0}};
+
+ if (!line_len)
+ continue;
+
+ if (*line_ptr == '#')
+ continue;
+
+ line_ptr[--line_len] = 0; /* cutting-off '\n' */
+
+ if (!regexec(®ex, line_ptr, 3, matches, 0)) {
+ char *virtual = xstrdup(&line_ptr[matches[1].rm_so]); /* TODO: free() this */
+ virtual[ matches[1].rm_eo - matches[1].rm_so] = 0;
+ if (*virtual == '$')
+ virtual++;
+
+ char *services = xstrdup(&line_ptr[matches[2].rm_so]); /* TODO: free() this */
+ services[matches[2].rm_eo - matches[2].rm_so] = 0;
+
+ /* $virtual: +service +service +service +service */
+ /* services */
+
+ services_unrolled_buffer_t buf;
+ buf.end = &buf.start[BUFSIZ];
+
+ buf.ptr = buf.start;
+
+ services_foreach(services, (services_foreach_funct_t)parse_insserv_parse_service, &buf);
+ *(--buf.ptr) = 0;
+
+ lsb_v2s_add(virtual, xstrdup(buf.start));
+ }
+ }
+
+ fclose(file_insserv);
+}
+
+void lsb_init()
+{
+ /* Hardcoded: */
+ ENTRY entries_v2s[] = {
+ {"all", "+"SERVICENAME_ALL},
+ {NULL, NULL},
+ };
+ ENTRY entries_s2v[] = {
+ {SERVICENAME_ALL, "all"},
+ {NULL, NULL},
+ };
+
+
+ /* Initialization: */
+ hcreate_r(HT_SIZE_VSRV, &ht_lsb_v2s);
+ hcreate_r(HT_SIZE_VSRV, &ht_lsb_s2v);
+ hcreate_r(MAX_need, &need_ht);
+ hcreate_r(MAX_use, &use_ht);
+ hcreate_r(MAX_before, &before_ht);
+ hcreate_r(MAX_provide, &provide_ht);
+
+ ENTRY *entry_ptr, *entry_res_ptr;
+
+ /* Remembering hardcoded values: */
+ entry_ptr = entries_v2s;
+ while (entry_ptr->key != NULL) {
+ hsearch_r(*entry_ptr, ENTER, &entry_res_ptr, &ht_lsb_v2s);
+ entry_ptr++;
+ }
+ entry_ptr = entries_s2v;
+ while (entry_ptr->key != NULL) {
+ hsearch_r(*entry_ptr, ENTER, &entry_res_ptr, &ht_lsb_s2v);
+ entry_ptr++;
+ }
+
+ /* Parse /etc/insserv.conf */
+ parse_insserv();
+}
+
+static inline const char *lsb_x2x(const char *const lsb_virtual, hsearch_data_t *ht)
+{
+ ENTRY entry, *entry_ptr;
+
+ entry.key = (char *)lsb_virtual;
+
+ hsearch_r(entry, FIND, &entry_ptr, ht);
+ if (entry_ptr != NULL)
+ return entry_ptr->data;
+
+ return NULL;
+}
+
+const char *lsb_v2s(const char *const lsb_virtual)
+{
+ return lsb_x2x(lsb_virtual, &ht_lsb_v2s);
+}
+
+static void lsb_header_parse(const char *const header, char *value)
+{
+ if (!strcmp(header, "provides")) {
+ PROVIDE(value);
+ } else
+ if (!strcmp(header, "required-start") || !strcmp(header, "required-stop")) {
+ NEED(value);
+ } else
+/* if (!strcmp(header, "default-start")) {
+ } else
+ if (!strcmp(header, "default-stop")) {
+ } else*/
+ if (!strcmp(header, "short-description")) {
+ description = value;
+ } else
+/* if (!strcmp(header, "description")) {
+ } else*/
+ if (!strcmp(header, "should-start") || !strcmp(header, "should-stop")) {
+ USE(value);
+ } else
+ if (!strcmp(header, "x-start-before") || !strcmp(header, "x-stop-after")) {
+ BEFORE(value);
+ } else
+ {}
+
+ return;
+}
+
+char *strtolower(char *_str)
+{
+ char *str = _str;
+ while (*str) *(str++) |= 0x20;
+ return _str;
+}
+
+lsb_parse_state_t lsb_parse(const char *initdscript)
+{
+ FILE *file_initdscript = fopen(initdscript, "r");
+
+ if (file_initdscript == NULL) {
+ fprintf(stderr, "Error: Unable to read \"%s\": %i: %s\n",
+ initdscript, errno, strerror(errno));
+ exit(errno);
+ }
+
+ regex_t regex_start, regex_header, regex_end;
+
+ xregcomp(®ex_start, "^### BEGIN INIT INFO\\s*$", REG_EXTENDED);
+ xregcomp(®ex_header, "^#\\s*(\\S+):\\s\\s*(.*\\S)\\s*$", REG_EXTENDED);
+ xregcomp(®ex_end, "^### END INIT INFO\\s*$", REG_EXTENDED);
+
+ lsb_parse_state_t state = LP_STARTED;
+ size_t line_len;
+ size_t line_size = 0;
+ char *line_ptr = NULL;
+ while ((line_len = getline(&line_ptr, &line_size, file_initdscript))!=-1) {
+ if (!line_len)
+ continue;
+
+ line_ptr[--line_len] = 0; /* cutting-off '\n' */
+
+ switch (state) {
+ case LP_STARTED: {
+ if (!regexec(®ex_start, line_ptr, 0, NULL, 0)) {
+ state = LP_PARSING_LSB;
+ continue;
+ }
+ break;
+ }
+ case LP_PARSING_LSB: {
+ regmatch_t matches[4] = {{0}};
+
+ if (!regexec(®ex_header, line_ptr, 3, matches, 0)) {
+ char *header = xstrdup(&line_ptr[matches[1].rm_so]);
+ header[matches[1].rm_eo - matches[1].rm_so] = 0;
+
+ char *value = xstrdup(&line_ptr[matches[2].rm_so]); /* TODO: free() this */
+ value[ matches[2].rm_eo - matches[2].rm_so] = 0;
+
+ lsb_header_parse(strtolower(header), value);
+
+ free(header);
+ } else
+ if (!regexec(®ex_end, line_ptr, 0, NULL, 0)) {
+ state = LP_COMPLETE;
+ goto l_lsb_parse_end;
+ }
+ break;
+ }
+ case LP_COMPLETE:
+ goto l_lsb_parse_end;
+ }
+ }
+
+l_lsb_parse_end:
+
+ if (state == LP_COMPLETE)
+ if (! (use_arg.relation_has_all || need_arg.relation_has_all))
+ PROVIDE(SERVICENAME_ALL);
+
+ fclose(file_initdscript);
+ return state;
+}
+
+static inline void print_relation(char **relation)
+{
+ char **ptr = relation;
+ while (*ptr != NULL) {
+ printf(" %s", *ptr);
+ ptr++;
+ }
+
+ return;
+}
+
+void lsb_print_orc()
+{
+ if (description != NULL)
+ printf("description=\"%s\"\n\n", description);
+
+ printf("%s", "depend () {\n");
+
+ if (*provide != NULL) {
+ printf("%s", "\tprovide");
+ print_relation(provide);
+ printf("\n");
+ } else
+ printf("\tprovide %s\n", service_me);
+ if (*use != NULL) {
+ printf("%s", "\tuse");
+ print_relation(use);
+ printf("\n");
+ }
+ if (*need != NULL) {
+ printf("%s", "\tneed");
+ print_relation(need);
+ printf("\n");
+ }
+ if (*before != NULL) {
+ printf("%s", "\tbefore");
+ print_relation(before);
+ printf("\n");
+ }
+ printf("}\n");
+
+ return;
+}
+
+int main(int argc, char *argv[])
+{
+ if (argc <= 1)
+ syntax();
+
+ const char *initdscript = argv[1];
+ service_me = basename(xstrdup(initdscript));
+
+ if (access(initdscript, R_OK)) {
+ fprintf(stderr, "Cannot get read access to file \"%s\": %i: %s\n",
+ initdscript, errno, strerror(errno));
+ exit(errno);
+ }
+
+ lsb_init();
+
+ if (lsb_parse(initdscript) == LP_COMPLETE)
+ lsb_print_orc();
+
+ exit(0);
+}
+
+/*
+Local Variables:
+c-basic-offset: 4
+tab-width: 4
+indent-tabs-mode: t
+End:
+ */
diff --git a/src/lsb2rcconf/meson.build b/src/lsb2rcconf/meson.build
new file mode 100644
index 0000000..40396c7
--- /dev/null
+++ b/src/lsb2rcconf/meson.build
@@ -0,0 +1,3 @@
+lsb2rcconf = executable('lsb2rcconf', 'main.c',
+ install: true,
+ install_dir : rc_bindir )
diff --git a/src/lsb2rcconf/tests/run.sh b/src/lsb2rcconf/tests/run.sh
new file mode 100644
index 0000000..c52d3c2
--- /dev/null
+++ b/src/lsb2rcconf/tests/run.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+exit 0
diff --git a/src/meson.build b/src/meson.build
index 10d88c4..399d49b 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -11,6 +11,7 @@ subdir('halt')
subdir('is_newer_than')
subdir('is_older_than')
subdir('kill_all')
+subdir('lsb2rcconf')
subdir('mark_service')
subdir('mountinfo')
subdir('on_ac_power')
diff --git a/src/shared/misc.c b/src/shared/misc.c
index e57907e..de12dc0 100644
--- a/src/shared/misc.c
+++ b/src/shared/misc.c
@@ -347,7 +347,7 @@ exec_service(const char *service, const char *arg)
sigprocmask(SIG_UNBLOCK, &full, NULL);
/* Safe to run now */
- execl(file, file, "--lockfd", sfd, arg, (char *) NULL);
+ execl("/sbin/openrc-run", "/sbin/openrc-run", file, "--lockfd", sfd, arg, (char *) NULL);
fprintf(stderr, "unable to exec `%s': %s\n",
file, strerror(errno));
svc_unlock(basename_c(service), fd);
|