chiark / gitweb /
Merge branch 'master' of ssh://git.freedesktop.org/git/systemd/systemd into work
[elogind.git] / src / boot / boot-loader.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Kay Sievers
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <locale.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <sys/timex.h>
30
31 #include "boot.h"
32 #include "boot-loader.h"
33 #include "build.h"
34 #include "util.h"
35 #include "strv.h"
36 #include "conf-files.h"
37
38 static char *loader_fragment_read_title(const char *fragment) {
39         FILE *f;
40         char line[LINE_MAX];
41         char *title = NULL;
42
43         f = fopen(fragment, "re");
44         if (!f)
45                 return NULL;
46
47         while (fgets(line, sizeof(line), f) != NULL) {
48                 char *s;
49                 size_t l;
50
51                 l = strlen(line);
52                 if (l < 1)
53                         continue;
54                 if (line[l-1] == '\n')
55                         line[l-1] = '\0';
56
57                 s = line;
58                 while (isspace(s[0]))
59                         s++;
60
61                 if (s[0] == '#')
62                         continue;
63
64                 if (!startswith(s, "title"))
65                         continue;
66
67                 s += strlen("title");
68                 if (!isspace(s[0]))
69                         continue;
70                 while (isspace(s[0]))
71                         s++;
72
73                 title = strdup(s);
74                 break;
75         }
76
77         fclose(f);
78         return title;
79 }
80
81 int boot_loader_read_entries(struct boot_info *info) {
82         _cleanup_strv_free_ char **files = NULL;
83         static const char *loader_dir[] = { "/boot/loader/entries", NULL};
84         unsigned int count;
85         unsigned int i;
86         int err;
87
88         err = conf_files_list_strv(&files, ".conf", NULL, loader_dir);
89         if (err < 0)
90                 return err;
91
92         count = strv_length(files);
93         info->loader_entries = new0(struct boot_info_entry, count);
94         if (!info->loader_entries)
95                 return -ENOMEM;
96
97         for (i = 0; i < count; i++) {
98                 info->loader_entries[i].title = loader_fragment_read_title(files[i]);
99                 info->loader_entries[i].path = strdup(files[i]);
100                 if (!info->loader_entries[i].title || !info->loader_entries[i].path) {
101                         free(info->loader_entries[i].title);
102                         free(info->loader_entries[i].path);
103                         return -ENOMEM;
104                 }
105                 info->loader_entries_count++;
106         }
107         return 0;
108 }
109
110 int boot_loader_find_active_entry(struct boot_info *info, const char *loader_active) {
111         char *fn;
112         unsigned int i;
113
114         if (!loader_active)
115                 return -ENOENT;
116         if (info->loader_entries_count == 0)
117                 return -ENOENT;
118
119         if (asprintf(&fn, "/boot/loader/entries/%s.conf", loader_active) < 0)
120                 return -ENOMEM;
121
122         for (i = 0; i < info->loader_entries_count; i++) {
123                 if (strcmp(fn, info->loader_entries[i].path) == 0) {
124                         info->loader_entry_active = i;
125                         break;
126                 }
127         }
128
129         free(fn);
130         return 0;
131 }