View | Details | Raw Unified | Return to bug 221935
Collapse All | Expand All

(-)swapon.c (-26 / +64 lines)
Lines 68-74 Link Here
68
static const char *swap_on_off(const char *, int, char *);
68
static const char *swap_on_off(const char *, int, char *);
69
static const char *swap_on_off_gbde(const char *, int);
69
static const char *swap_on_off_gbde(const char *, int);
70
static const char *swap_on_off_geli(const char *, char *, int);
70
static const char *swap_on_off_geli(const char *, char *, int);
71
static const char *swap_on_off_md(const char *, char *, int);
71
static const char *swap_on_off_md(const char *, char *, int, int,
72
    const char *);
72
static const char *swap_on_off_sfile(const char *, int);
73
static const char *swap_on_off_sfile(const char *, int);
73
static void swaplist(int, int, int);
74
static void swaplist(int, int, int);
74
static int run_cmd(int *, const char *, ...) __printflike(2, 3);
75
static int run_cmd(int *, const char *, ...) __printflike(2, 3);
Lines 217-264 Link Here
217
	exit(ret);
218
	exit(ret);
218
}
219
}
219
220
221
/* Strip off .bde or .eli suffix from swap device name */
222
static char *
223
swap_basename(const char *name)
224
{
225
	char *dname, *p;
226
227
	dname = strdup(name);
228
	p = strrchr(dname, '.');
229
	/* assert(p != NULL); */
230
	*p = '\0';
231
232
	return (dname);
233
}
234
220
static const char *
235
static const char *
221
swap_on_off(const char *name, int doingall, char *mntops)
236
swap_on_off(const char *name, int doingall, char *mntops)
222
{
237
{
223
	char base[PATH_MAX];
238
	char base[PATH_MAX];
239
	const char *ret;
240
	char *dname, *eliname;
241
	int is_eli = 0;
242
243
	basename_r(name, base);
244
245
	if (fnmatch("*.eli", base, 0) == 0) {
246
		is_eli = 1;
247
		dname = swap_basename(name);
248
	} else
249
		dname = strdup(name);
224
250
225
	/* Swap on vnode-backed md(4) device. */
251
	/* Swap on vnode-backed md(4) device. */
226
	if (mntops != NULL &&
252
	if (mntops != NULL &&
227
	    (fnmatch(_PATH_DEV MD_NAME "[0-9]*", name, 0) == 0 ||
253
	    (fnmatch(_PATH_DEV MD_NAME "[0-9]*", dname, 0) == 0 ||
228
	     fnmatch(MD_NAME "[0-9]*", name, 0) == 0 ||
254
	     fnmatch(MD_NAME "[0-9]*", dname, 0) == 0 ||
229
	     strncmp(_PATH_DEV MD_NAME, name,
255
	     strncmp(_PATH_DEV MD_NAME, dname,
230
		sizeof(_PATH_DEV) + sizeof(MD_NAME)) == 0 ||
256
		sizeof(_PATH_DEV) + sizeof(MD_NAME)) == 0 ||
231
	     strncmp(MD_NAME, name, sizeof(MD_NAME)) == 0))
257
	     strncmp(MD_NAME, dname, sizeof(MD_NAME)) == 0)) {
232
		return (swap_on_off_md(name, mntops, doingall));
258
		if (is_eli && which_prog == SWAPON) {
259
			ret = swap_on_off_md(dname, mntops, doingall,
260
				0, "");
261
			free(dname);
262
			if (ret == NULL)
263
				return (NULL);
264
			asprintf(&eliname, "%s.eli", ret);
265
			ret = swap_on_off_geli(eliname, mntops, doingall);
266
			free(eliname);
267
			return (ret);
268
		} else if (is_eli /* SWAPOFF */) {
269
			ret = swap_on_off_md(dname, mntops, doingall,
270
				1, ".eli");
271
			free(dname);
272
			return (ret);
273
		} else {
274
			free(dname);
275
			return (swap_on_off_md(name, mntops, doingall,
276
				1, ""));
277
		}
278
	}
233
279
234
	basename_r(name, base);
280
	free(dname);
235
281
236
	/* Swap on encrypted device by GEOM_BDE. */
282
	/* Swap on encrypted device by GEOM_BDE. */
237
	if (fnmatch("*.bde", base, 0) == 0)
283
	if (fnmatch("*.bde", base, 0) == 0)
238
		return (swap_on_off_gbde(name, doingall));
284
		return (swap_on_off_gbde(name, doingall));
239
285
240
	/* Swap on encrypted device by GEOM_ELI. */
286
	/* Swap on encrypted device by GEOM_ELI. */
241
	if (fnmatch("*.eli", base, 0) == 0)
287
	if (is_eli)
242
		return (swap_on_off_geli(name, mntops, doingall));
288
		return (swap_on_off_geli(name, mntops, doingall));
243
289
244
	/* Swap on special file. */
290
	/* Swap on special file. */
245
	return (swap_on_off_sfile(name, doingall));
291
	return (swap_on_off_sfile(name, doingall));
246
}
292
}
247
293
248
/* Strip off .bde or .eli suffix from swap device name */
249
static char *
250
swap_basename(const char *name)
251
{
252
	char *dname, *p;
253
254
	dname = strdup(name);
255
	p = strrchr(dname, '.');
256
	/* assert(p != NULL); */
257
	*p = '\0';
258
259
	return (dname);
260
}
261
262
static const char *
294
static const char *
263
swap_on_off_gbde(const char *name, int doingall)
295
swap_on_off_gbde(const char *name, int doingall)
264
{
296
{
Lines 373-378 Link Here
373
				/* ignore known option */
405
				/* ignore known option */
374
			} else if (strcmp(token, "noauto") == 0) {
406
			} else if (strcmp(token, "noauto") == 0) {
375
				/* ignore known option */
407
				/* ignore known option */
408
			} else if ((p = strstr(token, "file=")) == token) {
409
				/* ignore known option */
376
			} else if (strcmp(token, "sw") != 0) {
410
			} else if (strcmp(token, "sw") != 0) {
377
				warnx("Invalid option: %s", token);
411
				warnx("Invalid option: %s", token);
378
				free(ops);
412
				free(ops);
Lines 445-451 Link Here
445
}
479
}
446
480
447
static const char *
481
static const char *
448
swap_on_off_md(const char *name, char *mntops, int doingall)
482
swap_on_off_md(const char *name, char *mntops, int doingall,
483
    int do_sfile, const char *suffix)
449
{
484
{
450
	FILE *sfd;
485
	FILE *sfd;
451
	int fd, mdunit, error;
486
	int fd, mdunit, error;
Lines 613-622 Link Here
613
			}
648
			}
614
		}
649
		}
615
	}
650
	}
616
	snprintf(mdpath, sizeof(mdpath), "%s%s%d", _PATH_DEV,
651
	snprintf(mdpath, sizeof(mdpath), "%s%s%d%s", _PATH_DEV,
617
	    MD_NAME, mdunit);
652
	    MD_NAME, mdunit, suffix);
618
	mdpath[sizeof(mdpath) - 1] = '\0';
653
	mdpath[sizeof(mdpath) - 1] = '\0';
619
	ret = swap_on_off_sfile(mdpath, doingall);
654
	if (do_sfile)
655
	  ret = swap_on_off_sfile(mdpath, doingall);
656
	else
657
	  ret = mdpath;
620
658
621
	if (which_prog == SWAPOFF) {
659
	if (which_prog == SWAPOFF) {
622
		if (ret != NULL) {
660
		if (ret != NULL) {

Return to bug 221935