Magellan Linux

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

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

revision 2250 by niro, Mon Oct 21 13:56:22 2013 UTC revision 2262 by niro, Mon Oct 21 14:06:22 2013 UTC
# Line 136  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 157  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 176  const char *grubFindConfig(struct config Line 182  const char *grubFindConfig(struct config
182   "/boot/grub/grub.conf",   "/boot/grub/grub.conf",
183   "/boot/grub/menu.lst",   "/boot/grub/menu.lst",
184   "/etc/grub.conf",   "/etc/grub.conf",
185     "/boot/grub2/grub.cfg",
186     "/boot/grub2-efi/grub.cfg",
187   NULL   NULL
188      };      };
189      static int i = -1;      static int i = -1;
# Line 261  const char *grub2FindConfig(struct confi Line 269  const char *grub2FindConfig(struct confi
269      return configFiles[i];      return configFiles[i];
270  }  }
271    
272    /* kind of hacky.  It'll give the first 1024 bytes, ish. */
273    static char *grub2GetEnv(struct configFileInfo *info, char *name)
274    {
275        static char buf[1025];
276        char *s = NULL;
277        char *ret = NULL;
278        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
279        int rc = asprintf(&s, "grub2-editenv %s list | grep '^%s='", envFile, name);
280    
281        if (rc < 0)
282     return NULL;
283    
284        FILE *f = popen(s, "r");
285        if (!f)
286     goto out;
287    
288        memset(buf, '\0', sizeof (buf));
289        ret = fgets(buf, 1024, f);
290        pclose(f);
291    
292        if (ret) {
293     ret += strlen(name) + 1;
294     ret[strlen(ret) - 1] = '\0';
295        }
296        dbgPrintf("grub2GetEnv(%s): %s\n", name, ret);
297    out:
298        free(s);
299        return ret;
300    }
301    
302    static int sPopCount(const char *s, const char *c)
303    {
304        int ret = 0;
305        if (!s)
306     return -1;
307        for (int i = 0; s[i] != '\0'; i++)
308     for (int j = 0; c[j] != '\0'; j++)
309        if (s[i] == c[j])
310     ret++;
311        return ret;
312    }
313    
314    static char *shellEscape(const char *s)
315    {
316        int l = strlen(s) + sPopCount(s, "'") * 2;
317    
318        char *ret = calloc(l+1, sizeof (*ret));
319        if (!ret)
320     return NULL;
321        for (int i = 0, j = 0; s[i] != '\0'; i++, j++) {
322     if (s[i] == '\'')
323        ret[j++] = '\\';
324     ret[j] = s[i];
325        }
326        return ret;
327    }
328    
329    static void unquote(char *s)
330    {
331        int l = strlen(s);
332    
333        if ((s[l-1] == '\'' && s[0] == '\'') || (s[l-1] == '"' && s[0] == '"')) {
334     memmove(s, s+1, l-2);
335     s[l-2] = '\0';
336        }
337    }
338    
339    static int grub2SetEnv(struct configFileInfo *info, char *name, char *value)
340    {
341        char *s = NULL;
342        int rc = 0;
343        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
344    
345        unquote(value);
346        value = shellEscape(value);
347        if (!value)
348        return -1;
349    
350        rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value);
351        free(value);
352        if (rc <0)
353     return -1;
354    
355        dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
356        rc = system(s);
357        free(s);
358        return rc;
359    }
360    
361    /* this is a gigantic hack to avoid clobbering grub2 variables... */
362    static int is_special_grub2_variable(const char *name)
363    {
364        if (!strcmp(name,"\"${next_entry}\""))
365     return 1;
366        if (!strcmp(name,"\"${prev_saved_entry}\""))
367     return 1;
368        return 0;
369    }
370    
371  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
372    int count = 0;    int count = 0;
373    
# Line 355  char *grub2ExtractTitle(struct singleLin Line 462  char *grub2ExtractTitle(struct singleLin
462    
463  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
464      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
465        .getEnv = grub2GetEnv,
466        .setEnv = grub2SetEnv,
467      .keywords = grub2Keywords,      .keywords = grub2Keywords,
468      .defaultIsIndex = 1,      .defaultIsIndex = 1,
469      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 534  struct singleEntry * findEntryByIndex(st Line 643  struct singleEntry * findEntryByIndex(st
643  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
644       const char * path, const char * prefix,       const char * path, const char * prefix,
645       int * index);       int * index);
646    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
647          int * index);
648  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
649  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
650  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 691  static int isEntryStart(struct singleLin Line 802  static int isEntryStart(struct singleLin
802  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
803  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct singleLine * line) {
804      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it (leaks a byte) */
805      char * title;      char * title = NULL;
806      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
807      title++;   title = strdup(line->elements[0].item);
808      *(title + strlen(title) - 1) = '\0';   title++;
809     *(title + strlen(title) - 1) = '\0';
810        } else if (line->type == LT_MENUENTRY)
811     title = strdup(line->elements[1].item);
812        else
813     return NULL;
814      return title;      return title;
815  }  }
816    
# Line 952  static int getNextLine(char ** bufPtr, s Line 1068  static int getNextLine(char ** bufPtr, s
1068      return 0;      return 0;
1069  }  }
1070    
1071    static int isnumber(const char *s)
1072    {
1073        int i;
1074        for (i = 0; s[i] != '\0'; i++)
1075     if (s[i] < '0' || s[i] > '9')
1076        return 0;
1077        return i;
1078    }
1079    
1080  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1081        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1082      int in;      int in;
# Line 1036  static struct grubConfig * readConfig(co Line 1161  static struct grubConfig * readConfig(co
1161      dbgPrintf("\n");      dbgPrintf("\n");
1162      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1163      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1164      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1165        !is_special_grub2_variable(line->elements[2].item)) {
1166   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1167   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1168   defaultLine = line;   defaultLine = line;
# Line 1241  static struct grubConfig * readConfig(co Line 1367  static struct grubConfig * readConfig(co
1367          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1368      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1369      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1370      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1371     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1372     if (cfg->cfi->getEnv) {
1373        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1374        if (defTitle) {
1375     int index = 0;
1376     if (isnumber(defTitle)) {
1377        index = atoi(defTitle);
1378        entry = findEntryByIndex(cfg, index);
1379     } else {
1380        entry = findEntryByTitle(cfg, defTitle, &index);
1381     }
1382     if (entry)
1383        cfg->defaultImage = index;
1384        }
1385     }
1386   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1387      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1388      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1282  static struct grubConfig * readConfig(co Line 1423  static struct grubConfig * readConfig(co
1423          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1424      }      }
1425   }   }
1426        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1427     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1428     if (defTitle) {
1429        int index = 0;
1430        if (isnumber(defTitle)) {
1431     index = atoi(defTitle);
1432     entry = findEntryByIndex(cfg, index);
1433        } else {
1434     entry = findEntryByTitle(cfg, defTitle, &index);
1435        }
1436        if (entry)
1437     cfg->defaultImage = index;
1438     }
1439      } else {      } else {
1440          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1441      }      }
# Line 1299  static void writeDefault(FILE * out, cha Line 1453  static void writeDefault(FILE * out, cha
1453    
1454      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1455   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1456      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1457   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1458      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1459        char *title;
1460        entry = findEntryByIndex(cfg, cfg->defaultImage);
1461        line = getLineByType(LT_MENUENTRY, entry->lines);
1462        if (!line)
1463     line = getLineByType(LT_TITLE, entry->lines);
1464        if (line) {
1465     title = extractTitle(line);
1466     if (title)
1467        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1468        }
1469     }
1470        } else if (cfg->defaultImage > -1) {
1471   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1472      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1473          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1404  static int writeConfig(struct grubConfig Line 1570  static int writeConfig(struct grubConfig
1570      while (line) {      while (line) {
1571          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1572   line->numElements == 3 &&   line->numElements == 3 &&
1573   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1574     !is_special_grub2_variable(line->elements[2].item)) {
1575      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1576      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1577   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1859  struct singleEntry * findEntryByPath(str Line 2026  struct singleEntry * findEntryByPath(str
2026      return entry;      return entry;
2027  }  }
2028    
2029    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2030          int * index) {
2031        struct singleEntry * entry;
2032        struct singleLine * line;
2033        int i;
2034        char * newtitle;
2035    
2036        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2037     if (index && i < *index)
2038        continue;
2039     line = getLineByType(LT_TITLE, entry->lines);
2040     if (!line)
2041        line = getLineByType(LT_MENUENTRY, entry->lines);
2042     if (!line)
2043        continue;
2044     newtitle = grub2ExtractTitle(line);
2045     if (!newtitle)
2046        continue;
2047     if (!strcmp(title, newtitle))
2048        break;
2049        }
2050    
2051        if (!entry)
2052     return NULL;
2053    
2054        if (index)
2055     *index = i;
2056        return entry;
2057    }
2058    
2059  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2060      struct singleEntry * entry;      struct singleEntry * entry;
2061    
# Line 1881  struct singleEntry * findTemplate(struct Line 2078  struct singleEntry * findTemplate(struct
2078      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2079      int index;      int index;
2080    
2081      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2082     if (cfg->cfi->getEnv) {
2083        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2084        if (defTitle) {
2085     int index = 0;
2086     if (isnumber(defTitle)) {
2087        index = atoi(defTitle);
2088        entry = findEntryByIndex(cfg, index);
2089     } else {
2090        entry = findEntryByTitle(cfg, defTitle, &index);
2091     }
2092     if (entry)
2093        cfg->defaultImage = index;
2094        }
2095     }
2096        } else if (cfg->defaultImage > -1) {
2097   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2098   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2099      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 3835  int main(int argc, const char ** argv) { Line 4047  int main(int argc, const char ** argv) {
4047      char * removeArgs = NULL;      char * removeArgs = NULL;
4048      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4049      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4050        char * envPath = NULL;
4051      const char * chptr = NULL;      const char * chptr = NULL;
4052      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4053      struct grubConfig * config;      struct grubConfig * config;
# Line 3886  int main(int argc, const char ** argv) { Line 4099  int main(int argc, const char ** argv) {
4099      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4100   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4101      _("force grub2 stanzas to use efi") },      _("force grub2 stanzas to use efi") },
4102     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4103        _("path for environment data"),
4104        _("path") },
4105   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4106      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4107   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3999  int main(int argc, const char ** argv) { Line 4215  int main(int argc, const char ** argv) {
4215   return 1;   return 1;
4216      } else if (configureGrub2) {      } else if (configureGrub2) {
4217   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4218     if (envPath)
4219        cfi->envFile = envPath;
4220      } else if (configureLilo) {      } else if (configureLilo) {
4221   cfi = &liloConfigType;   cfi = &liloConfigType;
4222      } else if (configureGrub) {      } else if (configureGrub) {
# Line 4213  int main(int argc, const char ** argv) { Line 4431  int main(int argc, const char ** argv) {
4431          char * rootspec;          char * rootspec;
4432    
4433   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4434     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4435     cfi->defaultIsSaved)
4436        config->defaultImage = 0;
4437   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4438   if (!entry) return 0;   if (!entry) return 0;
4439   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
# Line 4231  int main(int argc, const char ** argv) { Line 4452  int main(int argc, const char ** argv) {
4452   struct singleEntry * entry;   struct singleEntry * entry;
4453    
4454   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4455     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4456     cfi->defaultIsSaved)
4457        config->defaultImage = 0;
4458   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4459   if (!entry) return 0;   if (!entry) return 0;
4460    
# Line 4253  int main(int argc, const char ** argv) { Line 4477  int main(int argc, const char ** argv) {
4477    
4478      } else if (displayDefaultIndex) {      } else if (displayDefaultIndex) {
4479          if (config->defaultImage == -1) return 0;          if (config->defaultImage == -1) return 0;
4480     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4481     cfi->defaultIsSaved)
4482        config->defaultImage = 0;
4483          printf("%i\n", config->defaultImage);          printf("%i\n", config->defaultImage);
4484          return 0;          return 0;
4485    

Legend:
Removed from v.2250  
changed lines
  Added in v.2262