2012-06-15 21:07:11 +02:00
|
|
|
/**
|
2016-08-29 10:29:11 +02:00
|
|
|
* Copyright (C) (2012-present) Daniel Graziotin <daniel@ineed.coffee>
|
2017-03-27 11:44:34 -04:00
|
|
|
* Modifications (2017-present) by Robert Musial <rmusial@fastmail.com>
|
2012-06-15 21:07:11 +02:00
|
|
|
*
|
|
|
|
* 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 3 of the License, 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.
|
|
|
|
*
|
|
|
|
*/
|
2012-06-16 11:02:48 +02:00
|
|
|
|
2012-08-23 22:32:49 +02:00
|
|
|
/**
|
|
|
|
* Code formatted with astyle -A3 -s --break-blocks=all --add-brackets *.c *.h
|
|
|
|
*/
|
|
|
|
|
2012-06-11 21:43:53 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-11-12 11:48:05 +01:00
|
|
|
#include <syslog.h>
|
2017-03-19 13:43:55 -04:00
|
|
|
#include <stdbool.h>
|
2012-11-12 11:48:05 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2012-06-11 21:43:53 +02:00
|
|
|
#include "mbpfan.h"
|
|
|
|
#include "daemon.h"
|
|
|
|
#include "global.h"
|
2018-08-15 13:14:35 -07:00
|
|
|
#include "main.h"
|
2012-06-16 11:02:48 +02:00
|
|
|
#include "minunit.h"
|
2012-06-11 21:43:53 +02:00
|
|
|
|
|
|
|
int daemonize = 1;
|
|
|
|
int verbose = 0;
|
|
|
|
|
2012-11-12 11:48:05 +01:00
|
|
|
const char *PROGRAM_NAME = "mbpfan";
|
|
|
|
const char *PROGRAM_PID = "/var/run/mbpfan.pid";
|
|
|
|
|
|
|
|
const char *CORETEMP_PATH = "/sys/devices/platform/coretemp.0";
|
|
|
|
const char *APPLESMC_PATH = "/sys/devices/platform/applesmc.768";
|
2012-06-15 21:07:11 +02:00
|
|
|
|
2012-06-11 21:43:53 +02:00
|
|
|
void print_usage(int argc, char *argv[])
|
|
|
|
{
|
2012-08-22 15:05:52 +02:00
|
|
|
if (argc >=1) {
|
|
|
|
printf("Usage: %s OPTION(S) \n", argv[0]);
|
|
|
|
printf("Options:\n");
|
|
|
|
printf("\t-h Show this help screen\n");
|
|
|
|
printf("\t-f Run in foreground\n");
|
|
|
|
printf("\t-t Run the tests\n");
|
|
|
|
printf("\t-v Be (a lot) verbose\n");
|
|
|
|
printf("\n");
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
2012-06-11 21:43:53 +02:00
|
|
|
}
|
|
|
|
|
2012-06-16 11:02:48 +02:00
|
|
|
|
2012-11-12 11:48:05 +01:00
|
|
|
void check_requirements()
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for root
|
2017-06-21 11:39:16 +02:00
|
|
|
*/
|
2012-11-12 11:48:05 +01:00
|
|
|
|
|
|
|
uid_t uid=getuid(), euid=geteuid();
|
|
|
|
|
|
|
|
if (uid != 0 || euid != 0) {
|
2014-06-29 12:58:53 +02:00
|
|
|
syslog(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME);
|
2012-11-12 11:48:05 +01:00
|
|
|
printf("%s not started with root privileges. Please run %s as root. Exiting.\n", PROGRAM_NAME, PROGRAM_NAME);
|
2014-06-29 12:42:52 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2012-11-12 11:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-18 22:16:49 +01:00
|
|
|
* Check for coretemp and applesmc modules
|
|
|
|
*/
|
2017-06-21 11:39:16 +02:00
|
|
|
DIR* dir = opendir(CORETEMP_PATH);
|
2017-06-21 11:32:55 +02:00
|
|
|
|
2017-06-21 11:39:16 +02:00
|
|
|
if (ENOENT == errno) {
|
|
|
|
syslog(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
|
|
|
|
printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME);
|
|
|
|
exit(EXIT_FAILURE);
|
2012-11-12 11:48:05 +01:00
|
|
|
}
|
|
|
|
|
2017-06-21 11:39:16 +02:00
|
|
|
closedir(dir);
|
2014-05-24 19:51:01 +10:00
|
|
|
|
2012-11-12 11:48:05 +01:00
|
|
|
|
2017-06-21 11:39:16 +02:00
|
|
|
dir = opendir(APPLESMC_PATH);
|
2017-06-21 11:32:55 +02:00
|
|
|
|
2017-06-21 11:39:16 +02:00
|
|
|
if (ENOENT == errno) {
|
|
|
|
syslog(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
|
|
|
|
printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME);
|
|
|
|
exit(EXIT_FAILURE);
|
2012-11-12 11:48:05 +01:00
|
|
|
}
|
|
|
|
|
2017-06-21 11:39:16 +02:00
|
|
|
closedir(dir);
|
|
|
|
|
2014-05-24 19:51:01 +10:00
|
|
|
|
2012-11-12 11:48:05 +01:00
|
|
|
}
|
|
|
|
|
2012-06-16 11:02:48 +02:00
|
|
|
|
2017-07-21 08:43:21 -07:00
|
|
|
static int read_value(const char *path)
|
|
|
|
{
|
|
|
|
int value = -1;
|
|
|
|
FILE *file = fopen(path, "r");
|
|
|
|
if (file != NULL) {
|
|
|
|
fscanf(file, "%d", &value);
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void set_defaults(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *path;
|
|
|
|
int value;
|
|
|
|
for (i = 1; i <= 10; ++i) {
|
|
|
|
path = smprintf("%s/fan%d_min", APPLESMC_PATH, i);
|
|
|
|
value = read_value(path);
|
|
|
|
if (value != -1 && (min_fan_speed == -1 || value < min_fan_speed)) {
|
|
|
|
min_fan_speed = value;
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
path = smprintf("%s/fan%d_max", APPLESMC_PATH, i);
|
|
|
|
value = read_value(path);
|
|
|
|
if (value != -1 && (max_fan_speed == -1 || value > max_fan_speed)) {
|
|
|
|
max_fan_speed = value;
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_fan_speed == -1) {
|
|
|
|
min_fan_speed = 2000;
|
|
|
|
}
|
|
|
|
if (max_fan_speed == -1) {
|
|
|
|
max_fan_speed = 6200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-11 21:43:53 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
int c;
|
2012-08-23 22:32:49 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
while( (c = getopt(argc, argv, "hftv|help")) != -1) {
|
|
|
|
switch(c) {
|
2012-06-16 11:02:48 +02:00
|
|
|
case 'h':
|
2012-08-22 15:05:52 +02:00
|
|
|
print_usage(argc, argv);
|
2014-06-29 12:42:52 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2012-08-22 15:05:52 +02:00
|
|
|
break;
|
2014-06-22 16:48:44 +02:00
|
|
|
|
2012-06-16 11:02:48 +02:00
|
|
|
case 'f':
|
2012-08-22 15:05:52 +02:00
|
|
|
daemonize = 0;
|
|
|
|
break;
|
2014-06-22 16:48:44 +02:00
|
|
|
|
2012-06-16 11:02:48 +02:00
|
|
|
case 't':
|
2012-08-22 15:05:52 +02:00
|
|
|
tests();
|
2014-06-29 12:42:52 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2012-08-22 15:05:52 +02:00
|
|
|
break;
|
2014-06-22 16:48:44 +02:00
|
|
|
|
2012-06-16 11:02:48 +02:00
|
|
|
case 'v':
|
2012-08-22 15:05:52 +02:00
|
|
|
verbose = 1;
|
|
|
|
break;
|
2014-06-22 16:48:44 +02:00
|
|
|
|
2012-06-16 11:02:48 +02:00
|
|
|
default:
|
2012-08-22 15:05:52 +02:00
|
|
|
print_usage(argc, argv);
|
2014-06-29 12:42:52 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2012-08-22 15:05:52 +02:00
|
|
|
break;
|
2012-06-11 21:43:53 +02:00
|
|
|
}
|
2012-06-16 11:02:48 +02:00
|
|
|
}
|
2012-06-11 21:43:53 +02:00
|
|
|
|
2012-10-19 20:13:07 +02:00
|
|
|
|
|
|
|
|
2012-11-12 11:48:05 +01:00
|
|
|
check_requirements();
|
2017-07-21 08:43:21 -07:00
|
|
|
set_defaults();
|
2012-10-19 20:13:07 +02:00
|
|
|
|
2012-08-22 15:05:52 +02:00
|
|
|
// pointer to mbpfan() function in mbpfan.c
|
|
|
|
void (*fan_control)() = mbpfan;
|
|
|
|
go_daemon(fan_control);
|
2014-06-29 12:42:52 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2017-03-19 13:43:55 -04:00
|
|
|
}
|