master xplshn/aruu / cmd / linux / vtallow.c
 1/* See LICENSE file for copyright and license details. */
 2
 3#include <sys/ioctl.h>
 4#include <sys/stat.h>
 5#include <sys/types.h>
 6
 7#include <fcntl.h>
 8#include <stdio.h>
 9#include <string.h>
10#include <unistd.h>
11
12#include "paths.h"
13#include "util.h"
14
15#define CONSOLE ARUU_PATH_DEVCONSOLE
16
17#define VT_LOCKSWITCH   0x560B /* disallow vt switching */
18#define VT_UNLOCKSWITCH 0x560C /* allow vt switching */
19
20static void
21usage(void)
22{
23  eprintf("usage: %s n | y\n", argv0);
24}
25
26// ?man vtallow: allow non-root vt access
27// ?man arguments: n | y
28// ?man allow non-root users to access virtual terminal devices
29int
30main(int argc, char *argv[])
31{
32  int fd;
33  int allow;
34
35  ARGBEGIN
36  {
37    default:
38      usage();
39  }
40  ARGEND;
41
42  if (argc != 1)
43    usage();
44
45  if (!strcmp(argv[0], "y"))
46    allow = 1;
47  else if (!strcmp(argv[0], "n"))
48    allow = 0;
49  else
50    usage();
51
52  if ((fd = open(CONSOLE, O_WRONLY)) < 0)
53    eprintf("open %s:", CONSOLE);
54  if (ioctl(fd, allow ? VT_UNLOCKSWITCH : VT_LOCKSWITCH) < 0)
55    eprintf("cannot %s VT switch:", allow ? "unlock" : "lock");
56  close(fd);
57  return 0;
58}