~ubuntu-branches/ubuntu/dapper/cpufreqd/dapper

« back to all changes in this revision

Viewing changes to sys_check.c

  • Committer: Bazaar Package Importer
  • Author(s): Mattia Dongili
  • Date: 2005-11-27 18:47:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127184742-9h26euwetr6kh1e6
Tags: 2.0.0-1

* New upstream release.
* cpufreqd.init: exit succesfully in case a stop is issued and
  cpufreqd is found running as requested by LSB thus making it
  possible to remove cpufreqd when cpufreqd itsef is stopped
  (closes: #340133)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C) 2002,2003,2004  Mattia Dongili<dongili@supereva.it>
3
 
 *                                George Staikos <staikos@0wned.org>
4
 
 *
5
 
 *  This program is free software; you can redistribute it and/or modify
6
 
 *  it under the terms of the GNU General Public License as published by
7
 
 *  the Free Software Foundation; either version 2 of the License, or
8
 
 *  (at your option) any later version.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
 */
19
 
#include "cpufreqd.h"
20
 
#include "main.h"
21
 
#include "sys_check.h"
22
 
 
23
 
/* int select_pm_plugin
24
 
 * 
25
 
 * Check the existance of power interfaces trying
26
 
 * to call the check function for each plugin
27
 
 */
28
 
 
29
 
 
30
 
 
31
 
/* int find_cpufreq_interface(general *configuration)
32
 
 *
33
 
 * Checks whether we need to use the 2.4 or 2.5/2.6
34
 
 * cpufreq interface and sets the has_sysfs bit
35
 
 * accordingly.
36
 
 *
37
 
 * Returns 0 on success, -1 otherwise
38
 
 */
39
 
int find_cpufreq_interface(general *configuration) {
40
 
  struct stat sb;
41
 
  int rc;
42
 
 
43
 
  /* try first with the 2.6 series */
44
 
  rc = stat(CPUFREQ_SYSFS_INTERFACE, &sb);
45
 
  if (rc == 0) {
46
 
    FILE *fp;
47
 
    char buf[256];
48
 
    
49
 
    cp_log(LOG_DEBUG, "find_cpufreq_interface(): found %s interface.\n", CPUFREQ_SYSFS_INTERFACE);
50
 
    configuration->has_sysfs = 1;
51
 
    
52
 
    /* Read min freq to translate frequencies */
53
 
    fp = fopen(CPUFREQ_SYSFS_INTERFACE_CPUMIN, "r");
54
 
    if (!fp || !fgets(buf, 255, fp)) {
55
 
      cp_log(LOG_ERR, "find_cpufreq_interface(): %s: %s\n",
56
 
             CPUFREQ_SYSFS_INTERFACE_CPUMIN, strerror(errno));
57
 
      return -1;
58
 
    }
59
 
    configuration->cpu_min_freq = atoi(buf);
60
 
    cp_log(LOG_DEBUG, "find_cpufreq_interface(): minimum available frequency %d\n", configuration->cpu_min_freq);
61
 
    fclose(fp);
62
 
 
63
 
    /* Read max freq to translate frequencies */
64
 
    fp = fopen(CPUFREQ_SYSFS_INTERFACE_CPUMAX, "r");
65
 
    if (!fp || !fgets(buf, 255, fp)) {
66
 
      cp_log(LOG_ERR, "find_cpufreq_interface(): %s: %s\n",
67
 
             CPUFREQ_SYSFS_INTERFACE_CPUMAX, strerror(errno));
68
 
      return -1;
69
 
    }
70
 
    configuration->cpu_max_freq = atoi(buf);
71
 
    cp_log(LOG_DEBUG, "find_cpufreq_interface(): maximum available frequency %d\n", configuration->cpu_max_freq);
72
 
    fclose(fp);
73
 
 
74
 
    return 0;
75
 
  }
76
 
  
77
 
  /* try with the 2.4 series or the deprecated /prco/cpufreq interface also */
78
 
  rc = stat(CPUFREQ_PROC_INTERFACE, &sb);
79
 
  if (rc == 0) {
80
 
    cp_log(LOG_DEBUG, "find_cpufreq_interface(): found %s interface.\n", CPUFREQ_PROC_INTERFACE);
81
 
    configuration->has_sysfs = 0;
82
 
    return 0;
83
 
  }
84
 
  
85
 
  cp_log(LOG_ERR, "find_cpufreq_interface(): no cpufreq interface found.\n");
86
 
  return -1;
87
 
    
88
 
}
89
 
 
90
 
 
91
 
/* int get_cpu_num(general *configuration)
92
 
 *
93
 
 * Gets the number of installed CPUs from procfs
94
 
 * and sets cpu_num appropriately.
95
 
 *
96
 
 * Returns 0 on success, -1 on error
97
 
 */
98
 
int get_cpu_num(general *configuration)
99
 
{
100
 
  FILE *fp;
101
 
  int n;
102
 
  char line[256];
103
 
 
104
 
  fp = fopen(CPUINFO_PROC, "r");
105
 
  if(!fp) {
106
 
    cp_log(LOG_ERR, "get_cpu_num(): %s: %s\n", CPUINFO_PROC, strerror(errno));
107
 
    return -1;
108
 
  }
109
 
  
110
 
  n = 0;
111
 
  while(!feof(fp)) {
112
 
    fgets(line, 255, fp);
113
 
    if(!strncmp(line, "processor", 9))
114
 
      n++;
115
 
  }
116
 
  fclose(fp);
117
 
  
118
 
  cp_log(LOG_DEBUG, "get_cpu_num(): found %i CPUs\n", n);
119
 
  configuration->cpu_num = n;
120
 
 
121
 
  return 0;
122
 
}
123