summaryrefslogtreecommitdiffstats
path: root/kdm/backend/bootman.c
blob: 291164ea7e9e7514bbe94d5f0b30e98ddb1ff454 (plain)
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
/*

Copyright 2005 Stephan Kulow <coolo@kde.org>
Copyright 2005 Oswald Buddenhagen <ossi@kde.org>

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the copyright holder.

*/

/*
 * xdm - display manager daemon
 * Author: Keith Packard, MIT X Consortium
 *
 * Boot options
 */

#include "dm.h"
#include "dm_error.h"

#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>

extern char **environ;

static int
getNull( char ***opts ATTR_UNUSED, int *def ATTR_UNUSED, int *cur ATTR_UNUSED )
{
	return BO_NOMAN;
}

static int
setNull( const char *opt ATTR_UNUSED, SdRec *sdr ATTR_UNUSED )
{
	return BO_NOMAN;
}

static char *
match( char *obuf, int *blen, const char *key, int klen )
{
	char *buf = obuf;
	if (memcmp( buf, key, klen ) || !isspace( buf[klen] ))
		return 0;
	buf += klen + 1;
	for (; isspace( *buf ); buf++);
	if (!*buf)
		return 0;
	*blen -= buf - obuf;
	return buf;
}

#define GRUB_MENU "/boot/grub/menu.lst"

static char *grub;

static int
getGrub( char ***opts, int *def, int *cur )
{
	FILE *f;
	char *ptr, *linp;
	int len;
	char line[1000];

	if (!grub && !(grub = locate( "grub-set-default" )))		
		return BO_NOMAN;

	*def = 0;
	*cur = -1;
	*opts = initStrArr( 0 );

	if (!(f = fopen( GRUB_MENU, "r" )))
		return errno == ENOENT ? BO_NOMAN : BO_IO;
	while ((len = fGets( line, sizeof(line), f )) != -1) {
		for (linp = line; isspace(*linp); linp++, len--);
		if ((ptr = match( linp, &len, "default", 7 )))
			*def = atoi( ptr );
		else if ((ptr = match( linp, &len, "title", 5 ))) {
			for (; isspace( ptr[len - 1] ); len--);
			*opts = addStrArr( *opts, ptr, len );
		}
	}
	fclose( f );

	return BO_OK;
}

static int
setGrub( const char *opt, SdRec *sdr )
{
	FILE *f;
	char *ptr;
	int len, i;
	char line[1000];

	if (!(f = fopen( GRUB_MENU, "r" )))
		return errno == ENOENT ? BO_NOMAN : BO_IO;
	for (i = 0; (len = fGets( line, sizeof(line), f )) != -1; )
		if ((ptr = match( line, &len, "title", 5 ))) {
			if (!strcmp( ptr, opt )) {
				fclose( f );
				sdr->osindex = i;
				sdr->bmstamp = mTime( GRUB_MENU );
				return BO_OK;
			}
			i++;
		}
	fclose( f );
	return BO_NOENT;
}

static void
commitGrub( void )
{
	char command[256];

	if (sdRec.bmstamp != mTime( GRUB_MENU ) &&
	    setGrub( sdRec.osname, &sdRec ) != BO_OK)
		return;

	sprintf(command, "%s %d", grub, sdRec.osindex);
	system(command);
}

static char *lilo;

static int
getLilo( char ***opts, int *def, int *cur )
{
	FILE *f;
	int cdef, pid, len, ret = BO_OK;
	static const char *args[5] = { 0, "-w", "-v", "-q", 0 };
	char buf[256], next[256];

	if (!lilo && !(lilo = locate( "lilo" )))
		return BO_NOMAN;

	args[0] = lilo;
	if (!(f = pOpen( (char **)args, 'r', &pid )))
		return BO_IO;
	*opts = 0;
	next[0] = 0;
	for (;;) {
		if ((len = fGets( buf, sizeof(buf), f)) == -1) {
			ret = BO_NOMAN;
			goto out;
		}
		if (!memcmp( buf, "Images:", 7 ))
			break;
#define Ldeflin "  Default boot command line:"
		if (!memcmp( buf, Ldeflin, strlen(Ldeflin) )) {
			memcpy( next, buf + strlen(Ldeflin) + 2, len - strlen(Ldeflin) - 3 );
			next[len - strlen(Ldeflin) - 3] = 0;
		}
	}
	cdef = *def = 0;
	*cur = -1;
	*opts = initStrArr( 0 );
	while ((len = fGets( buf, sizeof(buf), f)) != -1)
		if (buf[0] == ' ' && buf[1] == ' ' && buf[2] != ' ') {
			if (buf[len - 1] == '*') {
				*def = cdef;
				len--;
			}
			for (; buf[len - 1] == ' '; len--);
			*opts = addStrArr( *opts, buf + 2, len - 2 );
			if (!strcmp( (*opts)[cdef], next ))
				*cur = cdef;
			cdef++;
		}
  out:
	if (pClose( f, pid )) {
		if (*opts)
			freeStrArr( *opts );
		return BO_IO;
	}
	return ret;
}

static int
setLilo( const char *opt, SdRec *sdr ATTR_UNUSED )
{
	char **opts;
	int def, cur, ret, i;

	if ((ret = getLilo( &opts, &def, &cur )) != BO_OK)
		return ret;
	if (!*opt)
		opt = 0;
	else {
		for (i = 0; opts[i]; i++)
			if (!strcmp( opts[i], opt ))
				goto oke;
		freeStrArr( opts );
		return BO_NOENT;
	}
  oke:
	freeStrArr( opts );
	return BO_OK;
}

static void
commitLilo( void )
{
	static const char *args[5] = { 0, "-w", "-R", 0, 0 };

	args[0] = lilo;
	args[3] = sdRec.osname;
	runAndWait( (char **)args, environ );
}

static struct {
	int (*get)( char ***, int *, int * );
	int (*set)( const char *, SdRec * );
	void (*commit)( void );
} bootOpts[] = {
  { getNull, setNull, 0 },
  { getGrub, setGrub, commitGrub },
  { getLilo, setLilo, commitLilo },
};

int
getBootOptions( char ***opts, int *def, int *cur )
{
	return bootOpts[bootManager].get( opts, def, cur );
}

int
setBootOption( const char *opt, SdRec *sdr )
{
	int ret;

	if (sdr->osname) {
		free( sdr->osname );
		sdr->osname = 0;
	}
	if (opt) {
		if ((ret = bootOpts[bootManager].set( opt, sdr )) != BO_OK)
			return ret;
		if (!StrDup( &sdr->osname, opt ))
			return BO_IO; /* BO_NOMEM */
	}
	return BO_OK;
}

void
commitBootOption( void )
{
	if (sdRec.osname) {
		bootOpts[bootManager].commit();
/*
		free( sdRec.osname );
		sdRec.osname = 0;
*/
	}
}