aboutsummaryrefslogtreecommitdiffstats
path: root/failsafe.c
blob: f02055dcf42b7227c848fb1e3f150754e2bf8755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*  This file is part of cputemp2maxfreq.

    Copyright (C) 2023-2024 pa4wdh

    cputemp2maxfreq 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.

    cputemp2maxfreq 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 cputemp2maxfreq; see the file COPYING.  If not, see
    <http://www.gnu.org/licenses>.
*/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "cputemp2maxfreq.h"
#include "cpufreq.h"
#include "logger.h"
#include "cpulist.h"

extern struct s_cpudata cpudata;
extern struct s_config config;

void failsafe(int code)
{
// Close CSV log if used
 if (config.csvfile!=NULL) csvlog_close();

 if (((config.keepstate==1) && (code!=0)) || config.keepstate==0)
 {

// First try to set the CPU to it's minimum frequency
  if (cpufreq_set_long_int("scaling_max_freq",cpudata.min_freq,100)>0)
  {
   config.logger("Set scaling frequency to CPU's minimum frequency");
   cpulist_free();
   exit(code);
  }
  config.logger("Failed to set scaling frequency to CPU's minimum frequency, error: %d (%s)",errno,strerror(errno));

// If that failed, try the fallback frequency
  if (cpufreq_set_long_int("scaling_max_freq",config.fallback_freq,100)>0)
  {
   config.logger("Set scaling frequency to fallback frequency");
   cpulist_free();
   exit(code);
  }
  config.logger("Failed to set scaling frequency to fallback frequency, error: %d (%s)",errno,strerror(errno));
 
// Everything failed, issue a warning 
  config.logger("All safety measures failed, watch out not to fry your hardware");
 } else {
// User asked for no action
  config.logger("Keeping current state due to -k option");
 }

 cpulist_free();
 exit(code);
}