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);
176 num_groups = getgroups(MAX_GROUPS, list);
178 while (i<num_groups) {
194 * Checks for a definition in /etc/security/limits.conf that looks
195 * as if it allows RT scheduling priority.
197 * @returns 1 if there appears to be such a line
199 int system_has_rtprio_limits_conf ()
201 const char* limits = "/etc/security/limits.conf";
204 snprintf (cmd, sizeof (cmd), "grep -q 'rtprio *[0-9][0-9]*' %s", limits);
205 if (system (cmd) == 0) {
213 * Checks for the existence of the 'audio' group on this system
215 * @returns 0 if there is no 'audio' group, the group id otherwise
217 int system_has_audiogroup() {
218 return get_group_by_name("audio") || get_group_by_name ("jackuser");
223 * Checks for the existence of 'groupname' on this system
225 * @returns 0 if there is no group, the group id otherwise
227 int system_has_group(const char * name) {
228 return get_group_by_name(name);
233 * Tests wether the owner of this process is in the 'audio' group.
235 * @returns 0 if the owner of this process is not in the audio group, non-0 otherwise
237 int system_user_in_audiogroup() {
238 return system_user_in_group("audio") || system_user_in_group("jackuser");
243 * Determines wether the owner of this process can enable rt priority.
245 * @returns 0 if this process can not be switched to rt prio, non-0 otherwise
247 int system_user_can_rtprio() {
249 struct sched_param schparam;
251 memset(&schparam, 0, sizeof(struct sched_param));
253 if (-1 == (min_prio = sched_get_priority_min(SCHED_FIFO))) {
254 perror("sched_get_priority");
257 schparam.sched_priority = min_prio;
259 if (0 == sched_setscheduler(0, SCHED_FIFO, &schparam)) {
260 // TODO: restore previous state
261 schparam.sched_priority = 0;
262 if (0 != sched_setscheduler(0, SCHED_OTHER, &schparam)) {
263 perror("sched_setscheduler");
273 long long unsigned int system_memlock_amount() {
274 struct rlimit limits;
276 if (-1==getrlimit(RLIMIT_MEMLOCK, &limits)) {
277 perror("getrlimit on RLIMIT_MEMLOCK");
281 return limits.rlim_max;
286 * Checks wether the memlock limit is unlimited
288 * @returns - 0 if the memlock limit is limited, non-0 otherwise
290 int system_memlock_is_unlimited() {
291 return ((RLIM_INFINITY==system_memlock_amount())?1:0);
295 long long unsigned int system_available_physical_mem() {
297 long long unsigned int res = 0;
299 if (0<read_string(const_cast<char*>("/proc/meminfo"), buf, sizeof (buf))) {
300 if (strncmp (buf, "MemTotal:", 9) == 0) {
301 if (sscanf (buf, "%*s %llu", &res) != 1) {
302 perror ("parse error in /proc/meminfo");
306 perror("read from /proc/meminfo");
314 * Gets the version of the currently running kernel. The string
315 * returned has to be freed by the caller.
317 * @returns String with the full version of the kernel
319 char* system_kernel_version() {
325 char* system_get_username() {
329 if ((name = getlogin())) {