1 /* vi: set sw=4 ts=4: */
2 /*
3  * Disallocate virtual terminal(s)
4  *
5  * Copyright (C) 2003 by Tito Ragusa <farmatito@tiscali.it>
6  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10 //config:config DEALLOCVT
11 //config:	bool "deallocvt (1.9 kb)"
12 //config:	default y
13 //config:	help
14 //config:	This program deallocates unused virtual consoles.
15 
16 //applet:IF_DEALLOCVT(APPLET_NOEXEC(deallocvt, deallocvt, BB_DIR_USR_BIN, BB_SUID_DROP, deallocvt))
17 
18 //kbuild:lib-$(CONFIG_DEALLOCVT) += deallocvt.o
19 
20 //usage:#define deallocvt_trivial_usage
21 //usage:       "[N]"
22 //usage:#define deallocvt_full_usage "\n\n"
23 //usage:       "Deallocate unused virtual terminal /dev/ttyN"
24 
25 #include "libbb.h"
26 
27 /* From <linux/vt.h> */
28 enum { VT_DISALLOCATE = 0x5608 }; /* free memory associated to vt */
29 
30 int deallocvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
deallocvt_main(int argc UNUSED_PARAM,char ** argv)31 int deallocvt_main(int argc UNUSED_PARAM, char **argv)
32 {
33 	/* num = 0 deallocate all unused consoles */
34 	int num = 0;
35 
36 	if (argv[1]) {
37 		if (argv[2])
38 			bb_show_usage();
39 		num = xatou_range(argv[1], 1, 63);
40 	}
41 
42 	/* double cast suppresses "cast to ptr from int of different size" */
43 	xioctl(get_console_fd_or_die(), VT_DISALLOCATE, (void *)(ptrdiff_t)num);
44 	return EXIT_SUCCESS;
45 }
46