-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathdracut-install.c
2099 lines (1782 loc) · 77.8 KB
/
dracut-install.c
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* dracut-install.c -- install files and executables
Copyright (C) 2012 Harald Hoyer
Copyright (C) 2012 Red Hat, Inc. All rights reserved.
This program is free software: you can redistribute it and/or modify
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; If not, see <http://www.gnu.org/licenses/>.
*/
#define PROGRAM_VERSION_STRING "2"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <glob.h>
#include <libgen.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <libkmod.h>
#include <fts.h>
#include <regex.h>
#include <sys/utsname.h>
#include "log.h"
#include "hashmap.h"
#include "util.h"
#include "strv.h"
#define _asprintf(strp, fmt, ...) \
do { \
if (dracut_asprintf(strp, fmt, __VA_ARGS__) < 0) { \
log_error("Out of memory\n"); \
exit(EXIT_FAILURE); \
} \
} while (0)
static bool arg_hmac = false;
static bool arg_createdir = false;
static int arg_loglevel = -1;
static bool arg_optional = false;
static bool arg_silent = false;
static bool arg_all = false;
static bool arg_module = false;
static bool arg_modalias = false;
static bool arg_resolvelazy = false;
static bool arg_resolvedeps = false;
static bool arg_hostonly = false;
static bool no_xattr = false;
static char *destrootdir = NULL;
static char *sysrootdir = NULL;
static size_t sysrootdirlen = 0;
static char *kerneldir = NULL;
static size_t kerneldirlen = 0;
static char **firmwaredirs = NULL;
static char **pathdirs;
static char *ldd = NULL;
static char *logdir = NULL;
static char *logfile = NULL;
FILE *logfile_f = NULL;
static Hashmap *items = NULL;
static Hashmap *items_failed = NULL;
static Hashmap *modules_loaded = NULL;
static regex_t mod_filter_path;
static regex_t mod_filter_nopath;
static regex_t mod_filter_symbol;
static regex_t mod_filter_nosymbol;
static regex_t mod_filter_noname;
static bool arg_mod_filter_path = false;
static bool arg_mod_filter_nopath = false;
static bool arg_mod_filter_symbol = false;
static bool arg_mod_filter_nosymbol = false;
static bool arg_mod_filter_noname = false;
static int dracut_install(const char *src, const char *dst, bool isdir, bool resolvedeps, bool hashdst);
static inline void kmod_module_unrefp(struct kmod_module **p)
{
if (*p)
kmod_module_unref(*p);
}
#define _cleanup_kmod_module_unref_ _cleanup_(kmod_module_unrefp)
static inline void kmod_module_unref_listp(struct kmod_list **p)
{
if (*p)
kmod_module_unref_list(*p);
}
#define _cleanup_kmod_module_unref_list_ _cleanup_(kmod_module_unref_listp)
static inline void kmod_module_info_free_listp(struct kmod_list **p)
{
if (*p)
kmod_module_info_free_list(*p);
}
#define _cleanup_kmod_module_info_free_list_ _cleanup_(kmod_module_info_free_listp)
static inline void kmod_unrefp(struct kmod_ctx **p)
{
kmod_unref(*p);
}
#define _cleanup_kmod_unref_ _cleanup_(kmod_unrefp)
static inline void kmod_module_dependency_symbols_free_listp(struct kmod_list **p)
{
if (*p)
kmod_module_dependency_symbols_free_list(*p);
}
#define _cleanup_kmod_module_dependency_symbols_free_list_ _cleanup_(kmod_module_dependency_symbols_free_listp)
static inline void fts_closep(FTS **p)
{
if (*p)
fts_close(*p);
}
#define _cleanup_fts_close_ _cleanup_(fts_closep)
#define _cleanup_globfree_ _cleanup_(globfree)
static size_t dir_len(char const *file)
{
size_t length;
if (!file)
return 0;
/* Strip the basename and any redundant slashes before it. */
for (length = strlen(file) - 1; 0 < length; length--)
if (file[length] == '/' && file[length - 1] != '/')
break;
return length;
}
static char *convert_abs_rel(const char *from, const char *target)
{
/* we use the 4*MAXPATHLEN, which should not overrun */
char buf[MAXPATHLEN * 4];
_cleanup_free_ char *realtarget = NULL, *realfrom = NULL, *from_dir_p = NULL;
_cleanup_free_ char *target_dir_p = NULL;
size_t level = 0, fromlevel = 0, targetlevel = 0;
int l;
size_t i, rl, dirlen;
dirlen = dir_len(from);
from_dir_p = strndup(from, dirlen);
if (!from_dir_p)
return strdup(from + strlen(destrootdir));
if (realpath(from_dir_p, buf) == NULL) {
log_warning("convert_abs_rel(): from '%s' directory has no realpath: %m", from);
return strdup(from + strlen(destrootdir));
}
/* dir_len() skips double /'s e.g. //lib64, so we can't skip just one
* character - need to skip all leading /'s */
for (i = dirlen + 1; from[i] == '/'; ++i)
;
_asprintf(&realfrom, "%s/%s", buf, from + i);
dirlen = dir_len(target);
target_dir_p = strndup(target, dirlen);
if (!target_dir_p)
return strdup(from + strlen(destrootdir));
if (realpath(target_dir_p, buf) == NULL) {
log_warning("convert_abs_rel(): target '%s' directory has no realpath: %m", target);
return strdup(from + strlen(destrootdir));
}
for (i = dirlen + 1; target[i] == '/'; ++i)
;
_asprintf(&realtarget, "%s/%s", buf, target + i);
/* now calculate the relative path from <from> to <target> and
store it in <buf>
*/
rl = 0;
/* count the pathname elements of realtarget */
for (targetlevel = 0, i = 0; realtarget[i]; i++)
if (realtarget[i] == '/')
targetlevel++;
/* count the pathname elements of realfrom */
for (fromlevel = 0, i = 0; realfrom[i]; i++)
if (realfrom[i] == '/')
fromlevel++;
/* count the pathname elements, which are common for both paths */
for (level = 0, i = 0; realtarget[i] && (realtarget[i] == realfrom[i]); i++)
if (realtarget[i] == '/')
level++;
/* add "../" to the buf path, until the common pathname is
reached */
for (i = level; i < targetlevel; i++) {
if (i != level)
buf[rl++] = '/';
buf[rl++] = '.';
buf[rl++] = '.';
}
/* set l to the next uncommon pathname element in realfrom */
for (l = 1, i = 1; i < level; i++)
for (l++; realfrom[l] && realfrom[l] != '/'; l++) ;
/* skip next '/' */
l++;
/* append the uncommon rest of realfrom to the buf path */
for (i = level; i <= fromlevel; i++) {
if (rl)
buf[rl++] = '/';
while (realfrom[l] && realfrom[l] != '/')
buf[rl++] = realfrom[l++];
l++;
}
buf[rl] = 0;
return strdup(buf);
}
static int ln_r(const char *src, const char *dst)
{
int ret;
_cleanup_free_ const char *points_to = convert_abs_rel(src, dst);
log_info("ln -s '%s' '%s'", points_to, dst);
ret = symlink(points_to, dst);
if (ret != 0) {
log_error("ERROR: ln -s '%s' '%s': %m", points_to, dst);
return 1;
}
return 0;
}
/* Perform the O(1) btrfs clone operation, if possible.
Upon success, return 0. Otherwise, return -1 and set errno. */
static inline int clone_file(int dest_fd, int src_fd)
{
#undef BTRFS_IOCTL_MAGIC
#define BTRFS_IOCTL_MAGIC 0x94
#undef BTRFS_IOC_CLONE
#define BTRFS_IOC_CLONE _IOW (BTRFS_IOCTL_MAGIC, 9, int)
return ioctl(dest_fd, BTRFS_IOC_CLONE, src_fd);
}
static bool use_clone = true;
static int cp(const char *src, const char *dst)
{
int pid;
int ret = 0;
if (use_clone) {
struct stat sb;
_cleanup_close_ int dest_desc = -1, source_desc = -1;
if (lstat(src, &sb) != 0)
goto normal_copy;
if (S_ISLNK(sb.st_mode))
goto normal_copy;
source_desc = open(src, O_RDONLY | O_CLOEXEC);
if (source_desc < 0)
goto normal_copy;
dest_desc =
open(dst, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC,
(sb.st_mode) & (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO));
if (dest_desc < 0) {
goto normal_copy;
}
ret = clone_file(dest_desc, source_desc);
if (ret == 0) {
struct timeval tv[2];
if (fchown(dest_desc, sb.st_uid, sb.st_gid) != 0)
if (fchown(dest_desc, (uid_t) - 1, sb.st_gid) != 0) {
if (geteuid() == 0)
log_error("Failed to chown %s: %m", dst);
else
log_info("Failed to chown %s: %m", dst);
}
tv[0].tv_sec = sb.st_atime;
tv[0].tv_usec = 0;
tv[1].tv_sec = sb.st_mtime;
tv[1].tv_usec = 0;
futimes(dest_desc, tv);
return ret;
}
close(dest_desc);
dest_desc = -1;
/* clone did not work, remove the file */
unlink(dst);
/* do not try clone again */
use_clone = false;
}
normal_copy:
pid = fork();
const char *preservation = (geteuid() == 0
&& no_xattr == false) ? "--preserve=mode,xattr,timestamps,ownership" : "--preserve=mode,timestamps,ownership";
if (pid == 0) {
execlp("cp", "cp", "--reflink=auto", "--sparse=auto", preservation, "-fL", src, dst, NULL);
_exit(errno == ENOENT ? 127 : 126);
}
while (waitpid(pid, &ret, 0) == -1) {
if (errno != EINTR) {
log_error("ERROR: waitpid() failed: %m");
return 1;
}
}
ret = WIFSIGNALED(ret) ? 128 + WTERMSIG(ret) : WEXITSTATUS(ret);
if (ret != 0)
log_error("ERROR: 'cp --reflink=auto --sparse=auto %s -fL %s %s' failed with %d", preservation, src, dst, ret);
log_debug("cp ret = %d", ret);
return ret;
}
static int library_install(const char *src, const char *lib)
{
_cleanup_free_ char *p = NULL;
_cleanup_free_ char *pdir = NULL, *ppdir = NULL, *pppdir = NULL, *clib = NULL;
char *q, *clibdir;
int r, ret = 0;
r = dracut_install(lib, lib, false, false, true);
if (r != 0)
log_error("ERROR: failed to install '%s' for '%s'", lib, src);
else
log_debug("Lib install: '%s'", lib);
ret += r;
/* also install lib.so for lib.so.* files */
q = strstr(lib, ".so.");
if (q) {
p = strndup(lib, q - lib + 3);
/* ignore errors for base lib symlink */
if (dracut_install(p, p, false, false, true) == 0)
log_debug("Lib install: '%s'", p);
free(p);
}
/* Also try to install the same library from one directory above
* or from one directory above glibc-hwcaps.
This fixes the case, where only the HWCAP lib would be installed
# ldconfig -p|grep -F libc.so
libc.so.6 (libc6,64bit, hwcap: 0x0000001000000000, OS ABI: Linux 2.6.32) => /lib64/power6/libc.so.6
libc.so.6 (libc6,64bit, hwcap: 0x0000000000000200, OS ABI: Linux 2.6.32) => /lib64/power6x/libc.so.6
libc.so.6 (libc6,64bit, OS ABI: Linux 2.6.32) => /lib64/libc.so.6
*/
p = strdup(lib);
pdir = dirname_malloc(p);
if (!pdir)
return ret;
ppdir = dirname_malloc(pdir);
/* only one parent directory, not HWCAP library */
if (!ppdir || streq(ppdir, "/"))
return ret;
pppdir = dirname_malloc(ppdir);
if (!pppdir)
return ret;
clibdir = streq(basename(ppdir), "glibc-hwcaps") ? pppdir : ppdir;
clib = strjoin(clibdir, "/", basename(p), NULL);
if (dracut_install(clib, clib, false, false, true) == 0)
log_debug("Lib install: '%s'", clib);
/* also install lib.so for lib.so.* files */
q = strstr(clib, ".so.");
if (q) {
q[3] = '\0';
/* ignore errors for base lib symlink */
if (dracut_install(clib, clib, false, false, true) == 0)
log_debug("Lib install: '%s'", p);
}
return ret;
}
static char *get_real_file(const char *src, bool fullyresolve)
{
struct stat sb;
ssize_t linksz;
char linktarget[PATH_MAX + 1];
_cleanup_free_ char *fullsrcpath_a = NULL;
const char *fullsrcpath;
_cleanup_free_ char *abspath = NULL;
if (sysrootdirlen) {
if (strncmp(src, sysrootdir, sysrootdirlen) == 0) {
fullsrcpath = src;
} else {
_asprintf(&fullsrcpath_a, "%s/%s",
(sysrootdirlen ? sysrootdir : ""),
(src[0] == '/' ? src + 1 : src));
fullsrcpath = fullsrcpath_a;
}
} else {
fullsrcpath = src;
}
log_debug("get_real_file('%s')", fullsrcpath);
if (lstat(fullsrcpath, &sb) < 0)
return NULL;
switch (sb.st_mode & S_IFMT) {
case S_IFDIR:
case S_IFREG:
return strdup(fullsrcpath);
case S_IFLNK:
break;
default:
return NULL;
}
linksz = readlink(fullsrcpath, linktarget, sizeof(linktarget));
if (linksz < 0)
return NULL;
linktarget[linksz] = '\0';
log_debug("get_real_file: readlink('%s') returns '%s'", fullsrcpath, linktarget);
if (linktarget[0] == '/') {
_asprintf(&abspath, "%s%s", (sysrootdirlen ? sysrootdir : ""), linktarget);
} else {
_asprintf(&abspath, "%.*s/%s", (int)dir_len(fullsrcpath), fullsrcpath, linktarget);
}
if (fullyresolve) {
struct stat st;
if (lstat(abspath, &st) < 0) {
if (errno != ENOENT) {
return NULL;
}
}
if (S_ISLNK(st.st_mode)) {
return get_real_file(abspath, fullyresolve);
}
}
log_debug("get_real_file('%s') => '%s'", src, abspath);
return TAKE_PTR(abspath);
}
static int resolve_deps(const char *src)
{
int ret = 0, err;
_cleanup_free_ char *buf = NULL;
size_t linesize = LINE_MAX + 1;
_cleanup_free_ char *fullsrcpath = NULL;
fullsrcpath = get_real_file(src, true);
log_debug("resolve_deps('%s') -> get_real_file('%s', true) = '%s'", src, src, fullsrcpath);
if (!fullsrcpath)
return 0;
buf = malloc(linesize);
if (buf == NULL)
return -errno;
if (strstr(src, ".so") == NULL) {
_cleanup_close_ int fd = -1;
fd = open(fullsrcpath, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return -errno;
ret = read(fd, buf, linesize - 1);
if (ret == -1)
return -errno;
buf[ret] = '\0';
if (buf[0] == '#' && buf[1] == '!') {
/* we have a shebang */
char *p, *q;
for (p = &buf[2]; *p && isspace(*p); p++) ;
for (q = p; *q && (!isspace(*q)); q++) ;
*q = '\0';
log_debug("Script install: '%s'", p);
ret = dracut_install(p, p, false, true, false);
if (ret != 0)
log_error("ERROR: failed to install '%s'", p);
return ret;
}
}
int fds[2];
FILE *fptr;
if (pipe2(fds, O_CLOEXEC) == -1 || (fptr = fdopen(fds[0], "r")) == NULL) {
log_error("ERROR: pipe stream initialization for '%s' failed: %m", ldd);
exit(EXIT_FAILURE);
}
log_debug("%s %s", ldd, fullsrcpath);
pid_t ldd_pid;
if ((ldd_pid = fork()) == 0) {
dup2(fds[1], 1);
dup2(fds[1], 2);
putenv("LC_ALL=C");
execlp(ldd, ldd, fullsrcpath, (char *)NULL);
_exit(errno == ENOENT ? 127 : 126);
}
close(fds[1]);
ret = 0;
while (getline(&buf, &linesize, fptr) >= 0) {
char *p;
log_debug("ldd: '%s'", buf);
if (strstr(buf, "you do not have execution permission")) {
log_error("%s", buf);
ret += 1;
break;
}
/* errors from cross-compiler-ldd */
if (strstr(buf, "unable to find sysroot")) {
log_error("%s", buf);
ret += 1;
break;
}
/* musl ldd */
if (strstr(buf, "Not a valid dynamic program"))
break;
/* glibc */
if (strstr(buf, "cannot execute binary file"))
break;
if (strstr(buf, "not a dynamic executable"))
break;
if (strstr(buf, "loader cannot load itself"))
break;
if (strstr(buf, "not regular file"))
break;
if (strstr(buf, "cannot read header"))
break;
if (strstr(buf, "cannot be preloaded"))
break;
if (strstr(buf, destrootdir))
break;
p = buf;
if (strchr(p, '$')) {
/* take ldd variable expansion into account */
p = strstr(p, "=>");
if (!p)
p = buf;
}
p = strchr(p, '/');
if (p) {
char *q;
for (q = p; *q && *q != ' ' && *q != '\n'; q++) ;
*q = '\0';
ret += library_install(src, p);
}
}
fclose(fptr);
while (waitpid(ldd_pid, &err, 0) == -1) {
if (errno != EINTR) {
log_error("ERROR: waitpid() failed: %m");
return 1;
}
}
err = WIFSIGNALED(err) ? 128 + WTERMSIG(err) : WEXITSTATUS(err);
/* ldd has error conditions we largely don't care about ("not a dynamic executable", &c.):
only error out on hard errors (ENOENT, ENOEXEC, signals) */
if (err >= 126) {
log_error("ERROR: '%s %s' failed with %d", ldd, fullsrcpath, err);
return err;
} else
return ret;
}
/* Install ".<filename>.hmac" file for FIPS self-checks */
static int hmac_install(const char *src, const char *dst, const char *hmacpath)
{
_cleanup_free_ char *srchmacname = NULL;
_cleanup_free_ char *dsthmacname = NULL;
size_t dlen = dir_len(src);
if (endswith(src, ".hmac"))
return 0;
if (!hmacpath) {
hmac_install(src, dst, "/lib/fipscheck");
hmac_install(src, dst, "/lib64/fipscheck");
hmac_install(src, dst, "/lib/hmaccalc");
hmac_install(src, dst, "/lib64/hmaccalc");
}
if (hmacpath) {
_asprintf(&srchmacname, "%s/%s.hmac", hmacpath, &src[dlen + 1]);
_asprintf(&dsthmacname, "%s/%s.hmac", hmacpath, &src[dlen + 1]);
} else {
_asprintf(&srchmacname, "%.*s/.%s.hmac", (int)dlen, src, &src[dlen + 1]);
_asprintf(&dsthmacname, "%.*s/.%s.hmac", (int)dir_len(dst), dst, &src[dlen + 1]);
}
log_debug("hmac cp '%s' '%s'", srchmacname, dsthmacname);
dracut_install(srchmacname, dsthmacname, false, false, true);
return 0;
}
void mark_hostonly(const char *path)
{
_cleanup_free_ char *fulldstpath = NULL;
_cleanup_fclose_ FILE *f = NULL;
_asprintf(&fulldstpath, "%s/lib/dracut/hostonly-files", destrootdir);
f = fopen(fulldstpath, "a");
if (f == NULL) {
log_error("Could not open '%s' for writing.", fulldstpath);
return;
}
fprintf(f, "%s\n", path);
}
void dracut_log_cp(const char *path)
{
int ret;
ret = fprintf(logfile_f, "%s\n", path);
if (ret < 0)
log_error("Could not append '%s' to logfile '%s': %m", path, logfile);
}
static bool check_hashmap(Hashmap *hm, const char *item)
{
char *existing;
existing = hashmap_get(hm, item);
if (existing) {
if (strcmp(existing, item) == 0) {
return true;
}
}
return false;
}
static int dracut_mkdir(const char *src)
{
_cleanup_free_ char *parent = NULL;
char *path;
struct stat sb;
parent = strdup(src);
if (!parent)
return 1;
path = parent[0] == '/' ? parent + 1 : parent;
while (path) {
path = strstr(path, "/");
if (path)
*path = '\0';
if (stat(parent, &sb) == 0) {
if (!S_ISDIR(sb.st_mode)) {
log_error("%s exists but is not a directory!", parent);
return 1;
}
} else if (errno != ENOENT) {
log_error("ERROR: stat '%s': %m", parent);
return 1;
} else {
if (mkdir(parent, 0755) < 0) {
log_error("ERROR: mkdir '%s': %m", parent);
return 1;
}
}
if (path) {
*path = '/';
path++;
}
}
return 0;
}
static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir, bool resolvedeps, bool hashdst)
{
struct stat sb;
_cleanup_free_ char *fullsrcpath = NULL;
_cleanup_free_ char *fulldstpath = NULL;
_cleanup_free_ char *fulldstdir = NULL;
int ret;
bool src_islink = false;
bool src_isdir = false;
mode_t src_mode = 0;
bool dst_exists = true;
char *i = NULL;
const char *src, *dst;
if (sysrootdirlen) {
if (strncmp(orig_src, sysrootdir, sysrootdirlen) == 0) {
src = orig_src + sysrootdirlen;
fullsrcpath = strdup(orig_src);
} else {
src = orig_src;
_asprintf(&fullsrcpath, "%s%s", sysrootdir, src);
}
if (strncmp(orig_dst, sysrootdir, sysrootdirlen) == 0)
dst = orig_dst + sysrootdirlen;
else
dst = orig_dst;
} else {
src = orig_src;
fullsrcpath = strdup(src);
dst = orig_dst;
}
log_debug("dracut_install('%s', '%s', %d, %d, %d)", src, dst, isdir, resolvedeps, hashdst);
if (check_hashmap(items_failed, src)) {
log_debug("hash hit items_failed for '%s'", src);
return 1;
}
if (hashdst && check_hashmap(items, dst)) {
log_debug("hash hit items for '%s'", dst);
return 0;
}
if (lstat(fullsrcpath, &sb) < 0) {
if (!isdir) {
i = strdup(src);
hashmap_put(items_failed, i, i);
/* src does not exist */
return 1;
}
} else {
src_islink = S_ISLNK(sb.st_mode);
src_isdir = S_ISDIR(sb.st_mode);
src_mode = sb.st_mode;
}
_asprintf(&fulldstpath, "%s/%s", destrootdir, (dst[0] == '/' ? (dst + 1) : dst));
ret = stat(fulldstpath, &sb);
if (ret != 0) {
dst_exists = false;
if (errno != ENOENT) {
log_error("ERROR: stat '%s': %m", fulldstpath);
return 1;
}
}
if (ret == 0) {
if (resolvedeps && S_ISREG(sb.st_mode) && (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
log_debug("'%s' already exists, but checking for any deps", fulldstpath);
ret = resolve_deps(fullsrcpath + sysrootdirlen);
} else
log_debug("'%s' already exists", fulldstpath);
/* dst does already exist */
} else {
/* check destination directory */
fulldstdir = strndup(fulldstpath, dir_len(fulldstpath));
if (!fulldstdir) {
log_error("Out of memory!");
return 1;
}
ret = access(fulldstdir, F_OK);
if (ret < 0) {
_cleanup_free_ char *dname = NULL;
if (errno != ENOENT) {
log_error("ERROR: stat '%s': %m", fulldstdir);
return 1;
}
/* create destination directory */
log_debug("dest dir '%s' does not exist", fulldstdir);
dname = strndup(dst, dir_len(dst));
if (!dname)
return 1;
ret = dracut_install(dname, dname, true, false, true);
if (ret != 0) {
log_error("ERROR: failed to create directory '%s'", fulldstdir);
return 1;
}
}
if (src_isdir) {
if (dst_exists) {
if (S_ISDIR(sb.st_mode)) {
log_debug("dest dir '%s' already exists", fulldstpath);
return 0;
}
log_error("dest dir '%s' already exists but is not a directory", fulldstpath);
return 1;
}
log_info("mkdir '%s'", fulldstpath);
ret = dracut_mkdir(fulldstpath);
if (ret == 0) {
i = strdup(dst);
if (!i)
return -ENOMEM;
hashmap_put(items, i, i);
}
return ret;
}
/* ready to install src */
if (src_islink) {
_cleanup_free_ char *abspath = NULL;
abspath = get_real_file(src, false);
if (abspath == NULL)
return 1;
if (dracut_install(abspath, abspath, false, resolvedeps, hashdst)) {
log_debug("'%s' install error", abspath);
return 1;
}
if (faccessat(AT_FDCWD, abspath, F_OK, AT_SYMLINK_NOFOLLOW) != 0) {
log_debug("lstat '%s': %m", abspath);
return 1;
}
if (faccessat(AT_FDCWD, fulldstpath, F_OK, AT_SYMLINK_NOFOLLOW) != 0) {
_cleanup_free_ char *absdestpath = NULL;
_asprintf(&absdestpath, "%s/%s", destrootdir,
(abspath[0] == '/' ? (abspath + 1) : abspath) + sysrootdirlen);
ln_r(absdestpath, fulldstpath);
}
if (arg_hmac) {
/* copy .hmac files also */
hmac_install(src, dst, NULL);
}
return 0;
}
if (src_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
if (resolvedeps)
ret += resolve_deps(fullsrcpath + sysrootdirlen);
if (arg_hmac) {
/* copy .hmac files also */
hmac_install(src, dst, NULL);
}
}
log_debug("dracut_install ret = %d", ret);
if (arg_hostonly && !arg_module)
mark_hostonly(dst);
if (isdir) {
log_info("mkdir '%s'", fulldstpath);
ret += dracut_mkdir(fulldstpath);
} else {
log_info("cp '%s' '%s'", fullsrcpath, fulldstpath);
ret += cp(fullsrcpath, fulldstpath);
}
}
if (ret == 0) {
i = strdup(dst);
if (!i)
return -ENOMEM;
hashmap_put(items, i, i);
if (logfile_f)
dracut_log_cp(src);
}
log_debug("dracut_install ret = %d", ret);
return ret;
}
static void item_free(char *i)
{
assert(i);
free(i);
}
static void usage(int status)
{
/* */
printf("Usage: %s -D DESTROOTDIR [-r SYSROOTDIR] [OPTION]... -a SOURCE...\n"
"or: %s -D DESTROOTDIR [-r SYSROOTDIR] [OPTION]... SOURCE DEST\n"
"or: %s -D DESTROOTDIR [-r SYSROOTDIR] [OPTION]... -m KERNELMODULE [KERNELMODULE …]\n"
"\n"
"Install SOURCE (from rootfs or SYSROOTDIR) to DEST in DESTROOTDIR with all needed dependencies.\n"
"\n"
" KERNELMODULE can have the format:\n"
" <absolute path> with a leading /\n"
" =<kernel subdir>[/<kernel subdir>…] like '=drivers/hid'\n"
" <module name>\n"
"\n"
" -D --destrootdir Install all files to DESTROOTDIR as the root\n"
" -r --sysrootdir Install all files from SYSROOTDIR\n"
" -a --all Install all SOURCE arguments to DESTROOTDIR\n"
" -o --optional If SOURCE does not exist, do not fail\n"
" -d --dir SOURCE is a directory\n"
" -l --ldd Also install shebang executables and libraries\n"
" -L --logdir <DIR> Log files, which were installed from the host to <DIR>\n"
" -R --resolvelazy Only install shebang executables and libraries\n"
" for all SOURCE files\n"
" -H --hostonly Mark all SOURCE files as hostonly\n\n"
" -f --fips Also install all '.SOURCE.hmac' files\n"
"\n"
" --module,-m Install kernel modules, instead of files\n"
" --kerneldir Specify the kernel module directory\n"
" (default: /lib/modules/$(uname -r))\n"
" --firmwaredirs Specify the firmware directory search path with : separation\n"
" (default: $DRACUT_FIRMWARE_PATH, otherwise kernel-compatible\n"
" $(</sys/module/firmware_class/parameters/path),\n"
" /lib/firmware/updates/$(uname -r), /lib/firmware/updates\n"
" /lib/firmware/$(uname -r), /lib/firmware)\n"
" --silent Don't display error messages for kernel module install\n"
" --modalias Only generate module list from /sys/devices modalias list\n"
" -o --optional If kernel module does not exist, do not fail\n"
" -p --mod-filter-path Filter kernel modules by path regexp\n"
" -P --mod-filter-nopath Exclude kernel modules by path regexp\n"
" -s --mod-filter-symbol Filter kernel modules by symbol regexp\n"
" -S --mod-filter-nosymbol Exclude kernel modules by symbol regexp\n"
" -N --mod-filter-noname Exclude kernel modules by name regexp\n"
"\n"
" -v --verbose Show more output\n"
" --debug Show debug output\n"
" --version Show package version\n"
" -h --help Show this help\n"
"\n", program_invocation_short_name, program_invocation_short_name, program_invocation_short_name);
exit(status);
}
static int parse_argv(int argc, char *argv[])
{
int c;