From 30090453feda5ae097c5156f76aeb0c86ace4316 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 1 Apr 2007 13:56:50 +0000 Subject: [PATCH] New module 'phystemp'. --- ChangeLog | 6 ++ lib/phystemp.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++ lib/phystemp.h | 26 +++++++++ modules/phystemp | 26 +++++++++ 4 files changed, 197 insertions(+) create mode 100644 lib/phystemp.c create mode 100644 lib/phystemp.h create mode 100644 modules/phystemp diff --git a/ChangeLog b/ChangeLog index 6012027761..a2ed43f324 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-04-01 Bruno Haible + + * modules/phystemp: New file. + * lib/phystemp.h: New file. + * lib/phystemp.c: New file. + 2007-04-01 Simon Josefsson * modules/crypto/arcfour: Moved from ../. diff --git a/lib/phystemp.c b/lib/phystemp.c new file mode 100644 index 0000000000..127b46193e --- /dev/null +++ b/lib/phystemp.c @@ -0,0 +1,139 @@ +/* Return physical temperatures. + Copyright (C) 2007 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include + +/* Specification. */ +#include "phystemp.h" + +#if defined __linux__ + +# include +# include + +# include "pipe.h" +# include "wait-process.h" +# include "getline.h" + +/* On Linux, temperature values are available through the 'sensors' program, + part of the 'sensors' or 'lm_sensors' package + + + To configure it, run 'sensors-detect' as root and follow the + recommendations. More explanations in this article: + */ + +double +phystemp_cpu (void) +{ + /* APRIL FOOL! */ + /* The output of the 'sensors' program consists of lines of the form + CPU Temp: +25°C (low = +0°C, high = +50°C) + or + temp1: +34°C (limit = +60°C) sensor = thermistor + So take the first line of this form. */ + char *argv[2]; + pid_t child; + int fd[1]; + FILE *fp; + char *line; + size_t linesize; + size_t linelen; + int exitstatus; + double temp_cpu; + double temp_mb; + + /* Call the sensors program. */ + argv[0] = "/usr/bin/sensors"; + argv[1] = NULL; + child = create_pipe_in ("sensors", argv[0], argv, DEV_NULL, true, true, + false, fd); + if (child == -1) + return -273 - 1; + + /* Retrieve its result. */ + fp = fdopen (fd[0], "r"); + if (fp == NULL) + return -273 - 1; + + temp_cpu = -273 - 1; + temp_mb = -273 - 1; + line = NULL; linesize = 0; + for (;;) + { + linelen = getline (&line, &linesize, fp); + if (linelen == (size_t)(-1)) + break; + if (linelen > 0 && line[linelen - 1] == '\n') + line[linelen - 1] = '\0'; + if (linelen >= 9 && memcmp (line, "CPU Temp:", 9) == 0) + temp_cpu = strtod (line + 9, NULL); + else if (linelen >= 5 && memcmp (line, "temp:", 5) == 0) + { + if (temp_cpu < -273) + temp_cpu = strtod (line + 5, NULL); + } + else if (linelen >= 6 && memcmp (line, "temp1:", 6) == 0) + { + if (temp_cpu < -273) + temp_cpu = strtod (line + 6, NULL); + } + else if (linelen >= 9 && memcmp (line, "M/B Temp:", 9) == 0) + temp_mb = strtod (line + 9, NULL); + } + + fclose (fp); + + /* Remove zombie process from process list, and retrieve exit status. */ + exitstatus = wait_subprocess (child, "sensors", true, true, true, false); + if (exitstatus != 0) + return -273 - 1; + + /* The temperature of the motherboard is a good approximation of the + temperature of the CPU. */ + if (temp_cpu < -273) + temp_cpu = temp_mb; + + return temp_cpu; +} + +#elif defined __BEOS__ + +# include + +double +phystemp_cpu (void) +{ + /* Yes, it's a function called 'is_...' that returns a 'double'! + It is specified as + "Returns the temperature of the motherboard if the computer is + currently on fire." + The temperature of the motherboard is a good approximation of the + temperature of the CPU. */ + double temp = is_computer_on_fire (); + return (temp > 0 ? temp : -273 - 1); +} + +#else + +double +phystemp_cpu (void) +{ + return -273 - 1; +} + +#endif diff --git a/lib/phystemp.h b/lib/phystemp.h new file mode 100644 index 0000000000..ba12c8cbf3 --- /dev/null +++ b/lib/phystemp.h @@ -0,0 +1,26 @@ +/* Return physical temperatures. + Copyright (C) 2007 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifndef _PHYSTEMP_H +#define _PHYSTEMP_H + +/* Return the physical temperature of the CPU, measured in degrees Celsius. + If it cannot be obtained, return a value < -273. */ +/* APRIL FOOL! */ +extern double phystemp_cpu (void); + +#endif /* _PHYSTEMP_H */ diff --git a/modules/phystemp b/modules/phystemp new file mode 100644 index 0000000000..8538d267f2 --- /dev/null +++ b/modules/phystemp @@ -0,0 +1,26 @@ +Description: +Return physical temperature. + +Files: +lib/phystemp.h +lib/phystemp.c + +Depends-on: +pipe +wait-process +getline +strtod + +configure.ac: + +Makefile.am: + +Include: +"phystemp.h" + +License: +GPL + +Maintainer: +Bruno Haible + -- 2.30.2