chiark / gitweb /
bus: restore selinux access control to PID 1 for properties
[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
108         return 0;
109 }
110
111 int boot_loader_find_active_entry(struct boot_info *info, const char *loader_active) {
112         char *fn;
113         unsigned int i;
114
115         if (!loader_active)
116                 return -ENOENT;
117         if (info->loader_entries_count == 0)
118                 return -ENOENT;
119
120         if (asprintf(&fn, "/boot/loader/entries/%s.conf", loader_active) < 0)
121                 return -ENOMEM;
122
123         for (i = 0; i < info->loader_entries_count; i++) {
124                 if (streq(fn, info->loader_entries[i].path)) {
125                         info->loader_entry_active = i;
126                         break;
127                 }
128         }
129
130         free(fn);
131         return 0;
132 }