various fixes to MidiRegionView selection handling, key handling, drawing of ghost...
[ardour2.git] / tools / sanity_check / systemtest.cpp
blob4801ac2f092ce68c3cbc7d3af22de7b27fc8b05f
1 /**
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
22 **/
24 /** maximum number of groups a user can be a member of **/
25 #define MAX_GROUPS 100
27 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <grp.h>
34 #include <sched.h>
35 #include <string.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
40 #include <stdio.h>
41 #include <errno.h>
43 #include "systemtest.h"
45 /**
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.
50 **/
51 int system_has_frequencyscaling() {
52 int fd;
54 fd = open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", O_RDONLY);
56 if (-1==fd) {
57 return 0;
60 (void) close(fd);
62 return 1;
66 static int read_string(char* filename, char* buf, size_t buflen) {
67 int fd;
68 ssize_t r=-1;
70 memset (buf, 0, buflen);
72 fd = open (filename, O_RDONLY);
73 if (-1<fd) {
74 r = read (fd, buf, buflen-1);
75 (void) close(fd);
77 if (-1==r) {
78 fprintf(stderr, "Error while reading \"%s\": %s\n", filename, strerror(errno));
79 exit(EXIT_FAILURE);
83 return (int) r;
87 static int read_int(char* filename, int* value) {
88 char buf[20];
90 if (0<read_string(filename, buf, 20)) {
91 return (1==sscanf(buf, "%d", value));
94 return 0;
98 /**
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];
110 while (!done) {
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)) {
120 if (min!=max) {
121 // wrong governor AND different frequency limits -> scaling
122 return 1;
127 } else {
128 // couldn't open file -> no more cores
129 done = 1;
131 cpu++;
134 // couldn't find anything that points to scaling
135 return 0;
139 static gid_t get_group_by_name(const char* name) {
140 struct group* grp;
141 gid_t res = 0;
143 while ((0==res) && (NULL != (grp = getgrent()))) {
144 if (0==strcmp(name, grp->gr_name)) {
145 res = grp->gr_gid;
149 endgrent();
151 return res;
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;
162 unsigned int gid;
164 if (NULL==list) {
165 perror("Cannot allocate group list structure");
166 exit(EXIT_FAILURE);
169 gid = get_group_by_name(name);
170 if (0==gid) {
171 fprintf(stderr, "No %s group found\n", name);
172 return 0;
175 num_groups = getgroups(MAX_GROUPS, list);
177 while (i<num_groups) {
178 if (list[i]==gid) {
179 found = 1;
180 i = num_groups;
183 i++;
186 free(list);
188 return found;
192 /***
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";
201 char cmd[100];
203 snprintf (cmd, sizeof (cmd), "grep -q 'rtprio *[0-9][0-9]*' %s", limits);
204 if (system (cmd) == 0) {
205 return 1;
207 return 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() {
247 int min_prio;
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");
254 exit(EXIT_FAILURE);
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");
263 exit(EXIT_FAILURE);
265 return 1;
268 return 0;
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");
277 exit(EXIT_FAILURE);
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() {
295 char buf[256];
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");
304 } else {
305 perror("read from /proc/meminfo");
308 return res*1024;
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() {
319 return NULL;
324 char* system_get_username() {
325 char* res = NULL;
326 char* name = NULL;
328 if ((name = getlogin())) {
329 res = strdup(name);
332 return res;