1 /* nosysrq.c -- SysRq protection plugin for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software which is freely distributable under the terms of the
6 * GNU General Public License version 2, included as the file COPYING in this
7 * distribution. It is NOT public domain software, and any
8 * redistribution not permitted by the GNU General Public License is
9 * expressly forbidden without prior written permission from
20 #include "vlock_plugin.h"
22 const char *preceeds
[] = { "new", "all", NULL
};
23 const char *depends
[] = { "all", NULL
};
25 #define SYSRQ_PATH "/proc/sys/kernel/sysrq"
26 #define SYSRQ_DISABLE_VALUE "0\n"
28 struct sysrq_context
{
33 /* Disable SysRq and save old value in context. */
34 bool vlock_start(void **ctx_ptr
)
36 struct sysrq_context
*ctx
;
38 /* Allocate the context. */
39 if ((ctx
= malloc(sizeof *ctx
)) == NULL
)
42 /* XXX: add optional PAM check here */
44 /* Open the SysRq sysctl file for reading and writing. */
45 if ((ctx
->file
= fopen(SYSRQ_PATH
, "r+")) == NULL
) {
46 perror("vlock-nosysrq: could not open '" SYSRQ_PATH
"'");
53 /* Read the old value. */
54 if (fgets(ctx
->value
, sizeof ctx
->value
, ctx
->file
) == NULL
) {
55 perror("vlock-nosysrq: could not read from '" SYSRQ_PATH
"'");
59 /* Check whether all data was read. */
60 if (feof(ctx
->file
) != 0) {
61 fprintf(stderr
, "vlock-nosysrq: sysrq buffer to small: %zu\n",
66 /* Check if SysRq was already disabled. */
67 if (strcmp(SYSRQ_DISABLE_VALUE
, ctx
->value
) == 0)
71 if (fseek(ctx
->file
, 0, SEEK_SET
) < 0
72 || ftruncate(fileno(ctx
->file
), 0) < 0
73 || fputs(SYSRQ_DISABLE_VALUE
, ctx
->file
) < 0
74 || fflush(ctx
->file
) < 0) {
75 perror("vlock-nosysrq: could not write disable value to '" SYSRQ_PATH
"'");
93 /* Restore old SysRq value. */
94 bool vlock_end(void **ctx_ptr
)
96 struct sysrq_context
*ctx
= *ctx_ptr
;
102 if (fseek(ctx
->file
, 0, SEEK_SET
) < 0
103 || ftruncate(fileno(ctx
->file
), 0) < 0
104 || fputs(ctx
->value
, ctx
->file
) < 0 || fflush(ctx
->file
) < 0)
105 perror("vlock-nosysrq: could not write old value to '" SYSRQ_PATH
"'");