1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "analyze.h"
4 #include "analyze-verify.h"
5 #include "analyze-verify-util.h"
6 #include "copy.h"
7 #include "rm-rf.h"
8 #include "tmpfile-util.h"
9 
process_aliases(char * argv[],char * tempdir,char *** ret)10 static int process_aliases(char *argv[], char *tempdir, char ***ret) {
11         _cleanup_strv_free_ char **filenames = NULL;
12         int r;
13 
14         assert(argv);
15         assert(tempdir);
16         assert(ret);
17 
18         STRV_FOREACH(filename, strv_skip(argv, 1)) {
19                 _cleanup_free_ char *src = NULL, *dst = NULL, *base = NULL;
20                 const char *parse_arg;
21 
22                 parse_arg = *filename;
23                 r = extract_first_word(&parse_arg, &src, ":", EXTRACT_DONT_COALESCE_SEPARATORS|EXTRACT_RETAIN_ESCAPE);
24                 if (r < 0)
25                         return r;
26 
27                 if (!parse_arg) {
28                         r = strv_consume(&filenames, TAKE_PTR(src));
29                         if (r < 0)
30                                 return r;
31 
32                         continue;
33                 }
34 
35                 r = path_extract_filename(parse_arg, &base);
36                 if (r < 0)
37                         return r;
38 
39                 dst = path_join(tempdir, base);
40                 if (!dst)
41                         return -ENOMEM;
42 
43                 r = copy_file(src, dst, 0, 0644, 0, 0, COPY_REFLINK);
44                 if (r < 0)
45                         return r;
46 
47                 r = strv_consume(&filenames, TAKE_PTR(dst));
48                 if (r < 0)
49                         return r;
50         }
51 
52         *ret = TAKE_PTR(filenames);
53         return 0;
54 }
55 
verb_verify(int argc,char * argv[],void * userdata)56 int verb_verify(int argc, char *argv[], void *userdata) {
57         _cleanup_strv_free_ char **filenames = NULL;
58         _cleanup_(rm_rf_physical_and_freep) char *tempdir = NULL;
59         int r;
60 
61         r = mkdtemp_malloc("/tmp/systemd-analyze-XXXXXX", &tempdir);
62         if (r < 0)
63                 return log_error_errno(r, "Failed to setup working directory: %m");
64 
65         r = process_aliases(argv, tempdir, &filenames);
66         if (r < 0)
67                 return log_error_errno(r, "Couldn't process aliases: %m");
68 
69         return verify_units(filenames, arg_scope, arg_man, arg_generators, arg_recursive_errors, arg_root);
70 }
71