summaryrefslogtreecommitdiff
path: root/lib/active.c
blob: 232f90ae64d1fefe7745bd412cce6a50b4fa0bd6 (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#include <error.h>
#include <errno.h>
#include <string.h>
#include "initd.h"
#include "str.h"
#include "rcint.h"

static bool active_verbose = false;
static bool read_dir_symlinks(initd_list_t *ilp, const struct rcpair *rcp);
static char *rel_readlink(const char *path);
static void set_active_from_symlink(initd_list_t *ilp,
					const struct rcpair *rcp,
					const char *link, const char *tgt);
static void add_active_link(const initd_t *ip, const char *link,
			const struct rcpair *rcp, initd_key_t key);

void initd_list_set_actives(initd_list_t *ilp, const char *dir)
{
	char *cwd;
	struct rcpair *cur;

	if (!ilp || !dir)
		return;

	/* Change to the init.d directory */
	cwd = get_current_dir_name();
	if (chdir(dir) != 0)
		error(1, errno, "%s", dir);

	/* Check the various sysinit directories, stopping after the
	 * first one. */
	for (cur = rcsdirs; cur->dir; cur++) {
		/* if a sysinit dir was found, leave the loop */
		if (read_dir_symlinks(ilp, cur))
			break;
	}

	/* Check the all the numbered init directories */
	for (cur = rcdirs; cur->dir; cur++)
		read_dir_symlinks(ilp, cur);

	/* Return to the original directory */
	if (chdir(cwd) != 0)
		error(1, errno, "%s", cwd);
	free(cwd);
}

bool initd_is_active(const initd_t *ip, initd_rc_t rc, initd_key_t key)
{
	initd_rc_t match;

	if (!ip)
		return false;

	switch (key) {
	case KEY_ASTART:
		match = ip->astart;
		break;
	case KEY_ASTOP:
		match = ip->astop;
		break;
	default:
		/* Wrong key type */
		return false;
	}

	if (match & rc)
		return true;
	else
		return false;
}

static bool read_dir_symlinks(initd_list_t *ilp, const struct rcpair *rcp)
{
	char *dir, *link;
	DIR *dfd;
	struct dirent *de;

	if (!ilp || !rcp)
		return false;

	dir = rcp->dir;
	if (!dir)
		return false;

	if (active_verbose)
		printf("Changing to %s\n", dir);

	if (chdir(dir) != 0) {
		if (errno == ENOENT) {
			/* zero errno for later calls */
			errno = 0;
			return false;
		} else {
			error(1, errno, "%s", dir);
		}
	}

	dfd = opendir(".");
	if (!dfd)
		error(1, errno, "%s", dir);

	while ((de = readdir(dfd))) {
		if (!de || !de->d_name)
			continue;

		link = rel_readlink(de->d_name);
		if (!link)
			continue;

		if (active_verbose)
			printf("%s -> %s\n", de->d_name, link);

		set_active_from_symlink(ilp, rcp, de->d_name, link);
	}

	/* if errno is set, readdir had issues */
	if (errno)
		error(1, errno, "%s", dir);

	if (closedir(dfd) != 0)
		error(1, errno, "%s", dir);

	return true;
}

static char *rel_readlink(const char *path)
{
	struct stat ls;
	static char target[PATH_MAX];
	ssize_t tlen;

	if (lstat(path, &ls) != 0)
		error(1, errno, "%s", path);

	if (!S_ISLNK(ls.st_mode)) {
		if (active_verbose) {
			fprintf(stderr, "%s: Not a symlink, skipping\n",
				path);
		}
		return NULL;
	}

	/* Null all the bytes in target */
	memset(target, '\0', sizeof(target));

	tlen = readlink(path, target, sizeof(target));
	if (tlen < 0)
		error(1, errno, "%s", path);

	/* append a null byte if target was truncated */
	if (tlen == PATH_MAX)
		target[PATH_MAX - 1] = '\0';

	if (strncmp(target, "../init.d/", strlen("../init.d/")) != 0) {
		if (active_verbose) {
			fprintf(stderr,
				"%s -> %s: Not an init.d symlink, skipping\n",
				path, target);
		}
		return NULL;
	}

	return d_string_new(target);
}

static void set_active_from_symlink(initd_list_t *ilp,
					const struct rcpair *rcp,
					const char *link, const char *tgt)
{
	const char *tbase;
	size_t idlen;
	initd_t *ip;
	initd_key_t key;

	if (!(ilp || rcp || link || tgt))
		return;

	/* strip off the leading ../init.d/ in the target */
	idlen = strlen("../init.d/");
	if (strlen(tgt) <= idlen)
		return;
	tbase = tgt + idlen;

	/* see if this name exists in the initd pool */
	ip = initd_list_find_name(ilp, tbase);
	if (!ip) {
		if (active_verbose) {
			fprintf(stderr,
				"%s does not exist in initd list\n",
				tbase);
		}
		return;
	}

	/* is this a start (S) or stop (K) link? */
	if (link[0] == 'S') {
		key = KEY_ASTART;
	} else if (link[0] == 'K') {
		key = KEY_ASTOP;
	} else {
		fprintf(stderr, "link %s is not a valid S or K link\n",
			link);
		return;
	}

	/* set this level as active in the initd_t */
	initd_set_rc(ip, key, rcp->rc);

	/* add the link to the active list */
	add_active_link(ip, link, rcp, key);
}

static void add_active_link(const initd_t *ip, const char *link,
			const struct rcpair *rcp, initd_key_t key)
{
	char *fullpath;
	size_t pathlen;

	if (!(ip || link || rcp))
		return;

	/* Construct link path relative to init.d directory. This is
	 * rcp->dir + / + link */
	pathlen = strlen(rcp->dir) + strlen(link) + 2;
	fullpath = malloc(sizeof(char) * pathlen);
	if (!fullpath)
		error(2, errno, "malloc");
	if (snprintf(fullpath, pathlen, "%s/%s", rcp->dir, link) >= pathlen)
		error(2, errno, "snprintf");

	switch (key) {
	case KEY_ASTART:
		if (!strarg_exists(ip->astart_links, fullpath))
			initd_add_astart_links(ip, fullpath);
		break;
	case KEY_ASTOP:
		if (!strarg_exists(ip->astop_links, fullpath))
			initd_add_astop_links(ip, fullpath);
		break;
	default:
		free(fullpath);
	}
}