forked from NVIDIA/nvidia-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.c
More file actions
1786 lines (1337 loc) · 49.1 KB
/
backup.c
File metadata and controls
1786 lines (1337 loc) · 49.1 KB
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
/*
* nvidia-installer: A tool for installing NVIDIA software packages on
* Unix and Linux systems.
*
* Copyright (C) 2003 NVIDIA Corporation
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses>.
*
*
* backup.c - this source file contains functions used for backing up
* (and restoring) files that need to be moved out of the way during
* installation.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <ctype.h>
#include <stdlib.h>
#include "nvidia-installer.h"
#include "user-interface.h"
#include "backup.h"
#include "files.h"
#include "crc.h"
#include "misc.h"
#include "kernel.h"
#include "conflicting-kernel-modules.h"
#define BACKUP_DIRECTORY "/var/lib/nvidia"
#define BACKUP_LOG (BACKUP_DIRECTORY "/log")
#define BACKUP_MKDIR_LOG (BACKUP_DIRECTORY "/dirs")
/*
* Syntax for the backup log file:
*
* 1. The first line is the version string, assumed to be in the form:
* MAJOR.MINOR-PATCH
*
* 2. The second line is the driver description.
*
* XXX do we need anything to distinguish between official builds,
* nightlies, etc...?
*
* 3. The rest of the file is file entries; a file entry can be any one of:
*
* INSTALLED_FILE: <filename>
*
* INSTALLED_SYMLINK: <filename>
* <target>
*
* BACKED_UP_SYMLINK: <filename>
* <target>
* <permissions> <uid> <gid>
*
* BACKED_UP_FILE_NUM: <filename>
* <filesize> <permissions> <uid> <gid>
*
*/
#define BACKUP_LOG_PERMS (S_IRUSR|S_IWUSR)
#define BACKUP_DIRECTORY_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)
/*
* uninstall struct
*
*/
typedef struct {
int num;
char *filename;
char *target;
uint32 crc;
mode_t mode;
uid_t uid;
gid_t gid;
int ok;
} BackupLogEntry;
typedef struct {
char *version;
char *description;
BackupLogEntry *e;
int n;
} BackupInfo;
static BackupInfo *read_backup_log_file(Options *op);
static void free_backup_info(BackupInfo *b);
static int check_backup_log_entries(Options *op, BackupInfo *b);
static int do_uninstall(Options *op, const char *version,
const int skip_depmod);
static int sanity_check_backup_log_entries(Options *op, BackupInfo *b);
static char *create_backwards_compatible_version_string(const char *str);
static int reverse_strlen_compare(const void *a, const void *b);
/*
* init_backup() - initialize the backup engine; this consists of
* creating a new backup directory, and writing to the log file that
* we're about to install a new driver version.
*/
int init_backup(Options *op, Package *p)
{
mode_t orig_mode;
char *version;
FILE *log;
/* remove the directory, if it already exists */
if (directory_exists(BACKUP_DIRECTORY)) {
if (!remove_directory(op, BACKUP_DIRECTORY)) {
return FALSE;
}
}
/* create the backup directory, with perms only for owner */
if (!mkdir_recursive(op, BACKUP_DIRECTORY, BACKUP_DIRECTORY_PERMS, FALSE)) {
return FALSE;
}
/*
* fopen below creates the file with mode 0666 & ~umask.
* In order to ensure the result of that calculation is BACKUP_LOG_PERMS,
* we temporarily set umask to ~BACKUP_LOG_PERMS to leave just the bits
* we want.
*
* This assumes that BACKUP_LOG does not already exist (if it does, the
* file permissions will not be modified.) This is assured by the
* directory removal and re-creation code above.
*/
orig_mode = umask(~BACKUP_LOG_PERMS);
/* create the log file */
log = fopen(BACKUP_LOG, "a");
umask(orig_mode);
if (!log) {
ui_error(op, "Unable to create backup log file '%s' (%s).",
BACKUP_LOG, strerror(errno));
return FALSE;
}
/* write the version and description */
version = create_backwards_compatible_version_string(p->version);
fprintf(log, "%s\n", version);
fprintf(log, "%s\n", p->description);
nvfree(version);
/* close the log file */
if (fclose(log) != 0) {
ui_error(op, "Error while closing backup log file '%s' (%s).",
BACKUP_LOG, strerror(errno));
return FALSE;
}
return TRUE;
} /* init_backup() */
/*
* do_backup() - backup the specified file. If it is a regular file,
* just move it into the backup directory, and add an entry to the log
* file.
*/
int do_backup(Options *op, const char *filename)
{
int len, ret, ret_val;
struct stat stat_buf;
char *tmp = NULL;
FILE *log;
uint32 crc;
static int backup_file_number = BACKED_UP_FILE_NUM;
ret_val = FALSE;
log = fopen(BACKUP_LOG, "a");
if (!log) {
ui_error(op, "Unable to open backup log file '%s' (%s).",
BACKUP_LOG, strerror(errno));
return FALSE;
}
if (lstat(filename, &stat_buf) == -1) {
switch (errno) {
case ENOENT:
ret_val = TRUE;
break;
default:
ui_error(op, "Unable to determine properties for file '%s' (%s).",
filename, strerror(errno));
}
goto done;
}
if (S_ISREG(stat_buf.st_mode)) {
crc = compute_crc(op, filename);
len = strlen(BACKUP_DIRECTORY) + 64;
tmp = nvalloc(len + 1);
snprintf(tmp, len, "%s/%d", BACKUP_DIRECTORY, backup_file_number);
if (!nvrename(op, filename, tmp)) {
ui_error(op, "Unable to backup file '%s'.", filename);
goto done;
}
fprintf(log, "%d: %s\n", backup_file_number, filename);
/* write the filesize, permissions, uid, gid */
fprintf(log, "%u %04o %d %d\n", crc, stat_buf.st_mode,
stat_buf.st_uid, stat_buf.st_gid);
backup_file_number++;
} else if (S_ISLNK(stat_buf.st_mode)) {
tmp = get_symlink_target(op, filename);
ret = unlink(filename);
if (ret == -1) {
ui_error(op, "Unable to remove symbolic link '%s' (%s).",
filename, strerror(errno));
goto done;
}
fprintf(log, "%d: %s\n", BACKED_UP_SYMLINK, filename);
fprintf(log, "%s\n", tmp);
fprintf(log, "%04o %d %d\n", stat_buf.st_mode,
stat_buf.st_uid, stat_buf.st_gid);
} else if (S_ISDIR(stat_buf.st_mode)) {
/* XXX IMPLEMENT ME: recursive moving of a directory */
ui_error(op, "Unable to backup directory '%s'.", filename);
goto done;
} else {
ui_error(op, "Unable to backup file '%s' (don't know how to deal with "
"file type).", filename);
goto done;
}
ret_val = TRUE;
done:
nvfree(tmp);
/* close the log file */
if (fclose(log) != 0) {
ui_error(op, "Error while closing backup log file '%s' (%s).",
BACKUP_LOG, strerror(errno));
ret_val = FALSE;
}
return ret_val;
} /* do_backup() */
int log_install_file(Options *op, const char *filename)
{
FILE *log;
uint32 crc;
/* open the log file */
log = fopen(BACKUP_LOG, "a");
if (!log) {
ui_error(op, "Unable to open backup log file '%s' (%s).",
BACKUP_LOG, strerror(errno));
return FALSE;
}
fprintf(log, "%d: %s\n", INSTALLED_FILE, filename);
crc = compute_crc(op, filename);
fprintf(log, "%u\n", crc);
/* close the log file */
if (fclose(log) != 0) {
ui_error(op, "Error while closing backup log file '%s' (%s).",
BACKUP_LOG, strerror(errno));
return FALSE;
}
return TRUE;
} /* log_install_file() */
int log_create_symlink(Options *op, const char *filename, const char *target)
{
FILE *log;
/* open the log file */
log = fopen(BACKUP_LOG, "a");
if (!log) {
ui_error(op, "Unable to open backup log file '%s' (%s).",
BACKUP_LOG, strerror(errno));
return FALSE;
}
fprintf(log, "%d: %s\n", INSTALLED_SYMLINK, filename);
fprintf(log, "%s\n", target);
/* close the log file */
if (fclose(log) != 0) {
ui_error(op, "Error while closing backup log file '%s' (%s).",
BACKUP_LOG, strerror(errno));
return FALSE;
}
return TRUE;
} /* log_create_symlink() */
static int parse_first_line(const char *buf, int *num, char **filename)
{
char *c, *local_buf;
if (!buf || !num || !filename) return FALSE;
local_buf = nvstrdup(buf);
c = local_buf;
while ((*c != '\0') && (*c != ':')) {
if (!isdigit(*c)) return FALSE;
c++;
}
if (*c == '\0') return FALSE;
*c = '\0';
*num = strtol(local_buf, NULL, 10);
c++;
while (isspace(*c)) c++;
*filename = nvstrdup(c);
free(local_buf);
return TRUE;
}
static int parse_mode_uid_gid(const char *buf, mode_t *mode,
uid_t *uid, gid_t *gid)
{
char *c, *local_buf, *str;
if (!buf || !mode || !uid || !gid) return FALSE;
local_buf = nvstrdup(buf);
c = str = local_buf;
while ((*c != '\0') && (isdigit(*c))) c++;
if (*c == '\0') return FALSE;
*c = '\0';
*mode = strtol(str, NULL, 8);
str = ++c;
while ((*c != '\0') && (isdigit(*c))) c++;
if (*c == '\0') return FALSE;
*c = '\0';
*uid = strtol(str, NULL, 10);
str = ++c;
while ((*c != '\0') && (isdigit(*c))) c++;
*c = '\0';
*gid = strtol(str, NULL, 10);
free(local_buf);
return TRUE;
}
static int parse_crc_mode_uid_gid(const char *buf, uint32 *crc, mode_t *mode,
uid_t *uid, gid_t *gid)
{
char *c, *local_buf, *str;
if (!buf || !crc || !mode || !uid || !gid) return FALSE;
local_buf = nvstrdup(buf);
c = str = local_buf;
while ((*c != '\0') && (isdigit(*c))) c++;
if (*c == '\0') return FALSE;
*c = '\0';
*crc = strtoul(str, NULL, 10);
str = ++c;
while ((*c != '\0') && (isdigit(*c))) c++;
if (*c == '\0') return FALSE;
*c = '\0';
*mode = strtol(str, NULL, 8);
str = ++c;
while ((*c != '\0') && (isdigit(*c))) c++;
if (*c == '\0') return FALSE;
*c = '\0';
*uid = strtol(str, NULL, 10);
str = ++c;
while ((*c != '\0') && (isdigit(*c))) c++;
*c = '\0';
*gid = strtol(str, NULL, 10);
free(local_buf);
return TRUE;
}
static int parse_crc(const char *buf, uint32 *crc)
{
char *c, *local_buf, *str;
if (!buf || !crc) return FALSE;
local_buf = nvstrdup(buf);
c = str = local_buf;
while ((*c != '\0') && (isdigit(*c))) c++;
*c = '\0';
*crc = strtoul(str, NULL, 10);
free(local_buf);
return TRUE;
} /* parse_crc() */
/*
* Syntax for the mkdir log file:
*
* Each line in the file contains the name of a directory that was created
* during driver installation.
*
* XXX pathnames containing '\n' will break both this file, and the regular
* backup log file.
*/
/*
* log_mkdir() - takes a newline-delimited list of directories and appends
* them to the log of directories created during installation.
*/
int log_mkdir(Options *op, const char *dirs)
{
FILE *log;
/*
* create the backup directory if it doesn't exist; BACKUP_MKDIR_LOG
* is within BACKUP_DIRECTORY, so the below fopen(3) call depends on
* the existence of BACKUP_DIRECTORY
*/
if (!directory_exists(BACKUP_DIRECTORY) &&
!mkdir_recursive(op, BACKUP_DIRECTORY, BACKUP_DIRECTORY_PERMS, FALSE)) {
return FALSE;
}
/* open the log file */
log = fopen(BACKUP_MKDIR_LOG, "a");
if (!log) {
ui_error(op, "Unable to open mkdir log file '%s' (%s).",
BACKUP_MKDIR_LOG, strerror(errno));
return FALSE;
}
fprintf(log, "%s", dirs);
/* close the log file */
if (fclose(log) != 0) {
ui_error(op, "Error while closing mkdir log file '%s' (%s).",
BACKUP_MKDIR_LOG, strerror(errno));
return FALSE;
}
return TRUE;
}
/*
* reverse_strlen_compare() - Compare two strings by length, for sorting
* in order of decreasing length.
*/
static int reverse_strlen_compare(const void *a, const void *b)
{
return strlen(*(char * const *)b) - strlen(*(char * const *)a);
}
/*
* rmdir_recursive() - Use BACKUP_MKDIR_LOG to find directories that were
* created by a previous nvidia-installer, and delete any such directories.
* Returns TRUE if the log is found and all directories are successfully
* deleted; returns FALSE if any directories failed to be deleted or the
* log isn't found. The log enries are processed in reverse order, so that
* child directories get properly deleted before their parents.
*/
static int rmdir_recursive(Options *op)
{
FILE *log;
char **dirs;
int eof = FALSE, ret = TRUE, lines, i;
/* open the log file */
log = fopen(BACKUP_MKDIR_LOG, "r");
if (!log) {
/* Fail silently: most likely, the current driver was simply installed
* with an nvidia-installer that didn't log created directories. */
return FALSE;
}
/* Count the number of lines */
for (lines = 0; !eof; lines++) {
char *dir;
dir = fget_next_line(log, &eof);
nvfree(dir);
}
rewind(log);
dirs = nvalloc(lines * sizeof(char*));
for (i = 0; i < lines; i++) {
dirs[i] = fget_next_line(log, &eof);
}
qsort(dirs, lines, sizeof(char*), reverse_strlen_compare);
for (i = 0; i < lines; i++) {
if (dirs[i]) {
/* Ignore empty lines and the backup directory itself, since it is
* never empty as long as the dirs file is still around. */
if (strlen(dirs[i]) && strcmp(dirs[i], BACKUP_DIRECTORY) != 0) {
if (rmdir(dirs[i]) != 0) {
ui_log(op, "Failed to delete the directory '%s' (%s).",
dirs[i], strerror(errno));
ret = FALSE;
}
}
}
nvfree(dirs[i]);
}
nvfree(dirs);
if (!ret) {
ui_warn(op, "Failed to delete some directories. See %s for details.",
op->log_file_name);
}
/* close the log file */
if (fclose(log) != 0) {
ui_error(op, "Error while closing mkdir log file '%s' (%s).",
BACKUP_MKDIR_LOG, strerror(errno));
return FALSE;
}
return ret;
}
/*
* do_uninstall() - this function uninstalls a previously installed
* driver, by parsing the BACKUP_LOG file.
*/
static int do_uninstall(Options *op, const char *version,
const int skip_depmod)
{
BackupLogEntry *e;
BackupInfo *b;
int i, len, ok;
char *tmpstr;
float percent;
int removal_failed = FALSE, restore_failed = FALSE;
static const char existing_installation_is_borked[] =
"Your driver installation has been "
"altered since it was initially installed; this may happen, "
"for example, if you have since installed the NVIDIA driver through "
"a mechanism other than nvidia-installer (such as your "
"distribution's native package management system). "
"nvidia-installer will attempt to uninstall as best it can.";
/* do we even have a backup directory? */
if (access(BACKUP_DIRECTORY, F_OK) == -1) {
ui_message(op, "No driver backed up.");
return FALSE;
}
if ((b = read_backup_log_file(op)) == NULL) return FALSE;
ok = check_backup_log_entries(op, b);
if (!ok) {
if (op->logging) {
ui_warn(op, "%s Please see the file '%s' for details.",
existing_installation_is_borked, op->log_file_name);
} else {
ui_warn(op, "%s", existing_installation_is_borked);
}
}
tmpstr = nvstrcat("Uninstalling ", b->description, " (",
b->version, "):", NULL);
run_distro_hook(op, "pre-uninstall");
ui_status_begin(op, tmpstr, "Uninstalling");
free(tmpstr);
/* Remove any installed DKMS modules */
if (dkms_module_installed(op, version, NULL)) {
ui_log(op, "DKMS module detected; removing...");
if (!dkms_remove_module(op, version)) {
ui_warn(op, "Failed to remove installed DKMS module!");
}
}
/*
* given the list of Backup logfile entries, perform the necessary
* operations:
*
* Step 1: remove everything that was previously installed
*
* Step 2: restore everything that was previously backed up
*/
for (i = 0; i < b->n; i++) {
percent = (float) i / (float) (b->n * 2);
e = &b->e[i];
if (!e->ok) continue;
switch (e->num) {
/*
* This is a file that was installed -- now delete it.
*/
case INSTALLED_FILE:
if (unlink(e->filename) == -1) {
ui_log(op, "Unable to remove installed file '%s' (%s).",
e->filename, strerror(errno));
removal_failed = TRUE;
}
ui_status_update(op, percent, "%s", e->filename);
break;
case INSTALLED_SYMLINK:
if (unlink(e->filename) == -1) {
ui_log(op, "Unable to remove installed symlink '%s' (%s).",
e->filename, strerror(errno));
removal_failed = TRUE;
}
ui_status_update(op, percent, "%s", e->filename);
break;
}
}
for (i = 0; i < b->n; i++) {
percent = (float) (i + b->n) / (float) (b->n * 2);
e = &b->e[i];
if (!e->ok) continue;
switch (e->num) {
case INSTALLED_FILE:
case INSTALLED_SYMLINK:
/* nothing to do */
break;
case BACKED_UP_SYMLINK:
if (symlink(e->target, e->filename) == -1) {
/*
* XXX only print this warning if
* check_backup_log_entries() didn't see any problems.
*/
if (ok) {
restore_failed = TRUE;
}
ui_log(op, "Unable to restore symbolic link "
"%s -> %s (%s).", e->filename, e->target,
strerror(errno));
} else {
/* XXX do we need to chmod the symlink? */
if (lchown(e->filename, e->uid, e->gid)) {
ui_log(op, "Unable to restore owner (%d) and group "
"(%d) for symbolic link '%s' (%s).",
e->uid, e->gid, e->filename, strerror(errno));
restore_failed = TRUE;
}
}
ui_status_update(op, percent, "%s", e->filename);
break;
default:
len = strlen(BACKUP_DIRECTORY) + 64;
tmpstr = nvalloc(len + 1);
snprintf(tmpstr, len, "%s/%d", BACKUP_DIRECTORY, e->num);
if (!nvrename(op, tmpstr, e->filename)) {
ui_log(op, "Unable to restore file '%s'.", e->filename);
restore_failed = TRUE;
} else {
if (chown(e->filename, e->uid, e->gid)) {
ui_log(op, "Unable to restore owner (%d) and group "
"(%d) for file '%s' (%s).",
e->uid, e->gid, e->filename, strerror(errno));
restore_failed = TRUE;
} else {
if (chmod(e->filename, e->mode) == -1) {
ui_log(op, "Unable to restore permissions %04o for "
"file '%s'.", e->mode, e->filename);
restore_failed = TRUE;
}
}
}
ui_status_update(op, percent, "%s", e->filename);
free(tmpstr);
break;
}
}
if (removal_failed) {
ui_warn(op, "Failed to remove some installed files/symlinks. See %s "
"for details", op->log_file_name);
}
if (restore_failed) {
ui_warn(op, "Failed to restore some backed up files/symlinks, and/or "
"their attributes. See %s for details", op->log_file_name);
}
if (!rmdir_recursive(op)) {
ui_log(op, "Unable to delete directories created by previous "
"installation.");
}
ui_status_end(op, "done.");
/* remove the backup directory */
if (!remove_directory(op, BACKUP_DIRECTORY)) {
/* XXX what to do if this fails?... nothing */
}
if (!op->skip_module_unload) {
/*
* attempt to unload the kernel module(s), but don't abort if this
* fails: the kernel may not have been configured with support for
* module unloading or the user might have unloaded it themselves or the
* module might not have existed at all.
*/
for (i = 0; i < num_conflicting_kernel_modules; i++) {
rmmod_kernel_module(op, conflicting_kernel_modules[i]);
}
}
if (op->uninstall) {
/* Update modules.dep and the ldconfig(8) cache to remove entries for
* any DSOs and kernel modules that we just uninstalled. */
int status = 0;
ui_log(op, "Running %sldconfig:", op->skip_depmod ? "" : "depmod and ");
if (!op->skip_depmod) {
status |= run_command(op, NULL, FALSE, NULL, FALSE,
op->utils[DEPMOD], " -a ", op->kernel_name, NULL);
}
status |= run_command(op, NULL, FALSE, NULL, FALSE,
op->utils[LDCONFIG], NULL);
if (status == 0) {
ui_log(op, "done.");
} else {
ui_log(op, "error!");
ui_warn(op, "An error occurred while running depmod or ldconfig "
"after uninstallation: your system may have stale state "
"involving recently uninstalled files.");
}
/*
* In case systemd units were just uninstalled, run `systemctl
* daemon-reload` to reflect the change.
*/
if (op->utils[SYSTEMCTL]) {
char *cmd = nvstrcat(op->utils[SYSTEMCTL], " daemon-reload", NULL);
ui_log(op, "Running `%s`:", cmd);
status = run_command(op, NULL, FALSE, NULL, FALSE, cmd, NULL);
nvfree(cmd);
if (status == 0) {
ui_log(op, "done.");
} else {
ui_log(op, "error!");
ui_warn(op, "An error occurred while reloading the systemd "
"daemon configuration.");
}
}
}
run_distro_hook(op, "post-uninstall");
free_backup_info(b);
return TRUE;
} /* do_uninstall() */
/*
* read_backup_log_file() -
*/
static BackupInfo *read_backup_log_file(Options *op)
{
struct stat stat_buf;
char *buf, *c, *line, *filename;
int fd, num, length, line_num = 0;
float percent;
BackupLogEntry *e;
BackupInfo *b = NULL;
/* check the permissions of the backup directory */
if (stat(BACKUP_DIRECTORY, &stat_buf) == -1) {
ui_error(op, "Unable to get properties of %s (%s).",
BACKUP_DIRECTORY, strerror(errno));
return NULL;
}
if ((stat_buf.st_mode & PERM_MASK) != BACKUP_DIRECTORY_PERMS) {
ui_error(op, "The directory permissions of %s have been changed since"
"the directory was created!", BACKUP_DIRECTORY);
return NULL;
}
if ((fd = open(BACKUP_LOG, O_RDONLY)) == -1) {
ui_error(op, "Failure opening %s (%s).", BACKUP_LOG, strerror(errno));
return NULL;
}
if (fstat(fd, &stat_buf) == -1) {
ui_error(op, "Failure getting file properties for %s (%s).",
BACKUP_LOG, strerror(errno));
goto pre_map_fail;
}
if ((stat_buf.st_mode & PERM_MASK) != BACKUP_LOG_PERMS) {
ui_error(op, "The file permissions of %s have been changed since "
"the file was written!", BACKUP_LOG);
goto pre_map_fail;
}
/* map the file */
length = stat_buf.st_size;
buf = mmap(0, length, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
if (!buf) {
ui_error(op, "Unable to mmap file '%s' (%s).", BACKUP_LOG,
strerror(errno));
return NULL;
}
ui_status_begin(op, "Parsing log file:", "Parsing");
b = nvalloc(sizeof(BackupInfo));
b->version = get_next_line(buf, &c, buf, length);
if (!b->version || !c) goto parse_error;
percent = (float) (c - buf) / (float) stat_buf.st_size;
ui_status_update(op, percent, NULL);
b->description = get_next_line(c, &c, buf, length);
if (!b->description || !c) goto parse_error;
b->n = 0;
b->e = NULL;
line_num = 3;
while (1) {
percent = (float) (c - buf) / (float) stat_buf.st_size;
ui_status_update(op, percent, NULL);
/* read and parse the next line */
line = get_next_line(c, &c, buf, length);
if (!line) break;
if (!parse_first_line(line, &num, &filename)) goto parse_error;
line_num++;
free(line);
/* grow the BackupLogEntry array */
b->n++;
b->e = (BackupLogEntry *)
nvrealloc(b->e, sizeof(BackupLogEntry) * b->n);
memset(&b->e[b->n - 1], 0, sizeof(BackupLogEntry));
e = &b->e[b->n - 1];
e->num = num;
e->filename = filename;
e->ok = TRUE;
switch(e->num) {
case INSTALLED_FILE:
line = get_next_line(c, &c, buf, length);
if (line == NULL) goto parse_error;
line_num++;