This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplymouth.c
279 lines (227 loc) · 6.89 KB
/
plymouth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* plymouth.c - Plymouth plugin for the OpenRC init system
*
* Copyright (C) 2011 Amadeusz Żołnowski <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <assert.h>
#include <einfo.h>
#include <rc.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef DEBUG
# define DBG(x) einfo("[plymouth-plugin] " x)
#else
# define DBG(x)
#endif
#define BUFFER_SIZE 300
#define RWDIR (R_OK | W_OK | X_OK)
#ifndef RUN_DIR
#define RUN_DIR "/run/plymouth"
#endif
#ifndef PID_FILE
#define PID_FILE RUN_DIR "/pid"
#endif
enum {
PLY_MODE_BOOT,
PLY_MODE_SHUTDOWN
};
int command(const char* cmd)
{
int rv = system(cmd);
#if DEBUG
if(rv != 0) {
ewarn("[plymouth-plugin] command(\"%s\"): rv=%d", cmd, rv);
}
#endif
return rv;
}
int commandf(const char* cmd, ...)
{
static char buffer[BUFFER_SIZE];
va_list ap;
int rv;
va_start(ap, cmd);
rv = vsnprintf(buffer, BUFFER_SIZE, cmd, ap);
va_end(ap);
if(rv >= BUFFER_SIZE) {
eerror("[plymouth-plugin] command(\"%s\"): buffer overflow", buffer);
return -1;
}
return command(buffer);
}
bool ply_message(const char* hook, const char* name)
{
return (commandf("/bin/plymouth message --text=\"%s %s\"", hook, name) == 0);
}
bool ply_ping()
{
return (system("/bin/plymouth --ping") == 0);
}
bool ply_quit(int mode)
{
int rv = 0;
if(mode == PLY_MODE_BOOT)
rv = command("/bin/plymouth quit");
else if(mode == PLY_MODE_SHUTDOWN)
rv = command("/bin/plymouth quit --retain-splash");
else
assert(0 && "Unknown mode");
return (rv == 0);
}
bool ply_start(int mode)
{
int rv = 0;
if(!ply_ping()) {
ebegin("Starting plymouthd");
if(access(RUN_DIR, RWDIR) != 0) {
if(mkdir(RUN_DIR, 0755) != 0) {
eerror("[plymouth-plugin] Couldn't create " RUN_DIR);
return false;
}
}
#define PLYD "/sbin/plymouthd --attach-to-session --pid-file=" PID_FILE \
" --mode="
if(mode == PLY_MODE_BOOT)
rv = command(PLYD "boot");
else if(mode == PLY_MODE_SHUTDOWN)
rv = command(PLYD "shutdown");
else
assert(0 && "Unknown mode");
#undef PLYD
eend(rv, "");
if((rv == 0) && command("/bin/plymouth --show-splash") != 0)
return false;
}
return true;
}
bool ply_update_status(int hook, const char* name)
{
return (commandf("/bin/plymouth update --status=%d-%s", hook, name) == 0);
}
bool ply_update_rootfs_rw()
{
const int rwdir = RWDIR;
if(access("/var/lib/plymouth", rwdir) != 0
|| access("/var/log", rwdir) != 0) {
eerror("[plymouth-plugin] /var/lib/plymouth and /var/log need to be "
"writable at this stage, but are not!");
return false;
}
return (command("/bin/plymouth update-root-fs --read-write") == 0);
}
int rc_plugin_hook(RC_HOOK hook, const char *name)
{
int rv = 0;
char* runlevel = rc_runlevel_get();
const char* bootlevel = getenv("RC_BOOTLEVEL");
const char* defaultlevel = getenv("RC_DEFAULTLEVEL");
#ifdef DEBUG
einfo("hook=%d name=%s runlvl=%s plyd=%d", hook, name, runlevel,
ply_ping());
#endif
/* Don't do anything if we're not booting or shutting down. */
if(!(rc_runlevel_starting() || rc_runlevel_stopping())) {
switch(hook) {
case RC_HOOK_RUNLEVEL_STOP_IN:
case RC_HOOK_RUNLEVEL_STOP_OUT:
case RC_HOOK_RUNLEVEL_START_IN:
case RC_HOOK_RUNLEVEL_START_OUT:
/* Switching runlevels, so we're booting or shutting down.*/
break;
default:
DBG("Not booting or shutting down");
goto exit;
}
}
DBG("switch1");
switch(hook) {
case RC_HOOK_RUNLEVEL_START_IN:
case RC_HOOK_RUNLEVEL_STOP_IN:
case RC_HOOK_SERVICE_START_IN:
case RC_HOOK_SERVICE_STOP_IN:
ply_update_status(hook, name);
break;
default:
break;
}
DBG("switch2");
switch(hook) {
case RC_HOOK_RUNLEVEL_STOP_IN:
/* Start the Plymouth daemon and show splash when system is being shut
* down. */
if(strcmp(name, RC_LEVEL_SHUTDOWN) == 0) {
DBG("ply_start(PLY_MODE_SHUTDOWN)");
if(!ply_start(PLY_MODE_SHUTDOWN)
|| !ply_update_rootfs_rw())
rv = 1;
}
break;
case RC_HOOK_RUNLEVEL_START_IN:
/* Start the Plymouth daemon and show splash when entering the boot
* runlevel. Required /proc and /sys should already be mounted in
* sysinit runlevel. */
if(strcmp(name, bootlevel) == 0) {
DBG("ply_start(PLY_MODE_BOOT)");
if(!ply_start(PLY_MODE_BOOT))
rv = 1;
}
break;
case RC_HOOK_RUNLEVEL_START_OUT:
/* Stop the Plymouth daemon right after default runlevel is started. */
if(strcmp(name, defaultlevel) == 0) {
DBG("ply_quit(PLY_MODE_BOOT)");
if(!ply_quit(PLY_MODE_BOOT))
rv = 1;
}
break;
case RC_HOOK_SERVICE_STOP_IN:
/* Quit Plymouth when we're going to lost write access to /var/... */
if(strcmp(name, "mount-ro") == 0 &&
strcmp(runlevel, RC_LEVEL_SHUTDOWN) == 0) {
DBG("ply_quit(PLY_MODE_SHUTDOWN)");
if(!ply_quit(PLY_MODE_SHUTDOWN))
rv = 1;
}
break;
case RC_HOOK_SERVICE_STOP_NOW:
if(!ply_message("Stopping service", name))
rv = 1;
break;
case RC_HOOK_SERVICE_START_NOW:
if(!ply_message("Starting service", name))
rv = 1;
break;
case RC_HOOK_SERVICE_START_OUT:
/* Start Plymouth daemon if not yet started and tell we have rw access
* to /var/... */
if(strcmp(name, "localmount") == 0 &&
strcmp(runlevel, RC_LEVEL_SHUTDOWN) != 0) {
DBG("ply_update_rootfs_rw()");
if(!ply_update_rootfs_rw())
rv = 1;
}
break;
default:
break;
}
exit:
free(runlevel);
return rv;
}