2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 * Set of functions to gather system information for the jack setup wizard.
18 * TODO: Test for rt prio availability
20 * @author Florian Faber, faber@faberman.de
24 /** maximum number of groups a user can be a member of **/
25 #define MAX_GROUPS 100
30 #include <sys/types.h>
38 #include <sys/resource.h>
43 #include "systemtest.h"
46 * This function checks for the existence of known frequency scaling mechanisms
47 * in this system by testing for the availability of scaling governors/
49 * @returns 0 if the system has no frequency scaling capabilities non-0 otherwise.
51 int system_has_frequencyscaling() {
54 fd
= open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", O_RDONLY
);
66 static int read_string(char* filename
, char* buf
, size_t buflen
) {
70 memset (buf
, 0, buflen
);
72 fd
= open (filename
, O_RDONLY
);
74 r
= read (fd
, buf
, buflen
-1);
78 fprintf(stderr
, "Error while reading \"%s\": %s\n", filename
, strerror(errno
));
87 static int read_int(char* filename
, int* value
) {
90 if (0<read_string(filename
, buf
, 20)) {
91 return (1==sscanf(buf
, "%d", value
));
99 * This function determines wether any CPU core uses a variable clock speed if frequency
100 * scaling is available. If the governor for all cores is either "powersave" or
101 * "performance", the CPU frequency can be assumed to be static. This is also the case
102 * if scaling_min_freq and scaling_max_freq are set to the same value.
104 * @returns 0 if system doesn't use frequency scaling at the moment, non-0 otherwise
106 int system_uses_frequencyscaling() {
107 int cpu
=0, done
=0, min
, max
;
108 char filename
[256], buf
[256];
111 (void) snprintf(filename
, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpu
);
112 if (0<read_string(filename
, buf
, 256)) {
113 if ((0!=strncmp("performance", buf
, 11)) &&
114 (0!=strncmp("powersafe", buf
, 9))) {
115 // So it's neither the "performance" nor the "powersafe" governor
116 (void) snprintf(filename
, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu
);
117 if (read_int(filename
, &min
)) {
118 (void) snprintf(filename
, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu
);
119 if (read_int(filename
, &max
)) {
121 // wrong governor AND different frequency limits -> scaling
128 // couldn't open file -> no more cores
134 // couldn't find anything that points to scaling
139 static gid_t
get_group_by_name(const char* name
) {
143 while ((0==res
) && (NULL
!= (grp
= getgrent()))) {
144 if (0==strcmp(name
, grp
->gr_name
)) {
155 * Tests wether the owner of this process is in the group 'name'.
157 * @returns 0 if the owner of this process is not in the group, non-0 otherwise
159 int system_user_in_group(const char *name
) {
160 gid_t
* list
= (gid_t
*) malloc(MAX_GROUPS
* sizeof(gid_t
));
161 int num_groups
, i
=0, found
=0;
165 perror("Cannot allocate group list structure");
169 gid
= get_group_by_name(name
);
171 fprintf(stderr
, "No %s group found\n", name
);
175 num_groups
= getgroups(MAX_GROUPS
, list
);
177 while (i
<num_groups
) {
193 * Checks for a definition in /etc/security/limits.conf that looks
194 * as if it allows RT scheduling priority.
196 * @returns 1 if there appears to be such a line
198 int system_has_rtprio_limits_conf ()
200 const char* limits
= "/etc/security/limits.conf";
203 snprintf (cmd
, sizeof (cmd
), "grep -q 'rtprio *[0-9][0-9]*' %s", limits
);
204 if (system (cmd
) == 0) {
212 * Checks for the existence of the 'audio' group on this system
214 * @returns 0 if there is no 'audio' group, the group id otherwise
216 int system_has_audiogroup() {
217 return get_group_by_name("audio") || get_group_by_name ("jackuser");
222 * Checks for the existence of 'groupname' on this system
224 * @returns 0 if there is no group, the group id otherwise
226 int system_has_group(const char * name
) {
227 return get_group_by_name(name
);
232 * Tests wether the owner of this process is in the 'audio' group.
234 * @returns 0 if the owner of this process is not in the audio group, non-0 otherwise
236 int system_user_in_audiogroup() {
237 return system_user_in_group("audio") || system_user_in_group("jackuser");
242 * Determines wether the owner of this process can enable rt priority.
244 * @returns 0 if this process can not be switched to rt prio, non-0 otherwise
246 int system_user_can_rtprio() {
248 struct sched_param schparam
;
250 memset(&schparam
, 0, sizeof(struct sched_param
));
252 if (-1 == (min_prio
= sched_get_priority_min(SCHED_FIFO
))) {
253 perror("sched_get_priority");
256 schparam
.sched_priority
= min_prio
;
258 if (0 == sched_setscheduler(0, SCHED_FIFO
, &schparam
)) {
259 // TODO: restore previous state
260 schparam
.sched_priority
= 0;
261 if (0 != sched_setscheduler(0, SCHED_OTHER
, &schparam
)) {
262 perror("sched_setscheduler");
272 long long unsigned int system_memlock_amount() {
273 struct rlimit limits
;
275 if (-1==getrlimit(RLIMIT_MEMLOCK
, &limits
)) {
276 perror("getrlimit on RLIMIT_MEMLOCK");
280 return limits
.rlim_max
;
285 * Checks wether the memlock limit is unlimited
287 * @returns - 0 if the memlock limit is limited, non-0 otherwise
289 int system_memlock_is_unlimited() {
290 return ((RLIM_INFINITY
==system_memlock_amount())?1:0);
294 long long unsigned int system_available_physical_mem() {
296 long long unsigned int res
= 0;
298 if (0<read_string((char*)"/proc/meminfo", buf
, sizeof (buf
))) {
299 if (strncmp (buf
, "MemTotal:", 9) == 0) {
300 if (sscanf (buf
, "%*s %llu", &res
) != 1) {
301 perror ("parse error in /proc/meminfo");
305 perror("read from /proc/meminfo");
313 * Gets the version of the currently running kernel. The string
314 * returned has to be freed by the caller.
316 * @returns String with the full version of the kernel
318 char* system_kernel_version() {
324 char* system_get_username() {
328 if ((name
= getlogin())) {