--- trunk/grubby/grubby.c 2017/06/27 14:39:37 3021 +++ trunk/grubby/grubby.c 2020/07/07 11:09:37 3136 @@ -1712,7 +1712,20 @@ fprintf(out, "%sset default=\"${saved_entry}\"\n", indent); if (cfg->defaultImage >= FIRST_ENTRY_INDEX && cfg->cfi->setEnv) { char *title; - entry = findEntryByIndex(cfg, cfg->defaultImage); + int trueIndex, currentIndex; + + trueIndex = 0; + currentIndex = 0; + + while ((entry = findEntryByIndex(cfg, currentIndex))) { + if (!entry->skip) { + if (trueIndex == cfg->defaultImage) { + break; + } + trueIndex++; + } + currentIndex++; + } line = getLineByType(LT_MENUENTRY, entry->lines); if (!line) line = getLineByType(LT_TITLE, entry->lines); @@ -1779,6 +1792,7 @@ int needs = MAIN_DEFAULT; struct stat sb; int i; + int rc = 0; if (!strcmp(outName, "-")) { out = stdout; @@ -1893,16 +1907,42 @@ } if (tmpOutName) { - if (rename(tmpOutName, outName)) { - fprintf(stderr, - _("grubby: error moving %s to %s: %s\n"), - tmpOutName, outName, strerror(errno)); - unlink(outName); - return 1; + /* write userspace buffers */ + if (fflush(out)) + rc = 1; + + /* purge the write-back cache with fsync() */ + if (fsync(fileno(out))) + rc = 1; + + if (fclose(out)) + rc = 1; + + if (rc == 0 && rename(tmpOutName, outName)) { + unlink(tmpOutName); + rc = 1; + } + + /* fsync() the destination directory after rename */ + if (rc == 0) { + int dirfd; + + dirfd = open(dirname(strdupa(outName)), O_RDONLY); + if (dirfd < 0) + rc = 1; + else if (fsync(dirfd)) + rc = 1; + + if (dirfd >= 0) + close(dirfd); } + + if (rc == 1) + fprintf(stderr, + _("grubby: error flushing data: %m\n")); } - return 0; + return rc; } static int numEntries(struct grubConfig *cfg) @@ -2439,8 +2479,11 @@ index = 0; while ((entry = findEntryByIndex(cfg, index))) { if (suitableImage(entry, prefix, skipRemoved, flags)) { - int j; - for (j = 0; j < index; j++) { + int j, unmodifiedIndex; + + unmodifiedIndex = index; + + for (j = 0; j < unmodifiedIndex; j++) { entry2 = findEntryByIndex(cfg, j); if (entry2->skip) index--; @@ -2509,6 +2552,9 @@ struct singleEntry *bootEntry, *newDefault; int indexToVerify, firstKernelEntryIndex, currentLookupIndex; + /* initialize */ + currentLookupIndex = FIRST_ENTRY_INDEX; + /* handle the two cases where the user explictly picks the default * boot entry index as it would exist post-modification */ @@ -2519,8 +2565,7 @@ } /* Case 2: user picked an arbitrary index as the default boot entry */ - if (newDefaultBootEntryIndex >= FIRST_ENTRY_INDEX - && config->cfi->defaultIsIndex) { + if (newDefaultBootEntryIndex >= FIRST_ENTRY_INDEX) { indexToVerify = newDefaultBootEntryIndex; /* user chose to make latest boot entry the default */ @@ -2553,6 +2598,8 @@ /* check validity of existing default or first-entry-found selection */ if (defaultKernelPath) { + /* we must initialize this */ + firstKernelEntryIndex = 0; /* user requested first-entry-found */ if (!findEntryByPath(config, defaultKernelPath, prefix, &firstKernelEntryIndex)) { @@ -2572,8 +2619,30 @@ config->defaultImage++; } } else { - /* use pre-existing default entry */ - currentLookupIndex = config->defaultImage; + /* check to see if the default is stored in the environment */ + if (config->defaultImage < FIRST_ENTRY_INDEX) { + if (config->defaultImage == DEFAULT_SAVED || config->defaultImage == DEFAULT_SAVED_GRUB2) + { + if (config->cfi->defaultIsSaved) { + if (config->cfi->getEnv) { + char *defaultTitle = config->cfi->getEnv(config->cfi, "saved_entry"); + + if (defaultTitle) { + if (isnumber(defaultTitle)) { + currentLookupIndex = atoi(defaultTitle); + } else { + findEntryByTitle(config, defaultTitle, ¤tLookupIndex); + } + /* set the default Image to an actual index */ + config->defaultImage = currentLookupIndex; + } + } + } + } + } else { + /* use pre-existing default entry from the file*/ + currentLookupIndex = config->defaultImage; + } if (isAddingBootEntry && (newBootEntryIndex <= config->defaultImage)) { @@ -2644,7 +2713,7 @@ } } -void displayEntry(struct singleEntry *entry, const char *prefix, int index) +void displayEntry(struct grubConfig *config, struct singleEntry *entry, const char *prefix, int index) { struct singleLine *line; char *root = NULL; @@ -2740,7 +2809,14 @@ line = getLineByType(LT_TITLE, entry->lines); if (line) { - printf("title=%s\n", line->elements[1].item); + char *entryTitle; + /* if we can extractTitle, then it's a zipl config and + * if not then we go ahead with what's existed prior */ + entryTitle = extractTitle(config, line); + if (!entryTitle) { + entryTitle=line->elements[1].item; + } + printf("title=%s\n", entryTitle); } else { char *title; line = getLineByType(LT_MENUENTRY, entry->lines); @@ -3156,11 +3232,11 @@ printf("lba\n"); } - displayEntry(entry, prefix, i); + displayEntry(config, entry, prefix, i); i++; while ((entry = findEntryByPath(config, kernel, prefix, &i))) { - displayEntry(entry, prefix, i); + displayEntry(config, entry, prefix, i); i++; } @@ -3490,23 +3566,67 @@ line->numElements--; } -int argMatch(const char *one, const char *two) +static int argNameMatch(const char *one, const char *two) { char *first, *second; - char *chptr; + char *chptra, *chptrb; + int rc; first = strcpy(alloca(strlen(one) + 1), one); second = strcpy(alloca(strlen(two) + 1), two); - chptr = strchr(first, '='); - if (chptr) - *chptr = '\0'; + chptra = strchr(first, '='); + if (chptra) + *chptra = '\0'; + + chptrb = strchr(second, '='); + if (chptrb) + *chptrb = '\0'; + + rc = strcmp(first, second); + + if (chptra) + *chptra = '='; + if (chptrb) + *chptrb = '='; + + return rc; +} - chptr = strchr(second, '='); +static int argHasValue(const char *arg) +{ + char *chptr; + + chptr = strchr(arg, '='); if (chptr) - *chptr = '\0'; + return 1; + return 0; +} + +static int argValueMatch(const char *one, const char *two) +{ + char *first, *second; + char *chptra, *chptrb; + + first = strcpy(alloca(strlen(one) + 1), one); + second = strcpy(alloca(strlen(two) + 1), two); + + chptra = strchr(first, '='); + if (chptra) + chptra += 1; + + chptrb = strchr(second, '='); + if (chptrb) + chptrb += 1; - return strcmp(first, second); + if (!chptra && !chptrb) + return 0; + else if (!chptra) + return *chptrb - 0; + else if (!chptrb) + return 0 - *chptra; + else + return strcmp(chptra, chptrb); } int updateActualImage(struct grubConfig *cfg, const char *image, @@ -3650,7 +3770,7 @@ } if (usedElements[i]) continue; - if (!argMatch(line->elements[i].item, *arg)) { + if (!argNameMatch(line->elements[i].item, *arg)) { usedElements[i] = 1; break; } @@ -3709,9 +3829,12 @@ !strcmp(line->elements[i].item, "--")) /* reached the end of hyper args, stop here */ break; - if (!argMatch(line->elements[i].item, *arg)) { - removeElement(line, i); - break; + if (!argNameMatch(line->elements[i].item, *arg)) { + if (!argHasValue(*arg) || + !argValueMatch(line->elements[i].item, *arg)) { + removeElement(line, i); + break; + } } } /* handle removing LT_ROOT line too */