2 * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 // 1) read and lseek on every file in /proc
18 // 2) readlink of every symlink in /proc
19 // 3) recursively (1) + (2) for every directory in /proc
20 // 4) write to /proc/*/clear_refs and /proc/*/task/*/clear_refs
21 // 5) write to /proc/sysrq-trigger
25 #include <sys/types.h>
38 static void f_reg(DIR *d
, const char *filename
)
44 /* read from /proc/kmsg can block */
45 fd
= openat(dirfd(d
), filename
, O_RDONLY
|O_NONBLOCK
);
48 /* struct proc_ops::proc_lseek is mandatory if file is seekable. */
49 (void)lseek(fd
, 0, SEEK_SET
);
50 rv
= read(fd
, buf
, sizeof(buf
));
51 assert((0 <= rv
&& rv
<= sizeof(buf
)) || rv
== -1);
55 static void f_reg_write(DIR *d
, const char *filename
, const char *buf
, size_t len
)
60 fd
= openat(dirfd(d
), filename
, O_WRONLY
);
63 rv
= write(fd
, buf
, len
);
64 assert((0 <= rv
&& rv
<= len
) || rv
== -1);
68 static void f_lnk(DIR *d
, const char *filename
)
73 rv
= readlinkat(dirfd(d
), filename
, buf
, sizeof(buf
));
74 assert((0 <= rv
&& rv
<= sizeof(buf
)) || rv
== -1);
77 static void f(DIR *d
, unsigned int level
)
82 assert(de
->d_type
== DT_DIR
);
83 assert(streq(de
->d_name
, "."));
86 assert(de
->d_type
== DT_DIR
);
87 assert(streq(de
->d_name
, ".."));
89 while ((de
= xreaddir(d
))) {
90 assert(!streq(de
->d_name
, "."));
91 assert(!streq(de
->d_name
, ".."));
98 if (level
== 0 && streq(de
->d_name
, "sysrq-trigger")) {
99 f_reg_write(d
, de
->d_name
, "h", 1);
100 } else if (level
== 1 && streq(de
->d_name
, "clear_refs")) {
101 f_reg_write(d
, de
->d_name
, "1", 1);
102 } else if (level
== 3 && streq(de
->d_name
, "clear_refs")) {
103 f_reg_write(d
, de
->d_name
, "1", 1);
105 f_reg(d
, de
->d_name
);
109 fd
= openat(dirfd(d
), de
->d_name
, O_DIRECTORY
|O_RDONLY
);
119 f_lnk(d
, de
->d_name
);
132 d
= opendir("/proc");
136 /* Ensure /proc is proc. */
137 if (fstatfs(dirfd(d
), &sfs
) == -1) {
140 if (sfs
.f_type
!= 0x9fa0) {
141 fprintf(stderr
, "error: unexpected f_type %lx\n", (long)sfs
.f_type
);