LCOV - code coverage report
Current view: top level - delta - delta.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 24 341 7.0 %
Date: 2019-08-22 15:41:25 Functions: 4 19 21.1 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <errno.h>
       4             : #include <getopt.h>
       5             : #include <string.h>
       6             : #include <sys/prctl.h>
       7             : #include <unistd.h>
       8             : 
       9             : #include "alloc-util.h"
      10             : #include "dirent-util.h"
      11             : #include "fd-util.h"
      12             : #include "fs-util.h"
      13             : #include "hashmap.h"
      14             : #include "locale-util.h"
      15             : #include "log.h"
      16             : #include "main-func.h"
      17             : #include "nulstr-util.h"
      18             : #include "pager.h"
      19             : #include "parse-util.h"
      20             : #include "path-util.h"
      21             : #include "pretty-print.h"
      22             : #include "process-util.h"
      23             : #include "signal-util.h"
      24             : #include "stat-util.h"
      25             : #include "string-util.h"
      26             : #include "strv.h"
      27             : #include "terminal-util.h"
      28             : 
      29             : static const char prefixes[] =
      30             :         "/etc\0"
      31             :         "/run\0"
      32             :         "/usr/local/lib\0"
      33             :         "/usr/local/share\0"
      34             :         "/usr/lib\0"
      35             :         "/usr/share\0"
      36             : #if HAVE_SPLIT_USR
      37             :         "/lib\0"
      38             : #endif
      39             :         ;
      40             : 
      41             : static const char suffixes[] =
      42             :         "sysctl.d\0"
      43             :         "tmpfiles.d\0"
      44             :         "modules-load.d\0"
      45             :         "binfmt.d\0"
      46             :         "systemd/system\0"
      47             :         "systemd/user\0"
      48             :         "systemd/system-preset\0"
      49             :         "systemd/user-preset\0"
      50             :         "udev/rules.d\0"
      51             :         "modprobe.d\0";
      52             : 
      53             : static const char have_dropins[] =
      54             :         "systemd/system\0"
      55             :         "systemd/user\0";
      56             : 
      57             : static PagerFlags arg_pager_flags = 0;
      58             : static int arg_diff = -1;
      59             : 
      60             : static enum {
      61             :         SHOW_MASKED     = 1 << 0,
      62             :         SHOW_EQUIVALENT = 1 << 1,
      63             :         SHOW_REDIRECTED = 1 << 2,
      64             :         SHOW_OVERRIDDEN = 1 << 3,
      65             :         SHOW_UNCHANGED  = 1 << 4,
      66             :         SHOW_EXTENDED   = 1 << 5,
      67             : 
      68             :         SHOW_DEFAULTS =
      69             :         (SHOW_MASKED | SHOW_EQUIVALENT | SHOW_REDIRECTED | SHOW_OVERRIDDEN | SHOW_EXTENDED)
      70             : } arg_flags = 0;
      71             : 
      72           0 : static int equivalent(const char *a, const char *b) {
      73           0 :         _cleanup_free_ char *x = NULL, *y = NULL;
      74             :         int r;
      75             : 
      76           0 :         r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x);
      77           0 :         if (r < 0)
      78           0 :                 return r;
      79             : 
      80           0 :         r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y);
      81           0 :         if (r < 0)
      82           0 :                 return r;
      83             : 
      84           0 :         return path_equal(x, y);
      85             : }
      86             : 
      87           0 : static int notify_override_masked(const char *top, const char *bottom) {
      88           0 :         if (!(arg_flags & SHOW_MASKED))
      89           0 :                 return 0;
      90             : 
      91           0 :         printf("%s%s%s     %s %s %s\n",
      92             :                ansi_highlight_red(), "[MASKED]", ansi_normal(),
      93             :                top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
      94           0 :         return 1;
      95             : }
      96             : 
      97           0 : static int notify_override_equivalent(const char *top, const char *bottom) {
      98           0 :         if (!(arg_flags & SHOW_EQUIVALENT))
      99           0 :                 return 0;
     100             : 
     101           0 :         printf("%s%s%s %s %s %s\n",
     102             :                ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(),
     103             :                top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
     104           0 :         return 1;
     105             : }
     106             : 
     107           0 : static int notify_override_redirected(const char *top, const char *bottom) {
     108           0 :         if (!(arg_flags & SHOW_REDIRECTED))
     109           0 :                 return 0;
     110             : 
     111           0 :         printf("%s%s%s %s %s %s\n",
     112             :                ansi_highlight(), "[REDIRECTED]", ansi_normal(),
     113             :                top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
     114           0 :         return 1;
     115             : }
     116             : 
     117           0 : static int notify_override_overridden(const char *top, const char *bottom) {
     118           0 :         if (!(arg_flags & SHOW_OVERRIDDEN))
     119           0 :                 return 0;
     120             : 
     121           0 :         printf("%s%s%s %s %s %s\n",
     122             :                ansi_highlight(), "[OVERRIDDEN]", ansi_normal(),
     123             :                top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
     124           0 :         return 1;
     125             : }
     126             : 
     127           0 : static int notify_override_extended(const char *top, const char *bottom) {
     128           0 :         if (!(arg_flags & SHOW_EXTENDED))
     129           0 :                return 0;
     130             : 
     131           0 :         printf("%s%s%s   %s %s %s\n",
     132             :                ansi_highlight(), "[EXTENDED]", ansi_normal(),
     133             :                top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
     134           0 :         return 1;
     135             : }
     136             : 
     137           0 : static int notify_override_unchanged(const char *f) {
     138           0 :         if (!(arg_flags & SHOW_UNCHANGED))
     139           0 :                 return 0;
     140             : 
     141           0 :         printf("[UNCHANGED]  %s\n", f);
     142           0 :         return 1;
     143             : }
     144             : 
     145           0 : static int found_override(const char *top, const char *bottom) {
     146           0 :         _cleanup_free_ char *dest = NULL;
     147             :         pid_t pid;
     148             :         int r;
     149             : 
     150           0 :         assert(top);
     151           0 :         assert(bottom);
     152             : 
     153           0 :         if (null_or_empty_path(top) > 0)
     154           0 :                 return notify_override_masked(top, bottom);
     155             : 
     156           0 :         r = readlink_malloc(top, &dest);
     157           0 :         if (r >= 0) {
     158           0 :                 if (equivalent(dest, bottom) > 0)
     159           0 :                         return notify_override_equivalent(top, bottom);
     160             :                 else
     161           0 :                         return notify_override_redirected(top, bottom);
     162             :         }
     163             : 
     164           0 :         r = notify_override_overridden(top, bottom);
     165           0 :         if (!arg_diff)
     166           0 :                 return r;
     167             : 
     168           0 :         putchar('\n');
     169             : 
     170           0 :         fflush(stdout);
     171             : 
     172           0 :         r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
     173           0 :         if (r < 0)
     174           0 :                 return r;
     175           0 :         if (r == 0) {
     176           0 :                 execlp("diff", "diff", "-us", "--", bottom, top, NULL);
     177           0 :                 log_open();
     178           0 :                 log_error_errno(errno, "Failed to execute diff: %m");
     179           0 :                 _exit(EXIT_FAILURE);
     180             :         }
     181             : 
     182           0 :         (void) wait_for_terminate_and_check("diff", pid, WAIT_LOG_ABNORMAL);
     183           0 :         putchar('\n');
     184             : 
     185           0 :         return r;
     186             : }
     187             : 
     188           0 : static int enumerate_dir_d(
     189             :                 OrderedHashmap *top,
     190             :                 OrderedHashmap *bottom,
     191             :                 OrderedHashmap *drops,
     192             :                 const char *toppath, const char *drop) {
     193             : 
     194           0 :         _cleanup_free_ char *unit = NULL;
     195           0 :         _cleanup_free_ char *path = NULL;
     196           0 :         _cleanup_strv_free_ char **list = NULL;
     197             :         char **file;
     198             :         char *c;
     199             :         int r;
     200             : 
     201           0 :         assert(!endswith(drop, "/"));
     202             : 
     203           0 :         path = path_join(toppath, drop);
     204           0 :         if (!path)
     205           0 :                 return -ENOMEM;
     206             : 
     207           0 :         log_debug("Looking at %s", path);
     208             : 
     209           0 :         unit = strdup(drop);
     210           0 :         if (!unit)
     211           0 :                 return -ENOMEM;
     212             : 
     213           0 :         c = strrchr(unit, '.');
     214           0 :         if (!c)
     215           0 :                 return -EINVAL;
     216           0 :         *c = 0;
     217             : 
     218           0 :         r = get_files_in_directory(path, &list);
     219           0 :         if (r < 0)
     220           0 :                 return log_error_errno(r, "Failed to enumerate %s: %m", path);
     221             : 
     222           0 :         strv_sort(list);
     223             : 
     224           0 :         STRV_FOREACH(file, list) {
     225             :                 OrderedHashmap *h;
     226             :                 int k;
     227             :                 char *p;
     228             :                 char *d;
     229             : 
     230           0 :                 if (!endswith(*file, ".conf"))
     231           0 :                         continue;
     232             : 
     233           0 :                 p = path_join(path, *file);
     234           0 :                 if (!p)
     235           0 :                         return -ENOMEM;
     236           0 :                 d = p + strlen(toppath) + 1;
     237             : 
     238           0 :                 log_debug("Adding at top: %s %s %s", d, special_glyph(SPECIAL_GLYPH_ARROW), p);
     239           0 :                 k = ordered_hashmap_put(top, d, p);
     240           0 :                 if (k >= 0) {
     241           0 :                         p = strdup(p);
     242           0 :                         if (!p)
     243           0 :                                 return -ENOMEM;
     244           0 :                         d = p + strlen(toppath) + 1;
     245           0 :                 } else if (k != -EEXIST) {
     246           0 :                         free(p);
     247           0 :                         return k;
     248             :                 }
     249             : 
     250           0 :                 log_debug("Adding at bottom: %s %s %s", d, special_glyph(SPECIAL_GLYPH_ARROW), p);
     251           0 :                 free(ordered_hashmap_remove(bottom, d));
     252           0 :                 k = ordered_hashmap_put(bottom, d, p);
     253           0 :                 if (k < 0) {
     254           0 :                         free(p);
     255           0 :                         return k;
     256             :                 }
     257             : 
     258           0 :                 h = ordered_hashmap_get(drops, unit);
     259           0 :                 if (!h) {
     260           0 :                         h = ordered_hashmap_new(&string_hash_ops);
     261           0 :                         if (!h)
     262           0 :                                 return -ENOMEM;
     263           0 :                         ordered_hashmap_put(drops, unit, h);
     264           0 :                         unit = strdup(unit);
     265           0 :                         if (!unit)
     266           0 :                                 return -ENOMEM;
     267             :                 }
     268             : 
     269           0 :                 p = strdup(p);
     270           0 :                 if (!p)
     271           0 :                         return -ENOMEM;
     272             : 
     273           0 :                 log_debug("Adding to drops: %s %s %s %s %s",
     274             :                           unit, special_glyph(SPECIAL_GLYPH_ARROW), basename(p), special_glyph(SPECIAL_GLYPH_ARROW), p);
     275           0 :                 k = ordered_hashmap_put(h, basename(p), p);
     276           0 :                 if (k < 0) {
     277           0 :                         free(p);
     278           0 :                         if (k != -EEXIST)
     279           0 :                                 return k;
     280             :                 }
     281             :         }
     282           0 :         return 0;
     283             : }
     284             : 
     285           0 : static int enumerate_dir(
     286             :                 OrderedHashmap *top,
     287             :                 OrderedHashmap *bottom,
     288             :                 OrderedHashmap *drops,
     289             :                 const char *path, bool dropins) {
     290             : 
     291           0 :         _cleanup_closedir_ DIR *d = NULL;
     292             :         struct dirent *de;
     293           0 :         _cleanup_strv_free_ char **files = NULL, **dirs = NULL;
     294           0 :         size_t n_files = 0, allocated_files = 0, n_dirs = 0, allocated_dirs = 0;
     295             :         char **t;
     296             :         int r;
     297             : 
     298           0 :         assert(top);
     299           0 :         assert(bottom);
     300           0 :         assert(drops);
     301           0 :         assert(path);
     302             : 
     303           0 :         log_debug("Looking at %s", path);
     304             : 
     305           0 :         d = opendir(path);
     306           0 :         if (!d) {
     307           0 :                 if (errno == ENOENT)
     308           0 :                         return 0;
     309             : 
     310           0 :                 return log_error_errno(errno, "Failed to open %s: %m", path);
     311             :         }
     312             : 
     313           0 :         FOREACH_DIRENT_ALL(de, d, return -errno) {
     314           0 :                 dirent_ensure_type(d, de);
     315             : 
     316           0 :                 if (dropins && de->d_type == DT_DIR && endswith(de->d_name, ".d")) {
     317           0 :                         if (!GREEDY_REALLOC0(dirs, allocated_dirs, n_dirs + 2))
     318           0 :                                 return -ENOMEM;
     319             : 
     320           0 :                         dirs[n_dirs] = strdup(de->d_name);
     321           0 :                         if (!dirs[n_dirs])
     322           0 :                                 return -ENOMEM;
     323           0 :                         n_dirs ++;
     324             :                 }
     325             : 
     326           0 :                 if (!dirent_is_file(de))
     327           0 :                         continue;
     328             : 
     329           0 :                 if (!GREEDY_REALLOC0(files, allocated_files, n_files + 2))
     330           0 :                         return -ENOMEM;
     331             : 
     332           0 :                 files[n_files] = strdup(de->d_name);
     333           0 :                 if (!files[n_files])
     334           0 :                         return -ENOMEM;
     335           0 :                 n_files ++;
     336             :         }
     337             : 
     338           0 :         strv_sort(dirs);
     339           0 :         strv_sort(files);
     340             : 
     341           0 :         STRV_FOREACH(t, dirs) {
     342           0 :                 r = enumerate_dir_d(top, bottom, drops, path, *t);
     343           0 :                 if (r < 0)
     344           0 :                         return r;
     345             :         }
     346             : 
     347           0 :         STRV_FOREACH(t, files) {
     348           0 :                 _cleanup_free_ char *p = NULL;
     349             : 
     350           0 :                 p = path_join(path, *t);
     351           0 :                 if (!p)
     352           0 :                         return -ENOMEM;
     353             : 
     354           0 :                 log_debug("Adding at top: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW), p);
     355           0 :                 r = ordered_hashmap_put(top, basename(p), p);
     356           0 :                 if (r >= 0) {
     357           0 :                         p = strdup(p);
     358           0 :                         if (!p)
     359           0 :                                 return -ENOMEM;
     360           0 :                 } else if (r != -EEXIST)
     361           0 :                         return r;
     362             : 
     363           0 :                 log_debug("Adding at bottom: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW), p);
     364           0 :                 free(ordered_hashmap_remove(bottom, basename(p)));
     365           0 :                 r = ordered_hashmap_put(bottom, basename(p), p);
     366           0 :                 if (r < 0)
     367           0 :                         return r;
     368           0 :                 p = NULL;
     369             :         }
     370             : 
     371           0 :         return 0;
     372             : }
     373             : 
     374           0 : static int should_skip_path(const char *prefix, const char *suffix) {
     375             : #if HAVE_SPLIT_USR
     376             :         _cleanup_free_ char *target = NULL;
     377             :         const char *dirname, *p;
     378             : 
     379             :         dirname = prefix_roota(prefix, suffix);
     380             : 
     381             :         if (chase_symlinks(dirname, NULL, 0, &target) < 0)
     382             :                 return false;
     383             : 
     384             :         NULSTR_FOREACH(p, prefixes) {
     385             :                 _cleanup_free_ char *tmp = NULL;
     386             : 
     387             :                 if (path_startswith(dirname, p))
     388             :                         continue;
     389             : 
     390             :                 tmp = path_join(p, suffix);
     391             :                 if (!tmp)
     392             :                         return -ENOMEM;
     393             : 
     394             :                 if (path_equal(target, tmp)) {
     395             :                         log_debug("%s redirects to %s, skipping.", dirname, target);
     396             :                         return true;
     397             :                 }
     398             :         }
     399             : #endif
     400           0 :         return false;
     401             : }
     402             : 
     403           0 : static int process_suffix(const char *suffix, const char *onlyprefix) {
     404             :         const char *p;
     405             :         char *f;
     406             :         OrderedHashmap *top, *bottom, *drops;
     407             :         OrderedHashmap *h;
     408             :         char *key;
     409           0 :         int r = 0, k;
     410             :         Iterator i, j;
     411           0 :         int n_found = 0;
     412             :         bool dropins;
     413             : 
     414           0 :         assert(suffix);
     415           0 :         assert(!startswith(suffix, "/"));
     416           0 :         assert(!strstr(suffix, "//"));
     417             : 
     418           0 :         dropins = nulstr_contains(have_dropins, suffix);
     419             : 
     420           0 :         top = ordered_hashmap_new(&string_hash_ops);
     421           0 :         bottom = ordered_hashmap_new(&string_hash_ops);
     422           0 :         drops = ordered_hashmap_new(&string_hash_ops);
     423           0 :         if (!top || !bottom || !drops) {
     424           0 :                 r = -ENOMEM;
     425           0 :                 goto finish;
     426             :         }
     427             : 
     428           0 :         NULSTR_FOREACH(p, prefixes) {
     429           0 :                 _cleanup_free_ char *t = NULL;
     430             : 
     431           0 :                 if (should_skip_path(p, suffix) > 0)
     432           0 :                         continue;
     433             : 
     434           0 :                 t = path_join(p, suffix);
     435           0 :                 if (!t) {
     436           0 :                         r = -ENOMEM;
     437           0 :                         goto finish;
     438             :                 }
     439             : 
     440           0 :                 k = enumerate_dir(top, bottom, drops, t, dropins);
     441           0 :                 if (r == 0)
     442           0 :                         r = k;
     443             :         }
     444             : 
     445           0 :         ORDERED_HASHMAP_FOREACH_KEY(f, key, top, i) {
     446             :                 char *o;
     447             : 
     448           0 :                 o = ordered_hashmap_get(bottom, key);
     449           0 :                 assert(o);
     450             : 
     451           0 :                 if (!onlyprefix || startswith(o, onlyprefix)) {
     452           0 :                         if (path_equal(o, f)) {
     453           0 :                                 notify_override_unchanged(f);
     454             :                         } else {
     455           0 :                                 k = found_override(f, o);
     456           0 :                                 if (k < 0)
     457           0 :                                         r = k;
     458             :                                 else
     459           0 :                                         n_found += k;
     460             :                         }
     461             :                 }
     462             : 
     463           0 :                 h = ordered_hashmap_get(drops, key);
     464           0 :                 if (h)
     465           0 :                         ORDERED_HASHMAP_FOREACH(o, h, j)
     466           0 :                                 if (!onlyprefix || startswith(o, onlyprefix))
     467           0 :                                         n_found += notify_override_extended(f, o);
     468             :         }
     469             : 
     470           0 : finish:
     471           0 :         ordered_hashmap_free_free(top);
     472           0 :         ordered_hashmap_free_free(bottom);
     473             : 
     474           0 :         ORDERED_HASHMAP_FOREACH_KEY(h, key, drops, i) {
     475           0 :                 ordered_hashmap_free_free(ordered_hashmap_remove(drops, key));
     476           0 :                 ordered_hashmap_remove(drops, key);
     477           0 :                 free(key);
     478             :         }
     479           0 :         ordered_hashmap_free(drops);
     480             : 
     481           0 :         return r < 0 ? r : n_found;
     482             : }
     483             : 
     484           0 : static int process_suffixes(const char *onlyprefix) {
     485             :         const char *n;
     486           0 :         int n_found = 0, r;
     487             : 
     488           0 :         NULSTR_FOREACH(n, suffixes) {
     489           0 :                 r = process_suffix(n, onlyprefix);
     490           0 :                 if (r < 0)
     491           0 :                         return r;
     492             : 
     493           0 :                 n_found += r;
     494             :         }
     495             : 
     496           0 :         return n_found;
     497             : }
     498             : 
     499           0 : static int process_suffix_chop(const char *arg) {
     500             :         const char *p;
     501             : 
     502           0 :         assert(arg);
     503             : 
     504           0 :         if (!path_is_absolute(arg))
     505           0 :                 return process_suffix(arg, NULL);
     506             : 
     507             :         /* Strip prefix from the suffix */
     508           0 :         NULSTR_FOREACH(p, prefixes) {
     509             :                 const char *suffix;
     510             : 
     511           0 :                 suffix = startswith(arg, p);
     512           0 :                 if (suffix) {
     513           0 :                         suffix += strspn(suffix, "/");
     514           0 :                         if (*suffix)
     515           0 :                                 return process_suffix(suffix, p);
     516             :                         else
     517           0 :                                 return process_suffixes(arg);
     518             :                 }
     519             :         }
     520             : 
     521           0 :         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
     522             :                                "Invalid suffix specification %s.", arg);
     523             : }
     524             : 
     525           3 : static int help(void) {
     526           3 :         _cleanup_free_ char *link = NULL;
     527             :         int r;
     528             : 
     529           3 :         r = terminal_urlify_man("systemd-delta", "1", &link);
     530           3 :         if (r < 0)
     531           0 :                 return log_oom();
     532             : 
     533           3 :         printf("%s [OPTIONS...] [SUFFIX...]\n\n"
     534             :                "Find overridden configuration files.\n\n"
     535             :                "  -h --help           Show this help\n"
     536             :                "     --version        Show package version\n"
     537             :                "     --no-pager       Do not pipe output into a pager\n"
     538             :                "     --diff[=1|0]     Show a diff when overridden files differ\n"
     539             :                "  -t --type=LIST...   Only display a selected set of override types\n"
     540             :                "\nSee the %s for details.\n"
     541             :                , program_invocation_short_name
     542             :                , link
     543             :         );
     544             : 
     545           3 :         return 0;
     546             : }
     547             : 
     548           0 : static int parse_flags(const char *flag_str, int flags) {
     549             :         const char *word, *state;
     550             :         size_t l;
     551             : 
     552           0 :         FOREACH_WORD_SEPARATOR(word, l, flag_str, ",", state) {
     553           0 :                 if (strneq("masked", word, l))
     554           0 :                         flags |= SHOW_MASKED;
     555           0 :                 else if (strneq ("equivalent", word, l))
     556           0 :                         flags |= SHOW_EQUIVALENT;
     557           0 :                 else if (strneq("redirected", word, l))
     558           0 :                         flags |= SHOW_REDIRECTED;
     559           0 :                 else if (strneq("overridden", word, l))
     560           0 :                         flags |= SHOW_OVERRIDDEN;
     561           0 :                 else if (strneq("unchanged", word, l))
     562           0 :                         flags |= SHOW_UNCHANGED;
     563           0 :                 else if (strneq("extended", word, l))
     564           0 :                         flags |= SHOW_EXTENDED;
     565           0 :                 else if (strneq("default", word, l))
     566           0 :                         flags |= SHOW_DEFAULTS;
     567             :                 else
     568           0 :                         return -EINVAL;
     569             :         }
     570           0 :         return flags;
     571             : }
     572             : 
     573           4 : static int parse_argv(int argc, char *argv[]) {
     574             : 
     575             :         enum {
     576             :                 ARG_NO_PAGER = 0x100,
     577             :                 ARG_DIFF,
     578             :                 ARG_VERSION
     579             :         };
     580             : 
     581             :         static const struct option options[] = {
     582             :                 { "help",      no_argument,       NULL, 'h'          },
     583             :                 { "version",   no_argument,       NULL, ARG_VERSION  },
     584             :                 { "no-pager",  no_argument,       NULL, ARG_NO_PAGER },
     585             :                 { "diff",      optional_argument, NULL, ARG_DIFF     },
     586             :                 { "type",      required_argument, NULL, 't'          },
     587             :                 {}
     588             :         };
     589             : 
     590             :         int c;
     591             : 
     592           4 :         assert(argc >= 1);
     593           4 :         assert(argv);
     594             : 
     595           4 :         while ((c = getopt_long(argc, argv, "ht:", options, NULL)) >= 0)
     596             : 
     597           4 :                 switch (c) {
     598             : 
     599           3 :                 case 'h':
     600           3 :                         return help();
     601             : 
     602           0 :                 case ARG_VERSION:
     603           0 :                         return version();
     604             : 
     605           0 :                 case ARG_NO_PAGER:
     606           0 :                         arg_pager_flags |= PAGER_DISABLE;
     607           0 :                         break;
     608             : 
     609           0 :                 case 't': {
     610             :                         int f;
     611           0 :                         f = parse_flags(optarg, arg_flags);
     612           0 :                         if (f < 0)
     613           0 :                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
     614             :                                                        "Failed to parse flags field.");
     615           0 :                         arg_flags = f;
     616           0 :                         break;
     617             :                 }
     618             : 
     619           0 :                 case ARG_DIFF:
     620           0 :                         if (!optarg)
     621           0 :                                 arg_diff = 1;
     622             :                         else {
     623             :                                 int b;
     624             : 
     625           0 :                                 b = parse_boolean(optarg);
     626           0 :                                 if (b < 0)
     627           0 :                                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
     628             :                                                                "Failed to parse diff boolean.");
     629             : 
     630           0 :                                 arg_diff = b;
     631             :                         }
     632           0 :                         break;
     633             : 
     634           1 :                 case '?':
     635           1 :                         return -EINVAL;
     636             : 
     637           0 :                 default:
     638           0 :                         assert_not_reached("Unhandled option");
     639             :                 }
     640             : 
     641           0 :         return 1;
     642             : }
     643             : 
     644           4 : static int run(int argc, char *argv[]) {
     645           4 :         int r, k, n_found = 0;
     646             : 
     647           4 :         log_show_color(true);
     648           4 :         log_parse_environment();
     649           4 :         log_open();
     650             : 
     651           4 :         r = parse_argv(argc, argv);
     652           4 :         if (r <= 0)
     653           4 :                 return r;
     654             : 
     655           0 :         if (arg_flags == 0)
     656           0 :                 arg_flags = SHOW_DEFAULTS;
     657             : 
     658           0 :         if (arg_diff < 0)
     659           0 :                 arg_diff = !!(arg_flags & SHOW_OVERRIDDEN);
     660           0 :         else if (arg_diff)
     661           0 :                 arg_flags |= SHOW_OVERRIDDEN;
     662             : 
     663           0 :         (void) pager_open(arg_pager_flags);
     664             : 
     665           0 :         if (optind < argc) {
     666             :                 int i;
     667             : 
     668           0 :                 for (i = optind; i < argc; i++) {
     669           0 :                         path_simplify(argv[i], false);
     670             : 
     671           0 :                         k = process_suffix_chop(argv[i]);
     672           0 :                         if (k < 0)
     673           0 :                                 r = k;
     674             :                         else
     675           0 :                                 n_found += k;
     676             :                 }
     677             : 
     678             :         } else {
     679           0 :                 k = process_suffixes(NULL);
     680           0 :                 if (k < 0)
     681           0 :                         r = k;
     682             :                 else
     683           0 :                         n_found += k;
     684             :         }
     685             : 
     686           0 :         if (r >= 0)
     687           0 :                 printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found);
     688           0 :         return r;
     689             : }
     690             : 
     691           4 : DEFINE_MAIN_FUNCTION(run);

Generated by: LCOV version 1.14