36 |
#include <signal.h> |
#include <signal.h> |
37 |
#include <blkid/blkid.h> |
#include <blkid/blkid.h> |
38 |
|
|
39 |
|
#include "log.h" |
40 |
|
|
41 |
#ifndef DEBUG |
#ifndef DEBUG |
42 |
#define DEBUG 0 |
#define DEBUG 0 |
43 |
#endif |
#endif |
60 |
|
|
61 |
int isEfi = 0; |
int isEfi = 0; |
62 |
|
|
63 |
|
char *saved_command_line = NULL; |
64 |
|
|
65 |
/* comments get lumped in with indention */ |
/* comments get lumped in with indention */ |
66 |
struct lineElement { |
struct lineElement { |
67 |
char * item; |
char * item; |
230 |
}; |
}; |
231 |
static int i = -1; |
static int i = -1; |
232 |
static const char *grub_cfg = "/boot/grub/grub.cfg"; |
static const char *grub_cfg = "/boot/grub/grub.cfg"; |
233 |
|
int rc = -1; |
234 |
|
|
235 |
if (i == -1) { |
if (i == -1) { |
236 |
for (i = 0; configFiles[i] != NULL; i++) { |
for (i = 0; configFiles[i] != NULL; i++) { |
237 |
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
238 |
if (!access(configFiles[i], R_OK)) { |
if ((rc = access(configFiles[i], R_OK))) { |
239 |
|
if (errno == EACCES) { |
240 |
|
printf("Unable to access bootloader configuration file " |
241 |
|
"\"%s\": %m\n", configFiles[i]); |
242 |
|
exit(1); |
243 |
|
} |
244 |
|
continue; |
245 |
|
} else { |
246 |
dbgPrintf("found\n"); |
dbgPrintf("found\n"); |
247 |
return configFiles[i]; |
return configFiles[i]; |
248 |
} |
} |
966 |
int len; |
int len; |
967 |
char * buf; |
char * buf; |
968 |
|
|
969 |
if (!strcmp(inName, "-")) { |
if (inName == NULL) { |
970 |
|
printf("Could not find bootloader configuration\n"); |
971 |
|
exit(1); |
972 |
|
} else if (!strcmp(inName, "-")) { |
973 |
in = 0; |
in = 0; |
974 |
} else { |
} else { |
975 |
if ((in = open(inName, O_RDONLY)) < 0) { |
if ((in = open(inName, O_RDONLY)) < 0) { |
1549 |
return NULL; |
return NULL; |
1550 |
} |
} |
1551 |
|
|
1552 |
void printEntry(struct singleEntry * entry) { |
void printEntry(struct singleEntry * entry, FILE *f) { |
1553 |
int i; |
int i; |
1554 |
struct singleLine * line; |
struct singleLine * line; |
1555 |
|
|
1556 |
for (line = entry->lines; line; line = line->next) { |
for (line = entry->lines; line; line = line->next) { |
1557 |
fprintf(stderr, "DBG: %s", line->indent); |
log_message(f, "DBG: %s", line->indent); |
1558 |
for (i = 0; i < line->numElements; i++) { |
for (i = 0; i < line->numElements; i++) { |
1559 |
/* Need to handle this, because we strip the quotes from |
/* Need to handle this, because we strip the quotes from |
1560 |
* menuentry when read it. */ |
* menuentry when read it. */ |
1561 |
if (line->type == LT_MENUENTRY && i == 1) { |
if (line->type == LT_MENUENTRY && i == 1) { |
1562 |
if(!isquote(*line->elements[i].item)) |
if(!isquote(*line->elements[i].item)) |
1563 |
fprintf(stderr, "\'%s\'", line->elements[i].item); |
log_message(f, "\'%s\'", line->elements[i].item); |
1564 |
else |
else |
1565 |
fprintf(stderr, "%s", line->elements[i].item); |
log_message(f, "%s", line->elements[i].item); |
1566 |
fprintf(stderr, "%s", line->elements[i].indent); |
log_message(f, "%s", line->elements[i].indent); |
1567 |
|
|
1568 |
continue; |
continue; |
1569 |
} |
} |
1570 |
|
|
1571 |
fprintf(stderr, "%s%s", |
log_message(f, "%s%s", |
1572 |
line->elements[i].item, line->elements[i].indent); |
line->elements[i].item, line->elements[i].indent); |
1573 |
} |
} |
1574 |
fprintf(stderr, "\n"); |
log_message(f, "\n"); |
1575 |
} |
} |
1576 |
} |
} |
1577 |
|
|
1578 |
void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...) |
void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...) |
1579 |
{ |
{ |
1580 |
va_list argp; |
static int once; |
1581 |
|
va_list argp, argq; |
1582 |
|
|
1583 |
if (!debug) |
va_start(argp, fmt); |
1584 |
|
|
1585 |
|
va_copy(argq, argp); |
1586 |
|
if (!once) { |
1587 |
|
log_time(NULL); |
1588 |
|
log_message(NULL, "command line: %s\n", saved_command_line); |
1589 |
|
} |
1590 |
|
log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed"); |
1591 |
|
log_vmessage(NULL, fmt, argq); |
1592 |
|
|
1593 |
|
printEntry(entry, NULL); |
1594 |
|
va_end(argq); |
1595 |
|
|
1596 |
|
if (!debug) { |
1597 |
|
once = 1; |
1598 |
|
va_end(argp); |
1599 |
return; |
return; |
1600 |
|
} |
1601 |
|
|
1602 |
va_start(argp, fmt); |
if (okay) { |
1603 |
|
va_end(argp); |
1604 |
|
return; |
1605 |
|
} |
1606 |
|
|
1607 |
|
if (!once) |
1608 |
|
log_message(stderr, "DBG: command line: %s\n", saved_command_line); |
1609 |
|
once = 1; |
1610 |
fprintf(stderr, "DBG: Image entry failed: "); |
fprintf(stderr, "DBG: Image entry failed: "); |
1611 |
vfprintf(stderr, fmt, argp); |
vfprintf(stderr, fmt, argp); |
1612 |
printEntry(entry); |
printEntry(entry, stderr); |
1613 |
va_end(argp); |
va_end(argp); |
1614 |
} |
} |
1615 |
|
|
1636 |
char * rootdev; |
char * rootdev; |
1637 |
|
|
1638 |
if (skipRemoved && entry->skip) { |
if (skipRemoved && entry->skip) { |
1639 |
notSuitablePrintf(entry, "marked to skip\n"); |
notSuitablePrintf(entry, 0, "marked to skip\n"); |
1640 |
return 0; |
return 0; |
1641 |
} |
} |
1642 |
|
|
1643 |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
1644 |
if (!line) { |
if (!line) { |
1645 |
notSuitablePrintf(entry, "no line found\n"); |
notSuitablePrintf(entry, 0, "no line found\n"); |
1646 |
return 0; |
return 0; |
1647 |
} |
} |
1648 |
if (line->numElements < 2) { |
if (line->numElements < 2) { |
1649 |
notSuitablePrintf(entry, "line has only %d elements\n", |
notSuitablePrintf(entry, 0, "line has only %d elements\n", |
1650 |
line->numElements); |
line->numElements); |
1651 |
return 0; |
return 0; |
1652 |
} |
} |
1653 |
|
|
1654 |
if (flags & GRUBBY_BADIMAGE_OKAY) return 1; |
if (flags & GRUBBY_BADIMAGE_OKAY) { |
1655 |
|
notSuitablePrintf(entry, 1, "\n"); |
1656 |
|
return 1; |
1657 |
|
} |
1658 |
|
|
1659 |
fullName = alloca(strlen(bootPrefix) + |
fullName = alloca(strlen(bootPrefix) + |
1660 |
strlen(line->elements[1].item) + 1); |
strlen(line->elements[1].item) + 1); |
1665 |
sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/", |
sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/", |
1666 |
line->elements[1].item + rootspec_offset); |
line->elements[1].item + rootspec_offset); |
1667 |
if (access(fullName, R_OK)) { |
if (access(fullName, R_OK)) { |
1668 |
notSuitablePrintf(entry, "access to %s failed\n", fullName); |
notSuitablePrintf(entry, 0, "access to %s failed\n", fullName); |
1669 |
return 0; |
return 0; |
1670 |
} |
} |
1671 |
for (i = 2; i < line->numElements; i++) |
for (i = 2; i < line->numElements; i++) |
1686 |
|
|
1687 |
/* failed to find one */ |
/* failed to find one */ |
1688 |
if (!line) { |
if (!line) { |
1689 |
notSuitablePrintf(entry, "no line found\n"); |
notSuitablePrintf(entry, 0, "no line found\n"); |
1690 |
return 0; |
return 0; |
1691 |
} |
} |
1692 |
|
|
1695 |
if (i < line->numElements) |
if (i < line->numElements) |
1696 |
dev = line->elements[i].item + 5; |
dev = line->elements[i].item + 5; |
1697 |
else { |
else { |
1698 |
notSuitablePrintf(entry, "no root= entry found\n"); |
notSuitablePrintf(entry, 0, "no root= entry found\n"); |
1699 |
/* it failed too... can't find root= */ |
/* it failed too... can't find root= */ |
1700 |
return 0; |
return 0; |
1701 |
} |
} |
1704 |
|
|
1705 |
dev = getpathbyspec(dev); |
dev = getpathbyspec(dev); |
1706 |
if (!getpathbyspec(dev)) { |
if (!getpathbyspec(dev)) { |
1707 |
notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev); |
notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev); |
1708 |
return 0; |
return 0; |
1709 |
} else |
} else |
1710 |
dev = getpathbyspec(dev); |
dev = getpathbyspec(dev); |
1711 |
|
|
1712 |
rootdev = findDiskForRoot(); |
rootdev = findDiskForRoot(); |
1713 |
if (!rootdev) { |
if (!rootdev) { |
1714 |
notSuitablePrintf(entry, "can't find root device\n"); |
notSuitablePrintf(entry, 0, "can't find root device\n"); |
1715 |
return 0; |
return 0; |
1716 |
} |
} |
1717 |
|
|
1718 |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
1719 |
notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n", |
notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n", |
1720 |
getuuidbydev(rootdev), getuuidbydev(dev)); |
getuuidbydev(rootdev), getuuidbydev(dev)); |
1721 |
free(rootdev); |
free(rootdev); |
1722 |
return 0; |
return 0; |
1723 |
} |
} |
1724 |
|
|
1725 |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
1726 |
notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n", |
notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n", |
1727 |
getuuidbydev(rootdev), getuuidbydev(dev)); |
getuuidbydev(rootdev), getuuidbydev(dev)); |
1728 |
free(rootdev); |
free(rootdev); |
1729 |
return 0; |
return 0; |
1730 |
} |
} |
1731 |
|
|
1732 |
free(rootdev); |
free(rootdev); |
1733 |
|
notSuitablePrintf(entry, 1, "\n"); |
1734 |
|
|
1735 |
return 1; |
return 1; |
1736 |
} |
} |
3942 |
|
|
3943 |
signal(SIGSEGV, traceback); |
signal(SIGSEGV, traceback); |
3944 |
|
|
3945 |
|
int i = 0; |
3946 |
|
for (int j = 1; j < argc; j++) |
3947 |
|
i += strlen(argv[j]) + 1; |
3948 |
|
saved_command_line = malloc(i); |
3949 |
|
if (!saved_command_line) { |
3950 |
|
fprintf(stderr, "grubby: %m\n"); |
3951 |
|
exit(1); |
3952 |
|
} |
3953 |
|
saved_command_line[0] = '\0'; |
3954 |
|
for (int j = 1; j < argc; j++) { |
3955 |
|
strcat(saved_command_line, argv[j]); |
3956 |
|
strncat(saved_command_line, j == argc -1 ? "" : " ", 1); |
3957 |
|
} |
3958 |
|
|
3959 |
optCon = poptGetContext("grubby", argc, argv, options, 0); |
optCon = poptGetContext("grubby", argc, argv, options, 0); |
3960 |
poptReadDefaultConfig(optCon, 1); |
poptReadDefaultConfig(optCon, 1); |
3961 |
|
|
4199 |
return 0; |
return 0; |
4200 |
} |
} |
4201 |
|
|
4202 |
|
if (grubConfig == NULL) { |
4203 |
|
printf("Could not find bootloader configuration file.\n"); |
4204 |
|
exit(1); |
4205 |
|
} |
4206 |
|
|
4207 |
config = readConfig(grubConfig, cfi); |
config = readConfig(grubConfig, cfi); |
4208 |
if (!config) return 1; |
if (!config) return 1; |
4209 |
|
|