xref: /openbmc/linux/tools/thermal/thermometer/thermometer.c (revision 110acbc6a4518145db3a1a9c0686d730bb258bf1)
1*110acbc6SDaniel Lezcano // SPDX-License-Identifier: GPL-2.0-only
2*110acbc6SDaniel Lezcano // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
3*110acbc6SDaniel Lezcano #define _GNU_SOURCE
4*110acbc6SDaniel Lezcano #include <dirent.h>
5*110acbc6SDaniel Lezcano #include <fcntl.h>
6*110acbc6SDaniel Lezcano #include <getopt.h>
7*110acbc6SDaniel Lezcano #include <regex.h>
8*110acbc6SDaniel Lezcano #include <signal.h>
9*110acbc6SDaniel Lezcano #include <stdio.h>
10*110acbc6SDaniel Lezcano #include <stdlib.h>
11*110acbc6SDaniel Lezcano #include <string.h>
12*110acbc6SDaniel Lezcano #include <sys/stat.h>
13*110acbc6SDaniel Lezcano #include <sys/signalfd.h>
14*110acbc6SDaniel Lezcano #include <sys/timerfd.h>
15*110acbc6SDaniel Lezcano #include <sys/types.h>
16*110acbc6SDaniel Lezcano #include <sys/wait.h>
17*110acbc6SDaniel Lezcano #include <time.h>
18*110acbc6SDaniel Lezcano #include <unistd.h>
19*110acbc6SDaniel Lezcano #include <linux/thermal.h>
20*110acbc6SDaniel Lezcano 
21*110acbc6SDaniel Lezcano #include <libconfig.h>
22*110acbc6SDaniel Lezcano #include "thermal-tools.h"
23*110acbc6SDaniel Lezcano 
24*110acbc6SDaniel Lezcano #define CLASS_THERMAL "/sys/class/thermal"
25*110acbc6SDaniel Lezcano 
26*110acbc6SDaniel Lezcano enum {
27*110acbc6SDaniel Lezcano 	THERMOMETER_SUCCESS = 0,
28*110acbc6SDaniel Lezcano 	THERMOMETER_OPTION_ERROR,
29*110acbc6SDaniel Lezcano 	THERMOMETER_LOG_ERROR,
30*110acbc6SDaniel Lezcano 	THERMOMETER_CONFIG_ERROR,
31*110acbc6SDaniel Lezcano 	THERMOMETER_TIME_ERROR,
32*110acbc6SDaniel Lezcano 	THERMOMETER_INIT_ERROR,
33*110acbc6SDaniel Lezcano 	THERMOMETER_RUNTIME_ERROR
34*110acbc6SDaniel Lezcano };
35*110acbc6SDaniel Lezcano 
36*110acbc6SDaniel Lezcano struct options {
37*110acbc6SDaniel Lezcano 	int loglvl;
38*110acbc6SDaniel Lezcano 	int logopt;
39*110acbc6SDaniel Lezcano 	int overwrite;
40*110acbc6SDaniel Lezcano 	int duration;
41*110acbc6SDaniel Lezcano 	const char *config;
42*110acbc6SDaniel Lezcano 	char postfix[PATH_MAX];
43*110acbc6SDaniel Lezcano 	char output[PATH_MAX];
44*110acbc6SDaniel Lezcano };
45*110acbc6SDaniel Lezcano 
46*110acbc6SDaniel Lezcano struct tz_regex {
47*110acbc6SDaniel Lezcano 	regex_t regex;
48*110acbc6SDaniel Lezcano 	int polling;
49*110acbc6SDaniel Lezcano };
50*110acbc6SDaniel Lezcano 
51*110acbc6SDaniel Lezcano struct configuration {
52*110acbc6SDaniel Lezcano 	struct tz_regex *tz_regex;
53*110acbc6SDaniel Lezcano 	int nr_tz_regex;
54*110acbc6SDaniel Lezcano 
55*110acbc6SDaniel Lezcano };
56*110acbc6SDaniel Lezcano 
57*110acbc6SDaniel Lezcano struct tz {
58*110acbc6SDaniel Lezcano 	FILE *file_out;
59*110acbc6SDaniel Lezcano 	int fd_temp;
60*110acbc6SDaniel Lezcano 	int fd_timer;
61*110acbc6SDaniel Lezcano 	int polling;
62*110acbc6SDaniel Lezcano 	const char *name;
63*110acbc6SDaniel Lezcano };
64*110acbc6SDaniel Lezcano 
65*110acbc6SDaniel Lezcano struct thermometer {
66*110acbc6SDaniel Lezcano 	struct tz *tz;
67*110acbc6SDaniel Lezcano 	int nr_tz;
68*110acbc6SDaniel Lezcano };
69*110acbc6SDaniel Lezcano 
70*110acbc6SDaniel Lezcano static struct tz_regex *configuration_tz_match(const char *expr,
71*110acbc6SDaniel Lezcano 					       struct configuration *config)
72*110acbc6SDaniel Lezcano {
73*110acbc6SDaniel Lezcano 	int i;
74*110acbc6SDaniel Lezcano 
75*110acbc6SDaniel Lezcano 	for (i = 0; i < config->nr_tz_regex; i++) {
76*110acbc6SDaniel Lezcano 
77*110acbc6SDaniel Lezcano 		if (!regexec(&config->tz_regex[i].regex, expr, 0, NULL, 0))
78*110acbc6SDaniel Lezcano 			return &config->tz_regex[i];
79*110acbc6SDaniel Lezcano 	}
80*110acbc6SDaniel Lezcano 
81*110acbc6SDaniel Lezcano 	return NULL;
82*110acbc6SDaniel Lezcano }
83*110acbc6SDaniel Lezcano 
84*110acbc6SDaniel Lezcano static int configuration_default_init(struct configuration *config)
85*110acbc6SDaniel Lezcano {
86*110acbc6SDaniel Lezcano 	config->tz_regex = realloc(config->tz_regex, sizeof(*config->tz_regex) *
87*110acbc6SDaniel Lezcano 				   (config->nr_tz_regex + 1));
88*110acbc6SDaniel Lezcano 
89*110acbc6SDaniel Lezcano 	if (regcomp(&config->tz_regex[config->nr_tz_regex].regex, ".*",
90*110acbc6SDaniel Lezcano 		    REG_NOSUB | REG_EXTENDED)) {
91*110acbc6SDaniel Lezcano 		ERROR("Invalid regular expression\n");
92*110acbc6SDaniel Lezcano 		return -1;
93*110acbc6SDaniel Lezcano 	}
94*110acbc6SDaniel Lezcano 
95*110acbc6SDaniel Lezcano 	config->tz_regex[config->nr_tz_regex].polling = 250;
96*110acbc6SDaniel Lezcano 	config->nr_tz_regex = 1;
97*110acbc6SDaniel Lezcano 
98*110acbc6SDaniel Lezcano 	return 0;
99*110acbc6SDaniel Lezcano }
100*110acbc6SDaniel Lezcano 
101*110acbc6SDaniel Lezcano static int configuration_init(const char *path, struct configuration *config)
102*110acbc6SDaniel Lezcano {
103*110acbc6SDaniel Lezcano 	config_t cfg;
104*110acbc6SDaniel Lezcano 
105*110acbc6SDaniel Lezcano 	config_setting_t *tz;
106*110acbc6SDaniel Lezcano 	int i, length;
107*110acbc6SDaniel Lezcano 
108*110acbc6SDaniel Lezcano 	if (path && access(path, F_OK)) {
109*110acbc6SDaniel Lezcano 		ERROR("'%s' is not accessible\n", path);
110*110acbc6SDaniel Lezcano 		return -1;
111*110acbc6SDaniel Lezcano 	}
112*110acbc6SDaniel Lezcano 
113*110acbc6SDaniel Lezcano 	if (!path && !config->nr_tz_regex) {
114*110acbc6SDaniel Lezcano 		INFO("No thermal zones configured, using wildcard for all of them\n");
115*110acbc6SDaniel Lezcano 		return configuration_default_init(config);
116*110acbc6SDaniel Lezcano 	}
117*110acbc6SDaniel Lezcano 
118*110acbc6SDaniel Lezcano 	config_init(&cfg);
119*110acbc6SDaniel Lezcano 
120*110acbc6SDaniel Lezcano 	if (!config_read_file(&cfg, path)) {
121*110acbc6SDaniel Lezcano 		ERROR("Failed to parse %s:%d - %s\n", config_error_file(&cfg),
122*110acbc6SDaniel Lezcano 		      config_error_line(&cfg), config_error_text(&cfg));
123*110acbc6SDaniel Lezcano 
124*110acbc6SDaniel Lezcano 		return -1;
125*110acbc6SDaniel Lezcano 	}
126*110acbc6SDaniel Lezcano 
127*110acbc6SDaniel Lezcano 	tz = config_lookup(&cfg, "thermal-zones");
128*110acbc6SDaniel Lezcano 	if (!tz) {
129*110acbc6SDaniel Lezcano 		ERROR("No thermal zone configured to be monitored\n");
130*110acbc6SDaniel Lezcano 		return -1;
131*110acbc6SDaniel Lezcano 	}
132*110acbc6SDaniel Lezcano 
133*110acbc6SDaniel Lezcano 	length = config_setting_length(tz);
134*110acbc6SDaniel Lezcano 
135*110acbc6SDaniel Lezcano 	INFO("Found %d thermal zone(s) regular expression\n", length);
136*110acbc6SDaniel Lezcano 
137*110acbc6SDaniel Lezcano 	for (i = 0; i < length; i++) {
138*110acbc6SDaniel Lezcano 
139*110acbc6SDaniel Lezcano 		config_setting_t *node;
140*110acbc6SDaniel Lezcano 		const char *name;
141*110acbc6SDaniel Lezcano 		int polling;
142*110acbc6SDaniel Lezcano 
143*110acbc6SDaniel Lezcano 		node = config_setting_get_elem(tz, i);
144*110acbc6SDaniel Lezcano 		if (!node) {
145*110acbc6SDaniel Lezcano 			ERROR("Missing node name '%d'\n", i);
146*110acbc6SDaniel Lezcano 			return -1;
147*110acbc6SDaniel Lezcano 		};
148*110acbc6SDaniel Lezcano 
149*110acbc6SDaniel Lezcano 		if (!config_setting_lookup_string(node, "name", &name)) {
150*110acbc6SDaniel Lezcano 			ERROR("Thermal zone name not found\n");
151*110acbc6SDaniel Lezcano 			return -1;
152*110acbc6SDaniel Lezcano 		}
153*110acbc6SDaniel Lezcano 
154*110acbc6SDaniel Lezcano 		if (!config_setting_lookup_int(node, "polling", &polling)) {
155*110acbc6SDaniel Lezcano 			ERROR("Polling value not found");
156*110acbc6SDaniel Lezcano 			return -1;
157*110acbc6SDaniel Lezcano 		}
158*110acbc6SDaniel Lezcano 
159*110acbc6SDaniel Lezcano 		config->tz_regex = realloc(config->tz_regex, sizeof(*config->tz_regex) *
160*110acbc6SDaniel Lezcano 					(config->nr_tz_regex + 1));
161*110acbc6SDaniel Lezcano 
162*110acbc6SDaniel Lezcano 		if (regcomp(&config->tz_regex[config->nr_tz_regex].regex, name,
163*110acbc6SDaniel Lezcano 			    REG_NOSUB | REG_EXTENDED)) {
164*110acbc6SDaniel Lezcano 			ERROR("Invalid regular expression '%s'\n", name);
165*110acbc6SDaniel Lezcano 			continue;
166*110acbc6SDaniel Lezcano 		}
167*110acbc6SDaniel Lezcano 
168*110acbc6SDaniel Lezcano 		config->tz_regex[config->nr_tz_regex].polling = polling;
169*110acbc6SDaniel Lezcano 		config->nr_tz_regex++;
170*110acbc6SDaniel Lezcano 
171*110acbc6SDaniel Lezcano 		INFO("Thermal zone regular expression '%s' with polling %d\n",
172*110acbc6SDaniel Lezcano 		     name, polling);
173*110acbc6SDaniel Lezcano 	}
174*110acbc6SDaniel Lezcano 
175*110acbc6SDaniel Lezcano 	return 0;
176*110acbc6SDaniel Lezcano }
177*110acbc6SDaniel Lezcano 
178*110acbc6SDaniel Lezcano static void usage(const char *cmd)
179*110acbc6SDaniel Lezcano {
180*110acbc6SDaniel Lezcano 	printf("%s Version: %s\n", cmd, VERSION);
181*110acbc6SDaniel Lezcano 	printf("Usage: %s [options]\n", cmd);
182*110acbc6SDaniel Lezcano 	printf("\t-h, --help\t\tthis help\n");
183*110acbc6SDaniel Lezcano 	printf("\t-o, --output <dir>\toutput directory for temperature capture\n");
184*110acbc6SDaniel Lezcano 	printf("\t-c, --config <file>\tconfiguration file\n");
185*110acbc6SDaniel Lezcano 	printf("\t-d, --duration <seconds>\tcapture duration\n");
186*110acbc6SDaniel Lezcano 	printf("\t-l, --loglevel <level>\tlog level: ");
187*110acbc6SDaniel Lezcano 	printf("DEBUG, INFO, NOTICE, WARN, ERROR\n");
188*110acbc6SDaniel Lezcano 	printf("\t-p, --postfix <string>\tpostfix to be happened at the end of the files\n");
189*110acbc6SDaniel Lezcano 	printf("\t-s, --syslog\t\toutput to syslog\n");
190*110acbc6SDaniel Lezcano 	printf("\t-w, --overwrite\t\toverwrite the temperature capture files if they exist\n");
191*110acbc6SDaniel Lezcano 	printf("\n");
192*110acbc6SDaniel Lezcano 	exit(0);
193*110acbc6SDaniel Lezcano }
194*110acbc6SDaniel Lezcano 
195*110acbc6SDaniel Lezcano static int options_init(int argc, char *argv[], struct options *options)
196*110acbc6SDaniel Lezcano {
197*110acbc6SDaniel Lezcano 	int opt;
198*110acbc6SDaniel Lezcano 	time_t now = time(NULL);
199*110acbc6SDaniel Lezcano 
200*110acbc6SDaniel Lezcano 	struct option long_options[] = {
201*110acbc6SDaniel Lezcano 		{ "help",	no_argument, NULL, 'h' },
202*110acbc6SDaniel Lezcano 		{ "config",	required_argument, NULL, 'c' },
203*110acbc6SDaniel Lezcano 		{ "duration",	required_argument, NULL, 'd' },
204*110acbc6SDaniel Lezcano 		{ "loglevel",	required_argument, NULL, 'l' },
205*110acbc6SDaniel Lezcano 		{ "postfix",	required_argument, NULL, 'p' },
206*110acbc6SDaniel Lezcano 		{ "output",	required_argument, NULL, 'o' },
207*110acbc6SDaniel Lezcano 		{ "syslog",	required_argument, NULL, 's' },
208*110acbc6SDaniel Lezcano 		{ "overwrite",	no_argument, NULL, 'w' },
209*110acbc6SDaniel Lezcano 		{ 0, 0, 0, 0 }
210*110acbc6SDaniel Lezcano 	};
211*110acbc6SDaniel Lezcano 
212*110acbc6SDaniel Lezcano 	strftime(options->postfix, sizeof(options->postfix),
213*110acbc6SDaniel Lezcano 		 "-%Y-%m-%d_%H:%M:%S", gmtime(&now));
214*110acbc6SDaniel Lezcano 
215*110acbc6SDaniel Lezcano 	while (1) {
216*110acbc6SDaniel Lezcano 
217*110acbc6SDaniel Lezcano 		int optindex = 0;
218*110acbc6SDaniel Lezcano 
219*110acbc6SDaniel Lezcano 		opt = getopt_long(argc, argv, "ho:c:d:l:p:sw", long_options, &optindex);
220*110acbc6SDaniel Lezcano 		if (opt == -1)
221*110acbc6SDaniel Lezcano 			break;
222*110acbc6SDaniel Lezcano 
223*110acbc6SDaniel Lezcano 		switch (opt) {
224*110acbc6SDaniel Lezcano 		case 'c':
225*110acbc6SDaniel Lezcano 			options->config = optarg;
226*110acbc6SDaniel Lezcano 			break;
227*110acbc6SDaniel Lezcano 		case 'd':
228*110acbc6SDaniel Lezcano 			options->duration = atoi(optarg) * 1000;
229*110acbc6SDaniel Lezcano 			break;
230*110acbc6SDaniel Lezcano 		case 'l':
231*110acbc6SDaniel Lezcano 			options->loglvl = log_str2level(optarg);
232*110acbc6SDaniel Lezcano 			break;
233*110acbc6SDaniel Lezcano 		case 'h':
234*110acbc6SDaniel Lezcano 			usage(basename(argv[0]));
235*110acbc6SDaniel Lezcano 			break;
236*110acbc6SDaniel Lezcano 		case 'p':
237*110acbc6SDaniel Lezcano 			strcpy(options->postfix, optarg);
238*110acbc6SDaniel Lezcano 			break;
239*110acbc6SDaniel Lezcano 		case 'o':
240*110acbc6SDaniel Lezcano 			strcpy(options->output, optarg);
241*110acbc6SDaniel Lezcano 			break;
242*110acbc6SDaniel Lezcano 		case 's':
243*110acbc6SDaniel Lezcano 			options->logopt = TO_SYSLOG;
244*110acbc6SDaniel Lezcano 			break;
245*110acbc6SDaniel Lezcano 		case 'w':
246*110acbc6SDaniel Lezcano 			options->overwrite = 1;
247*110acbc6SDaniel Lezcano 			break;
248*110acbc6SDaniel Lezcano 		default: /* '?' */
249*110acbc6SDaniel Lezcano 			ERROR("Usage: %s --help\n", argv[0]);
250*110acbc6SDaniel Lezcano 			return -1;
251*110acbc6SDaniel Lezcano 		}
252*110acbc6SDaniel Lezcano 	}
253*110acbc6SDaniel Lezcano 
254*110acbc6SDaniel Lezcano 	return 0;
255*110acbc6SDaniel Lezcano }
256*110acbc6SDaniel Lezcano 
257*110acbc6SDaniel Lezcano static int thermometer_add_tz(const char *path, const char *name, int polling,
258*110acbc6SDaniel Lezcano 			      struct thermometer *thermometer)
259*110acbc6SDaniel Lezcano {
260*110acbc6SDaniel Lezcano 	int fd;
261*110acbc6SDaniel Lezcano 	char tz_path[PATH_MAX];
262*110acbc6SDaniel Lezcano 
263*110acbc6SDaniel Lezcano 	sprintf(tz_path, CLASS_THERMAL"/%s/temp", path);
264*110acbc6SDaniel Lezcano 
265*110acbc6SDaniel Lezcano 	fd = open(tz_path, O_RDONLY);
266*110acbc6SDaniel Lezcano 	if (fd < 0) {
267*110acbc6SDaniel Lezcano 		ERROR("Failed to open '%s': %m\n", tz_path);
268*110acbc6SDaniel Lezcano 		return -1;
269*110acbc6SDaniel Lezcano 	}
270*110acbc6SDaniel Lezcano 
271*110acbc6SDaniel Lezcano 	thermometer->tz = realloc(thermometer->tz,
272*110acbc6SDaniel Lezcano 				  sizeof(*thermometer->tz) * (thermometer->nr_tz + 1));
273*110acbc6SDaniel Lezcano 	if (!thermometer->tz) {
274*110acbc6SDaniel Lezcano 		ERROR("Failed to allocate thermometer->tz\n");
275*110acbc6SDaniel Lezcano 		return -1;
276*110acbc6SDaniel Lezcano 	}
277*110acbc6SDaniel Lezcano 
278*110acbc6SDaniel Lezcano 	thermometer->tz[thermometer->nr_tz].fd_temp = fd;
279*110acbc6SDaniel Lezcano 	thermometer->tz[thermometer->nr_tz].name = strdup(name);
280*110acbc6SDaniel Lezcano 	thermometer->tz[thermometer->nr_tz].polling = polling;
281*110acbc6SDaniel Lezcano 	thermometer->nr_tz++;
282*110acbc6SDaniel Lezcano 
283*110acbc6SDaniel Lezcano 	INFO("Added thermal zone '%s->%s (polling:%d)'\n", path, name, polling);
284*110acbc6SDaniel Lezcano 
285*110acbc6SDaniel Lezcano 	return 0;
286*110acbc6SDaniel Lezcano }
287*110acbc6SDaniel Lezcano 
288*110acbc6SDaniel Lezcano static int thermometer_init(struct configuration *config,
289*110acbc6SDaniel Lezcano 			    struct thermometer *thermometer)
290*110acbc6SDaniel Lezcano {
291*110acbc6SDaniel Lezcano 	DIR *dir;
292*110acbc6SDaniel Lezcano 	struct dirent *dirent;
293*110acbc6SDaniel Lezcano 	struct tz_regex *tz_regex;
294*110acbc6SDaniel Lezcano 	const char *tz_dirname = "thermal_zone";
295*110acbc6SDaniel Lezcano 
296*110acbc6SDaniel Lezcano 	if (mainloop_init()) {
297*110acbc6SDaniel Lezcano 		ERROR("Failed to start mainloop\n");
298*110acbc6SDaniel Lezcano 		return -1;
299*110acbc6SDaniel Lezcano 	}
300*110acbc6SDaniel Lezcano 
301*110acbc6SDaniel Lezcano 	dir = opendir(CLASS_THERMAL);
302*110acbc6SDaniel Lezcano 	if (!dir) {
303*110acbc6SDaniel Lezcano 		ERROR("failed to open '%s'\n", CLASS_THERMAL);
304*110acbc6SDaniel Lezcano 		return -1;
305*110acbc6SDaniel Lezcano 	}
306*110acbc6SDaniel Lezcano 
307*110acbc6SDaniel Lezcano 	while ((dirent = readdir(dir))) {
308*110acbc6SDaniel Lezcano 		char tz_type[THERMAL_NAME_LENGTH];
309*110acbc6SDaniel Lezcano 		char tz_path[PATH_MAX];
310*110acbc6SDaniel Lezcano 		FILE *tz_file;
311*110acbc6SDaniel Lezcano 
312*110acbc6SDaniel Lezcano 		if (strncmp(dirent->d_name, tz_dirname, strlen(tz_dirname)))
313*110acbc6SDaniel Lezcano 			continue;
314*110acbc6SDaniel Lezcano 
315*110acbc6SDaniel Lezcano 		sprintf(tz_path, CLASS_THERMAL"/%s/type", dirent->d_name);
316*110acbc6SDaniel Lezcano 
317*110acbc6SDaniel Lezcano 		tz_file = fopen(tz_path, "r");
318*110acbc6SDaniel Lezcano 		if (!tz_file) {
319*110acbc6SDaniel Lezcano 			ERROR("Failed to open '%s': %m", tz_path);
320*110acbc6SDaniel Lezcano 			continue;
321*110acbc6SDaniel Lezcano 		}
322*110acbc6SDaniel Lezcano 
323*110acbc6SDaniel Lezcano 		fscanf(tz_file, "%s", tz_type);
324*110acbc6SDaniel Lezcano 
325*110acbc6SDaniel Lezcano 		fclose(tz_file);
326*110acbc6SDaniel Lezcano 
327*110acbc6SDaniel Lezcano 		tz_regex = configuration_tz_match(tz_type, config);
328*110acbc6SDaniel Lezcano 		if (!tz_regex)
329*110acbc6SDaniel Lezcano 			continue;
330*110acbc6SDaniel Lezcano 
331*110acbc6SDaniel Lezcano 		if (thermometer_add_tz(dirent->d_name, tz_type,
332*110acbc6SDaniel Lezcano 				       tz_regex->polling, thermometer))
333*110acbc6SDaniel Lezcano 			continue;
334*110acbc6SDaniel Lezcano 	}
335*110acbc6SDaniel Lezcano 
336*110acbc6SDaniel Lezcano 	closedir(dir);
337*110acbc6SDaniel Lezcano 
338*110acbc6SDaniel Lezcano 	return 0;
339*110acbc6SDaniel Lezcano }
340*110acbc6SDaniel Lezcano 
341*110acbc6SDaniel Lezcano static int timer_temperature_callback(int fd, void *arg)
342*110acbc6SDaniel Lezcano {
343*110acbc6SDaniel Lezcano 	struct tz *tz = arg;
344*110acbc6SDaniel Lezcano 	char buf[16] = { 0 };
345*110acbc6SDaniel Lezcano 
346*110acbc6SDaniel Lezcano 	pread(tz->fd_temp, buf, sizeof(buf), 0);
347*110acbc6SDaniel Lezcano 
348*110acbc6SDaniel Lezcano 	fprintf(tz->file_out, "%ld %s", getuptimeofday_ms(), buf);
349*110acbc6SDaniel Lezcano 
350*110acbc6SDaniel Lezcano 	read(fd, buf, sizeof(buf));
351*110acbc6SDaniel Lezcano 
352*110acbc6SDaniel Lezcano 	return 0;
353*110acbc6SDaniel Lezcano }
354*110acbc6SDaniel Lezcano 
355*110acbc6SDaniel Lezcano static int thermometer_start(struct thermometer *thermometer,
356*110acbc6SDaniel Lezcano 			     struct options *options)
357*110acbc6SDaniel Lezcano {
358*110acbc6SDaniel Lezcano 	struct itimerspec timer_it = { 0 };
359*110acbc6SDaniel Lezcano 	char *path;
360*110acbc6SDaniel Lezcano 	FILE *f;
361*110acbc6SDaniel Lezcano 	int i;
362*110acbc6SDaniel Lezcano 
363*110acbc6SDaniel Lezcano 	INFO("Capturing %d thermal zone(s) temperature...\n", thermometer->nr_tz);
364*110acbc6SDaniel Lezcano 
365*110acbc6SDaniel Lezcano 	if (access(options->output, F_OK) && mkdir(options->output, 0700)) {
366*110acbc6SDaniel Lezcano 		ERROR("Failed to create directory '%s'\n", options->output);
367*110acbc6SDaniel Lezcano 		return -1;
368*110acbc6SDaniel Lezcano 	}
369*110acbc6SDaniel Lezcano 
370*110acbc6SDaniel Lezcano 	for (i = 0; i < thermometer->nr_tz; i++) {
371*110acbc6SDaniel Lezcano 
372*110acbc6SDaniel Lezcano 		asprintf(&path, "%s/%s%s", options->output,
373*110acbc6SDaniel Lezcano 			 thermometer->tz[i].name, options->postfix);
374*110acbc6SDaniel Lezcano 
375*110acbc6SDaniel Lezcano 		if (!options->overwrite && !access(path, F_OK)) {
376*110acbc6SDaniel Lezcano 			ERROR("'%s' already exists\n", path);
377*110acbc6SDaniel Lezcano 			return -1;
378*110acbc6SDaniel Lezcano 		}
379*110acbc6SDaniel Lezcano 
380*110acbc6SDaniel Lezcano 		f = fopen(path, "w");
381*110acbc6SDaniel Lezcano 		if (!f) {
382*110acbc6SDaniel Lezcano 			ERROR("Failed to create '%s':%m\n", path);
383*110acbc6SDaniel Lezcano 			return -1;
384*110acbc6SDaniel Lezcano 		}
385*110acbc6SDaniel Lezcano 
386*110acbc6SDaniel Lezcano 		fprintf(f, "timestamp(ms) %s(°mC)\n", thermometer->tz[i].name);
387*110acbc6SDaniel Lezcano 
388*110acbc6SDaniel Lezcano 		thermometer->tz[i].file_out = f;
389*110acbc6SDaniel Lezcano 
390*110acbc6SDaniel Lezcano 		DEBUG("Created '%s' file for thermal zone '%s'\n", path, thermometer->tz[i].name);
391*110acbc6SDaniel Lezcano 
392*110acbc6SDaniel Lezcano 		/*
393*110acbc6SDaniel Lezcano 		 * Create polling timer
394*110acbc6SDaniel Lezcano 		 */
395*110acbc6SDaniel Lezcano 		thermometer->tz[i].fd_timer = timerfd_create(CLOCK_MONOTONIC, 0);
396*110acbc6SDaniel Lezcano 		if (thermometer->tz[i].fd_timer < 0) {
397*110acbc6SDaniel Lezcano 			ERROR("Failed to create timer for '%s': %m\n",
398*110acbc6SDaniel Lezcano 			      thermometer->tz[i].name);
399*110acbc6SDaniel Lezcano 			return -1;
400*110acbc6SDaniel Lezcano 		}
401*110acbc6SDaniel Lezcano 
402*110acbc6SDaniel Lezcano 		DEBUG("Watching '%s' every %d ms\n",
403*110acbc6SDaniel Lezcano 		      thermometer->tz[i].name, thermometer->tz[i].polling);
404*110acbc6SDaniel Lezcano 
405*110acbc6SDaniel Lezcano 		timer_it.it_interval = timer_it.it_value =
406*110acbc6SDaniel Lezcano 			msec_to_timespec(thermometer->tz[i].polling);
407*110acbc6SDaniel Lezcano 
408*110acbc6SDaniel Lezcano 		if (timerfd_settime(thermometer->tz[i].fd_timer, 0,
409*110acbc6SDaniel Lezcano 				    &timer_it, NULL) < 0)
410*110acbc6SDaniel Lezcano 			return -1;
411*110acbc6SDaniel Lezcano 
412*110acbc6SDaniel Lezcano 		if (mainloop_add(thermometer->tz[i].fd_timer,
413*110acbc6SDaniel Lezcano 				 timer_temperature_callback,
414*110acbc6SDaniel Lezcano 				 &thermometer->tz[i]))
415*110acbc6SDaniel Lezcano 			return -1;
416*110acbc6SDaniel Lezcano 	}
417*110acbc6SDaniel Lezcano 
418*110acbc6SDaniel Lezcano 	return 0;
419*110acbc6SDaniel Lezcano }
420*110acbc6SDaniel Lezcano 
421*110acbc6SDaniel Lezcano static int thermometer_execute(int argc, char *argv[], char *const envp[], pid_t *pid)
422*110acbc6SDaniel Lezcano {
423*110acbc6SDaniel Lezcano 	if (!argc)
424*110acbc6SDaniel Lezcano 		return 0;
425*110acbc6SDaniel Lezcano 
426*110acbc6SDaniel Lezcano 	*pid = fork();
427*110acbc6SDaniel Lezcano 	if (*pid < 0) {
428*110acbc6SDaniel Lezcano 		ERROR("Failed to fork process: %m");
429*110acbc6SDaniel Lezcano 		return -1;
430*110acbc6SDaniel Lezcano 	}
431*110acbc6SDaniel Lezcano 
432*110acbc6SDaniel Lezcano 	if (!(*pid)) {
433*110acbc6SDaniel Lezcano 		execvpe(argv[0], argv, envp);
434*110acbc6SDaniel Lezcano 		exit(1);
435*110acbc6SDaniel Lezcano 	}
436*110acbc6SDaniel Lezcano 
437*110acbc6SDaniel Lezcano 	return 0;
438*110acbc6SDaniel Lezcano }
439*110acbc6SDaniel Lezcano 
440*110acbc6SDaniel Lezcano static int kill_process(__maybe_unused int fd, void *arg)
441*110acbc6SDaniel Lezcano {
442*110acbc6SDaniel Lezcano 	pid_t pid = *(pid_t *)arg;
443*110acbc6SDaniel Lezcano 
444*110acbc6SDaniel Lezcano 	if (kill(pid, SIGTERM))
445*110acbc6SDaniel Lezcano 		ERROR("Failed to send SIGTERM signal to '%d': %p\n", pid);
446*110acbc6SDaniel Lezcano 	else if (waitpid(pid, NULL, 0))
447*110acbc6SDaniel Lezcano 		ERROR("Failed to wait pid '%d': %p\n", pid);
448*110acbc6SDaniel Lezcano 
449*110acbc6SDaniel Lezcano 	mainloop_exit();
450*110acbc6SDaniel Lezcano 
451*110acbc6SDaniel Lezcano 	return 0;
452*110acbc6SDaniel Lezcano }
453*110acbc6SDaniel Lezcano 
454*110acbc6SDaniel Lezcano static int exit_mainloop(__maybe_unused int fd, __maybe_unused void *arg)
455*110acbc6SDaniel Lezcano {
456*110acbc6SDaniel Lezcano 	mainloop_exit();
457*110acbc6SDaniel Lezcano 	return 0;
458*110acbc6SDaniel Lezcano }
459*110acbc6SDaniel Lezcano 
460*110acbc6SDaniel Lezcano static int thermometer_wait(struct options *options, pid_t pid)
461*110acbc6SDaniel Lezcano {
462*110acbc6SDaniel Lezcano 	int fd;
463*110acbc6SDaniel Lezcano 	sigset_t mask;
464*110acbc6SDaniel Lezcano 
465*110acbc6SDaniel Lezcano 	/*
466*110acbc6SDaniel Lezcano 	 * If there is a duration specified, we will exit the mainloop
467*110acbc6SDaniel Lezcano 	 * and gracefully close all the files which will flush the
468*110acbc6SDaniel Lezcano 	 * file system cache
469*110acbc6SDaniel Lezcano 	 */
470*110acbc6SDaniel Lezcano 	if (options->duration) {
471*110acbc6SDaniel Lezcano 		struct itimerspec timer_it = { 0 };
472*110acbc6SDaniel Lezcano 
473*110acbc6SDaniel Lezcano 		timer_it.it_value = msec_to_timespec(options->duration);
474*110acbc6SDaniel Lezcano 
475*110acbc6SDaniel Lezcano 		fd = timerfd_create(CLOCK_MONOTONIC, 0);
476*110acbc6SDaniel Lezcano 		if (fd < 0) {
477*110acbc6SDaniel Lezcano 			ERROR("Failed to create duration timer: %m\n");
478*110acbc6SDaniel Lezcano 			return -1;
479*110acbc6SDaniel Lezcano 		}
480*110acbc6SDaniel Lezcano 
481*110acbc6SDaniel Lezcano 		if (timerfd_settime(fd, 0, &timer_it, NULL)) {
482*110acbc6SDaniel Lezcano 			ERROR("Failed to set timer time: %m\n");
483*110acbc6SDaniel Lezcano 			return -1;
484*110acbc6SDaniel Lezcano 		}
485*110acbc6SDaniel Lezcano 
486*110acbc6SDaniel Lezcano 		if (mainloop_add(fd, pid < 0 ? exit_mainloop : kill_process, &pid)) {
487*110acbc6SDaniel Lezcano 			ERROR("Failed to set timer exit mainloop callback\n");
488*110acbc6SDaniel Lezcano 			return -1;
489*110acbc6SDaniel Lezcano 		}
490*110acbc6SDaniel Lezcano 	}
491*110acbc6SDaniel Lezcano 
492*110acbc6SDaniel Lezcano 	/*
493*110acbc6SDaniel Lezcano 	 * We want to catch any keyboard interrupt, as well as child
494*110acbc6SDaniel Lezcano 	 * signals if any in order to exit properly
495*110acbc6SDaniel Lezcano 	 */
496*110acbc6SDaniel Lezcano 	sigemptyset(&mask);
497*110acbc6SDaniel Lezcano 	sigaddset(&mask, SIGINT);
498*110acbc6SDaniel Lezcano 	sigaddset(&mask, SIGQUIT);
499*110acbc6SDaniel Lezcano 	sigaddset(&mask, SIGCHLD);
500*110acbc6SDaniel Lezcano 
501*110acbc6SDaniel Lezcano 	if (sigprocmask(SIG_BLOCK, &mask, NULL)) {
502*110acbc6SDaniel Lezcano 		ERROR("Failed to set sigprocmask: %m\n");
503*110acbc6SDaniel Lezcano 		return -1;
504*110acbc6SDaniel Lezcano 	}
505*110acbc6SDaniel Lezcano 
506*110acbc6SDaniel Lezcano 	fd = signalfd(-1, &mask, 0);
507*110acbc6SDaniel Lezcano 	if (fd < 0) {
508*110acbc6SDaniel Lezcano 		ERROR("Failed to set the signalfd: %m\n");
509*110acbc6SDaniel Lezcano 		return -1;
510*110acbc6SDaniel Lezcano 	}
511*110acbc6SDaniel Lezcano 
512*110acbc6SDaniel Lezcano 	if (mainloop_add(fd, exit_mainloop, NULL)) {
513*110acbc6SDaniel Lezcano 		ERROR("Failed to set timer exit mainloop callback\n");
514*110acbc6SDaniel Lezcano 		return -1;
515*110acbc6SDaniel Lezcano 	}
516*110acbc6SDaniel Lezcano 
517*110acbc6SDaniel Lezcano 	return mainloop(-1);
518*110acbc6SDaniel Lezcano }
519*110acbc6SDaniel Lezcano 
520*110acbc6SDaniel Lezcano static int thermometer_stop(struct thermometer *thermometer)
521*110acbc6SDaniel Lezcano {
522*110acbc6SDaniel Lezcano 	int i;
523*110acbc6SDaniel Lezcano 
524*110acbc6SDaniel Lezcano 	INFO("Closing/flushing output files\n");
525*110acbc6SDaniel Lezcano 
526*110acbc6SDaniel Lezcano 	for (i = 0; i < thermometer->nr_tz; i++)
527*110acbc6SDaniel Lezcano 		fclose(thermometer->tz[i].file_out);
528*110acbc6SDaniel Lezcano 
529*110acbc6SDaniel Lezcano 	return 0;
530*110acbc6SDaniel Lezcano }
531*110acbc6SDaniel Lezcano 
532*110acbc6SDaniel Lezcano int main(int argc, char *argv[], char *const envp[])
533*110acbc6SDaniel Lezcano {
534*110acbc6SDaniel Lezcano 	struct options options = {
535*110acbc6SDaniel Lezcano 		.loglvl = LOG_DEBUG,
536*110acbc6SDaniel Lezcano 		.logopt = TO_STDOUT,
537*110acbc6SDaniel Lezcano 		.output = ".",
538*110acbc6SDaniel Lezcano 	};
539*110acbc6SDaniel Lezcano 	struct configuration config = { 0 };
540*110acbc6SDaniel Lezcano 	struct thermometer thermometer = { 0 };
541*110acbc6SDaniel Lezcano 
542*110acbc6SDaniel Lezcano 	pid_t pid = -1;
543*110acbc6SDaniel Lezcano 
544*110acbc6SDaniel Lezcano 	if (options_init(argc, argv, &options))
545*110acbc6SDaniel Lezcano 		return THERMOMETER_OPTION_ERROR;
546*110acbc6SDaniel Lezcano 
547*110acbc6SDaniel Lezcano 	if (log_init(options.loglvl, argv[0], options.logopt))
548*110acbc6SDaniel Lezcano 		return THERMOMETER_LOG_ERROR;
549*110acbc6SDaniel Lezcano 
550*110acbc6SDaniel Lezcano 	if (configuration_init(options.config, &config))
551*110acbc6SDaniel Lezcano 		return THERMOMETER_CONFIG_ERROR;
552*110acbc6SDaniel Lezcano 
553*110acbc6SDaniel Lezcano 	if (uptimeofday_init())
554*110acbc6SDaniel Lezcano 		return THERMOMETER_TIME_ERROR;
555*110acbc6SDaniel Lezcano 
556*110acbc6SDaniel Lezcano 	if (thermometer_init(&config, &thermometer))
557*110acbc6SDaniel Lezcano 		return THERMOMETER_INIT_ERROR;
558*110acbc6SDaniel Lezcano 
559*110acbc6SDaniel Lezcano 	if (thermometer_start(&thermometer, &options))
560*110acbc6SDaniel Lezcano 		return THERMOMETER_RUNTIME_ERROR;
561*110acbc6SDaniel Lezcano 
562*110acbc6SDaniel Lezcano 	if (thermometer_execute(argc - optind, &argv[optind], envp, &pid))
563*110acbc6SDaniel Lezcano 		return THERMOMETER_RUNTIME_ERROR;
564*110acbc6SDaniel Lezcano 
565*110acbc6SDaniel Lezcano 	if (thermometer_wait(&options, pid))
566*110acbc6SDaniel Lezcano 		return THERMOMETER_RUNTIME_ERROR;
567*110acbc6SDaniel Lezcano 
568*110acbc6SDaniel Lezcano 	if (thermometer_stop(&thermometer))
569*110acbc6SDaniel Lezcano 		return THERMOMETER_RUNTIME_ERROR;
570*110acbc6SDaniel Lezcano 
571*110acbc6SDaniel Lezcano 	return THERMOMETER_SUCCESS;
572*110acbc6SDaniel Lezcano }
573