2 * This file is part of stability-monitor
4 * Copyright © 2019 Samsung Electronics
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <sys/syscall.h>
24 #include <sys/types.h>
31 #define PROC_TSM_MODULE_NAME "proc_tsm"
32 #define PROC_TSM_PATH "/proc/tsm"
34 #define finit_module(fd, params, flags) syscall(__NR_finit_module, fd, params, flags)
37 static int str_to_ullarray(char *str, unsigned long long *array)
42 while (*ptr != '\n' && *ptr != 0)
43 if (n < MAX_PARAMS_PER_LINE) {
44 array[n++] = strtoull(ptr, &ptr, 10);
46 _W("Reached maximum number of parameters per line (%d), discarding others",
54 /* Read data prepared by kernel module */
55 static int get_raw_data(struct list_head *rd)
57 char buf[MAX_LINE] = {0,};
58 struct raw_data_item *rd_i;
62 fp = fopen(PROC_TSM_PATH, "r");
64 _E("Unable to open " PROC_TSM_PATH ": %m");
68 while (fgets(buf, MAX_LINE, fp) != NULL) {
69 ret = raw_data_new_item(rd, &rd_i);
73 ret = str_to_ullarray(buf, rd_i->v);
75 _E("Unable to parse raw data from kernel: \"%s\"\n", buf);
89 /* Load kernel module if not loaded */
94 if (access(PROC_TSM_PATH, O_RDONLY) == 0) {
95 _D("Kernel module already loaded\n");
99 fd = open(KMOD_PATH, O_RDONLY);
101 _E("Unable to open file descriptor");
105 ret = finit_module(fd, "", 0);
107 _E("Unable to load kernel module: %m");
112 static void cleanup(void)
117 struct raw_data_provider proc_tsm_module = {
118 .name = PROC_TSM_MODULE_NAME,
120 .get_raw_data = get_raw_data,
124 RAW_DATA_PROVIDER_REGISTER(&proc_tsm_module);