Magellan Linux

Diff of /tags/grubby-8_32/grubby.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2056 by niro, Wed Feb 20 14:04:28 2013 UTC revision 2257 by niro, Mon Oct 21 14:01:48 2013 UTC
# Line 36  Line 36 
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
# Line 58  int debug = 0; /* Currently just for tem Line 60  int debug = 0; /* Currently just for tem
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;
# Line 132  struct configFileInfo; Line 136  struct configFileInfo;
136  typedef const char *(*findConfigFunc)(struct configFileInfo *);  typedef const char *(*findConfigFunc)(struct configFileInfo *);
137  typedef const int (*writeLineFunc)(struct configFileInfo *,  typedef const int (*writeLineFunc)(struct configFileInfo *,
138   struct singleLine *line);   struct singleLine *line);
139    typedef char *(*getEnvFunc)(struct configFileInfo *, char *name);
140    typedef int (*setEnvFunc)(struct configFileInfo *, char *name, char *value);
141    
142  struct configFileInfo {  struct configFileInfo {
143      char * defaultConfig;      char * defaultConfig;
144      findConfigFunc findConfig;      findConfigFunc findConfig;
145      writeLineFunc writeLine;      writeLineFunc writeLine;
146        getEnvFunc getEnv;
147        setEnvFunc setEnv;
148      struct keywordTypes * keywords;      struct keywordTypes * keywords;
149      int caseInsensitive;      int caseInsensitive;
150      int defaultIsIndex;      int defaultIsIndex;
151      int defaultIsVariable;      int defaultIsVariable;
152      int defaultSupportSaved;      int defaultSupportSaved;
153        int defaultIsSaved;
154      enum lineType_e entryStart;      enum lineType_e entryStart;
155      enum lineType_e entryEnd;      enum lineType_e entryEnd;
156      int needsBootPrefix;      int needsBootPrefix;
# Line 153  struct configFileInfo { Line 162  struct configFileInfo {
162      int mbInitRdIsModule;      int mbInitRdIsModule;
163      int mbConcatArgs;      int mbConcatArgs;
164      int mbAllowExtraInitRds;      int mbAllowExtraInitRds;
165        char *envFile;
166  };  };
167    
168  struct keywordTypes grubKeywords[] = {  struct keywordTypes grubKeywords[] = {
# Line 226  const char *grub2FindConfig(struct confi Line 236  const char *grub2FindConfig(struct confi
236      };      };
237      static int i = -1;      static int i = -1;
238      static const char *grub_cfg = "/boot/grub/grub.cfg";      static const char *grub_cfg = "/boot/grub/grub.cfg";
239        int rc = -1;
240    
241      if (i == -1) {      if (i == -1) {
242   for (i = 0; configFiles[i] != NULL; i++) {   for (i = 0; configFiles[i] != NULL; i++) {
243      dbgPrintf("Checking \"%s\": ", configFiles[i]);      dbgPrintf("Checking \"%s\": ", configFiles[i]);
244      if (!access(configFiles[i], R_OK)) {      if ((rc = access(configFiles[i], R_OK))) {
245     if (errno == EACCES) {
246        printf("Unable to access bootloader configuration file "
247           "\"%s\": %m\n", configFiles[i]);
248        exit(1);
249     }
250     continue;
251        } else {
252   dbgPrintf("found\n");   dbgPrintf("found\n");
253   return configFiles[i];   return configFiles[i];
254      }      }
# Line 249  const char *grub2FindConfig(struct confi Line 267  const char *grub2FindConfig(struct confi
267      return configFiles[i];      return configFiles[i];
268  }  }
269    
270    /* kind of hacky.  It'll give the first 1024 bytes, ish. */
271    static char *grub2GetEnv(struct configFileInfo *info, char *name)
272    {
273        static char buf[1025];
274        char *s = NULL;
275        char *ret = NULL;
276        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
277        int rc = asprintf(&s, "grub2-editenv %s list | grep '^%s='", envFile, name);
278    
279        if (rc < 0)
280     return NULL;
281    
282        FILE *f = popen(s, "r");
283        if (!f)
284     goto out;
285    
286        memset(buf, '\0', sizeof (buf));
287        ret = fgets(buf, 1024, f);
288        pclose(f);
289    
290        if (ret) {
291     ret += strlen(name) + 1;
292     ret[strlen(ret) - 1] = '\0';
293        }
294        dbgPrintf("grub2GetEnv(%s): %s\n", name, ret);
295    out:
296        free(s);
297        return ret;
298    }
299    
300    static int sPopCount(const char *s, const char *c)
301    {
302        int ret = 0;
303        if (!s)
304     return -1;
305        for (int i = 0; s[i] != '\0'; i++)
306     for (int j = 0; c[j] != '\0'; j++)
307        if (s[i] == c[j])
308     ret++;
309        return ret;
310    }
311    
312    static char *shellEscape(const char *s)
313    {
314        int l = strlen(s) + sPopCount(s, "'") * 2;
315    
316        char *ret = calloc(l+1, sizeof (*ret));
317        if (!ret)
318     return NULL;
319        for (int i = 0, j = 0; s[i] != '\0'; i++, j++) {
320     if (s[i] == '\'')
321        ret[j++] = '\\';
322     ret[j] = s[i];
323        }
324        return ret;
325    }
326    
327    static void unquote(char *s)
328    {
329        int l = strlen(s);
330    
331        if ((s[l-1] == '\'' && s[0] == '\'') || (s[l-1] == '"' && s[0] == '"')) {
332     memmove(s, s+1, l-2);
333     s[l-2] = '\0';
334        }
335    }
336    
337    static int grub2SetEnv(struct configFileInfo *info, char *name, char *value)
338    {
339        char *s = NULL;
340        int rc = 0;
341        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
342    
343        unquote(value);
344        value = shellEscape(value);
345        if (!value)
346        return -1;
347    
348        rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value);
349        free(value);
350        if (rc <0)
351     return -1;
352    
353        dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
354        rc = system(s);
355        free(s);
356        return rc;
357    }
358    
359    /* this is a gigantic hack to avoid clobbering grub2 variables... */
360    static int is_special_grub2_variable(const char *name)
361    {
362        if (!strcmp(name,"\"${next_entry}\""))
363     return 1;
364        if (!strcmp(name,"\"${prev_saved_entry}\""))
365     return 1;
366        return 0;
367    }
368    
369  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
370    int count = 0;    int count = 0;
371    
# Line 343  char *grub2ExtractTitle(struct singleLin Line 460  char *grub2ExtractTitle(struct singleLin
460    
461  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
462      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
463        .getEnv = grub2GetEnv,
464        .setEnv = grub2SetEnv,
465      .keywords = grub2Keywords,      .keywords = grub2Keywords,
466      .defaultIsIndex = 1,      .defaultIsIndex = 1,
467      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 522  struct singleEntry * findEntryByIndex(st Line 641  struct singleEntry * findEntryByIndex(st
641  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
642       const char * path, const char * prefix,       const char * path, const char * prefix,
643       int * index);       int * index);
644    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
645          int * index);
646  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
647  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
648  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 679  static int isEntryStart(struct singleLin Line 800  static int isEntryStart(struct singleLin
800  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
801  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct singleLine * line) {
802      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it (leaks a byte) */
803      char * title;      char * title = NULL;
804      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
805      title++;   title = strdup(line->elements[0].item);
806      *(title + strlen(title) - 1) = '\0';   title++;
807     *(title + strlen(title) - 1) = '\0';
808        } else if (line->type == LT_MENUENTRY)
809     title = strdup(line->elements[1].item);
810        else
811     return NULL;
812      return title;      return title;
813  }  }
814    
# Line 954  static struct grubConfig * readConfig(co Line 1080  static struct grubConfig * readConfig(co
1080      int len;      int len;
1081      char * buf;      char * buf;
1082    
1083      if (!strcmp(inName, "-")) {      if (inName == NULL) {
1084            printf("Could not find bootloader configuration\n");
1085            exit(1);
1086        } else if (!strcmp(inName, "-")) {
1087   in = 0;   in = 0;
1088      } else {      } else {
1089   if ((in = open(inName, O_RDONLY)) < 0) {   if ((in = open(inName, O_RDONLY)) < 0) {
# Line 1021  static struct grubConfig * readConfig(co Line 1150  static struct grubConfig * readConfig(co
1150      dbgPrintf("\n");      dbgPrintf("\n");
1151      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1152      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1153      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1154        !is_special_grub2_variable(line->elements[2].item)) {
1155   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1156   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1157   defaultLine = line;   defaultLine = line;
# Line 1226  static struct grubConfig * readConfig(co Line 1356  static struct grubConfig * readConfig(co
1356          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1357      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1358      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1359      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1360     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1361     if (cfg->cfi->getEnv) {
1362        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1363        if (defTitle) {
1364     int index = 0;
1365     entry = findEntryByTitle(cfg, defTitle, &index);
1366     if (entry)
1367        cfg->defaultImage = index;
1368        }
1369     }
1370   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1371      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1372      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1267  static struct grubConfig * readConfig(co Line 1407  static struct grubConfig * readConfig(co
1407          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1408      }      }
1409   }   }
1410        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1411     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1412     if (defTitle) {
1413        int index = 0;
1414        entry = findEntryByTitle(cfg, defTitle, &index);
1415        if (entry)
1416     cfg->defaultImage = index;
1417     }
1418      } else {      } else {
1419          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1420      }      }
# Line 1284  static void writeDefault(FILE * out, cha Line 1432  static void writeDefault(FILE * out, cha
1432    
1433      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1434   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1435      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1436   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1437      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1438        char *title;
1439        entry = findEntryByIndex(cfg, cfg->defaultImage);
1440        line = getLineByType(LT_MENUENTRY, entry->lines);
1441        if (!line)
1442     line = getLineByType(LT_TITLE, entry->lines);
1443        if (line) {
1444     title = extractTitle(line);
1445     if (title)
1446        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1447        }
1448     }
1449        } else if (cfg->defaultImage > -1) {
1450   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1451      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1452          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1389  static int writeConfig(struct grubConfig Line 1549  static int writeConfig(struct grubConfig
1549      while (line) {      while (line) {
1550          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1551   line->numElements == 3 &&   line->numElements == 3 &&
1552   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1553     !is_special_grub2_variable(line->elements[2].item)) {
1554      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1555      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1556   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1534  static char *findDiskForRoot() Line 1695  static char *findDiskForRoot()
1695      return NULL;      return NULL;
1696  }  }
1697    
1698  void printEntry(struct singleEntry * entry) {  void printEntry(struct singleEntry * entry, FILE *f) {
1699      int i;      int i;
1700      struct singleLine * line;      struct singleLine * line;
1701    
1702      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1703   fprintf(stderr, "DBG: %s", line->indent);   log_message(f, "DBG: %s", line->indent);
1704   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1705      /* Need to handle this, because we strip the quotes from      /* Need to handle this, because we strip the quotes from
1706       * menuentry when read it. */       * menuentry when read it. */
1707      if (line->type == LT_MENUENTRY && i == 1) {      if (line->type == LT_MENUENTRY && i == 1) {
1708   if(!isquote(*line->elements[i].item))   if(!isquote(*line->elements[i].item))
1709      fprintf(stderr, "\'%s\'", line->elements[i].item);      log_message(f, "\'%s\'", line->elements[i].item);
1710   else   else
1711      fprintf(stderr, "%s", line->elements[i].item);      log_message(f, "%s", line->elements[i].item);
1712   fprintf(stderr, "%s", line->elements[i].indent);   log_message(f, "%s", line->elements[i].indent);
1713    
1714   continue;   continue;
1715      }      }
1716            
1717      fprintf(stderr, "%s%s",      log_message(f, "%s%s",
1718      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1719   }   }
1720   fprintf(stderr, "\n");   log_message(f, "\n");
1721      }      }
1722  }  }
1723    
1724  void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)  void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...)
1725  {  {
1726      va_list argp;      static int once;
1727        va_list argp, argq;
1728    
1729        va_start(argp, fmt);
1730    
1731        va_copy(argq, argp);
1732        if (!once) {
1733     log_time(NULL);
1734     log_message(NULL, "command line: %s\n", saved_command_line);
1735        }
1736        log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed");
1737        log_vmessage(NULL, fmt, argq);
1738    
1739        printEntry(entry, NULL);
1740        va_end(argq);
1741    
1742      if (!debug)      if (!debug) {
1743     once = 1;
1744         va_end(argp);
1745   return;   return;
1746        }
1747    
1748      va_start(argp, fmt);      if (okay) {
1749     va_end(argp);
1750     return;
1751        }
1752    
1753        if (!once)
1754     log_message(stderr, "DBG: command line: %s\n", saved_command_line);
1755        once = 1;
1756      fprintf(stderr, "DBG: Image entry failed: ");      fprintf(stderr, "DBG: Image entry failed: ");
1757      vfprintf(stderr, fmt, argp);      vfprintf(stderr, fmt, argp);
1758      printEntry(entry);      printEntry(entry, stderr);
1759      va_end(argp);      va_end(argp);
1760  }  }
1761    
# Line 1597  int suitableImage(struct singleEntry * e Line 1782  int suitableImage(struct singleEntry * e
1782      char * rootdev;      char * rootdev;
1783    
1784      if (skipRemoved && entry->skip) {      if (skipRemoved && entry->skip) {
1785   notSuitablePrintf(entry, "marked to skip\n");   notSuitablePrintf(entry, 0, "marked to skip\n");
1786   return 0;   return 0;
1787      }      }
1788    
1789      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1790      if (!line) {      if (!line) {
1791   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1792   return 0;   return 0;
1793      }      }
1794      if (line->numElements < 2) {      if (line->numElements < 2) {
1795   notSuitablePrintf(entry, "line has only %d elements\n",   notSuitablePrintf(entry, 0, "line has only %d elements\n",
1796      line->numElements);      line->numElements);
1797   return 0;   return 0;
1798      }      }
1799    
1800      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) {
1801        notSuitablePrintf(entry, 1, "\n");
1802        return 1;
1803        }
1804    
1805      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1806        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
# Line 1623  int suitableImage(struct singleEntry * e Line 1811  int suitableImage(struct singleEntry * e
1811      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",      sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1812              line->elements[1].item + rootspec_offset);              line->elements[1].item + rootspec_offset);
1813      if (access(fullName, R_OK)) {      if (access(fullName, R_OK)) {
1814   notSuitablePrintf(entry, "access to %s failed\n", fullName);   notSuitablePrintf(entry, 0, "access to %s failed\n", fullName);
1815   return 0;   return 0;
1816      }      }
1817      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
# Line 1644  int suitableImage(struct singleEntry * e Line 1832  int suitableImage(struct singleEntry * e
1832    
1833              /* failed to find one */              /* failed to find one */
1834              if (!line) {              if (!line) {
1835   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1836   return 0;   return 0;
1837              }              }
1838    
# Line 1653  int suitableImage(struct singleEntry * e Line 1841  int suitableImage(struct singleEntry * e
1841      if (i < line->numElements)      if (i < line->numElements)
1842          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1843      else {      else {
1844   notSuitablePrintf(entry, "no root= entry found\n");   notSuitablePrintf(entry, 0, "no root= entry found\n");
1845   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1846          return 0;          return 0;
1847              }              }
# Line 1662  int suitableImage(struct singleEntry * e Line 1850  int suitableImage(struct singleEntry * e
1850    
1851      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1852      if (!getpathbyspec(dev)) {      if (!getpathbyspec(dev)) {
1853          notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);          notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev);
1854          return 0;          return 0;
1855      } else      } else
1856   dev = getpathbyspec(dev);   dev = getpathbyspec(dev);
1857    
1858      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1859      if (!rootdev) {      if (!rootdev) {
1860          notSuitablePrintf(entry, "can't find root device\n");          notSuitablePrintf(entry, 0, "can't find root device\n");
1861   return 0;   return 0;
1862      }      }
1863    
1864      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1865          notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n",
1866   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1867          free(rootdev);          free(rootdev);
1868          return 0;          return 0;
1869      }      }
1870    
1871      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1872          notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",          notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n",
1873   getuuidbydev(rootdev), getuuidbydev(dev));   getuuidbydev(rootdev), getuuidbydev(dev));
1874   free(rootdev);   free(rootdev);
1875          return 0;          return 0;
1876      }      }
1877    
1878      free(rootdev);      free(rootdev);
1879        notSuitablePrintf(entry, 1, "\n");
1880    
1881      return 1;      return 1;
1882  }  }
# Line 1816  struct singleEntry * findEntryByPath(str Line 2005  struct singleEntry * findEntryByPath(str
2005      return entry;      return entry;
2006  }  }
2007    
2008    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2009          int * index) {
2010        struct singleEntry * entry;
2011        struct singleLine * line;
2012        int i;
2013        char * newtitle;
2014    
2015        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2016     if (index && i < *index)
2017        continue;
2018     line = getLineByType(LT_TITLE, entry->lines);
2019     if (!line)
2020        line = getLineByType(LT_MENUENTRY, entry->lines);
2021     if (!line)
2022        continue;
2023     newtitle = grub2ExtractTitle(line);
2024     if (!newtitle)
2025        continue;
2026     if (!strcmp(title, newtitle))
2027        break;
2028        }
2029    
2030        if (!entry)
2031     return NULL;
2032    
2033        if (index)
2034     *index = i;
2035        return entry;
2036    }
2037    
2038  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2039      struct singleEntry * entry;      struct singleEntry * entry;
2040    
# Line 1838  struct singleEntry * findTemplate(struct Line 2057  struct singleEntry * findTemplate(struct
2057      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2058      int index;      int index;
2059    
2060      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2061     if (cfg->cfi->getEnv) {
2062        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2063        if (defTitle) {
2064     int index = 0;
2065     entry = findEntryByTitle(cfg, defTitle, &index);
2066        }
2067     }
2068        } else if (cfg->defaultImage > -1) {
2069   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2070   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2071      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 3757  static void traceback(int signum) Line 3984  static void traceback(int signum)
3984      memset(array, '\0', sizeof (array));      memset(array, '\0', sizeof (array));
3985      size = backtrace(array, 40);      size = backtrace(array, 40);
3986    
3987      fprintf(stderr, "grubby recieved SIGSEGV!  Backtrace (%ld):\n",      fprintf(stderr, "grubby received SIGSEGV!  Backtrace (%ld):\n",
3988              (unsigned long)size);              (unsigned long)size);
3989      backtrace_symbols_fd(array, size, STDERR_FILENO);      backtrace_symbols_fd(array, size, STDERR_FILENO);
3990      exit(1);      exit(1);
# Line 3792  int main(int argc, const char ** argv) { Line 4019  int main(int argc, const char ** argv) {
4019      char * removeArgs = NULL;      char * removeArgs = NULL;
4020      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4021      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4022        char * envPath = NULL;
4023      const char * chptr = NULL;      const char * chptr = NULL;
4024      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4025      struct grubConfig * config;      struct grubConfig * config;
# Line 3843  int main(int argc, const char ** argv) { Line 4071  int main(int argc, const char ** argv) {
4071      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4072   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4073      _("force grub2 stanzas to use efi") },      _("force grub2 stanzas to use efi") },
4074     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4075        _("path for environment data"),
4076        _("path") },
4077   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4078      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4079   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3855  int main(int argc, const char ** argv) { Line 4086  int main(int argc, const char ** argv) {
4086   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,   { "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0,
4087      _("initrd image for the new kernel"), _("initrd-path") },      _("initrd image for the new kernel"), _("initrd-path") },
4088   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',   { "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i',
4089      _("auxilliary initrd image for things other than the new kernel"), _("initrd-path") },      _("auxiliary initrd image for things other than the new kernel"), _("initrd-path") },
4090   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,   { "lilo", 0, POPT_ARG_NONE, &configureLilo, 0,
4091      _("configure lilo bootloader") },      _("configure lilo bootloader") },
4092   { "make-default", 0, 0, &makeDefault, 0,   { "make-default", 0, 0, &makeDefault, 0,
# Line 3899  int main(int argc, const char ** argv) { Line 4130  int main(int argc, const char ** argv) {
4130    
4131      signal(SIGSEGV, traceback);      signal(SIGSEGV, traceback);
4132    
4133        int i = 0;
4134        for (int j = 1; j < argc; j++)
4135     i += strlen(argv[j]) + 1;
4136        saved_command_line = malloc(i);
4137        if (!saved_command_line) {
4138     fprintf(stderr, "grubby: %m\n");
4139     exit(1);
4140        }
4141        saved_command_line[0] = '\0';
4142        for (int j = 1; j < argc; j++) {
4143     strcat(saved_command_line, argv[j]);
4144     strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
4145        }
4146    
4147      optCon = poptGetContext("grubby", argc, argv, options, 0);      optCon = poptGetContext("grubby", argc, argv, options, 0);
4148      poptReadDefaultConfig(optCon, 1);      poptReadDefaultConfig(optCon, 1);
4149    
# Line 3942  int main(int argc, const char ** argv) { Line 4187  int main(int argc, const char ** argv) {
4187   return 1;   return 1;
4188      } else if (configureGrub2) {      } else if (configureGrub2) {
4189   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4190     if (envPath)
4191        cfi->envFile = envPath;
4192      } else if (configureLilo) {      } else if (configureLilo) {
4193   cfi = &liloConfigType;   cfi = &liloConfigType;
4194      } else if (configureGrub) {      } else if (configureGrub) {
# Line 4142  int main(int argc, const char ** argv) { Line 4389  int main(int argc, const char ** argv) {
4389   return 0;   return 0;
4390      }      }
4391    
4392        if (grubConfig == NULL) {
4393     printf("Could not find bootloader configuration file.\n");
4394     exit(1);
4395        }
4396    
4397      config = readConfig(grubConfig, cfi);      config = readConfig(grubConfig, cfi);
4398      if (!config) return 1;      if (!config) return 1;
4399    
# Line 4151  int main(int argc, const char ** argv) { Line 4403  int main(int argc, const char ** argv) {
4403          char * rootspec;          char * rootspec;
4404    
4405   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4406     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4407     cfi->defaultIsSaved)
4408        config->defaultImage = 0;
4409   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4410   if (!entry) return 0;   if (!entry) return 0;
4411   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
# Line 4169  int main(int argc, const char ** argv) { Line 4424  int main(int argc, const char ** argv) {
4424   struct singleEntry * entry;   struct singleEntry * entry;
4425    
4426   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4427     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4428     cfi->defaultIsSaved)
4429        config->defaultImage = 0;
4430   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4431   if (!entry) return 0;   if (!entry) return 0;
4432    
# Line 4191  int main(int argc, const char ** argv) { Line 4449  int main(int argc, const char ** argv) {
4449    
4450      } else if (displayDefaultIndex) {      } else if (displayDefaultIndex) {
4451          if (config->defaultImage == -1) return 0;          if (config->defaultImage == -1) return 0;
4452     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4453     cfi->defaultIsSaved)
4454        config->defaultImage = 0;
4455          printf("%i\n", config->defaultImage);          printf("%i\n", config->defaultImage);
4456            return 0;
4457    
4458      } else if (kernelInfo)      } else if (kernelInfo)
4459   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);

Legend:
Removed from v.2056  
changed lines
  Added in v.2257