<commit_before>/**
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Set of functions to gather system information for the jack setup wizard.
 * 
 * TODO: Test for rt prio availability
 *
 * @author <NAME>, <EMAIL>
 *
 **/

/** maximum number of groups a user can be a member of **/
#define MAX_GROUPS 100

#include <fcntl.h>

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>

#include <sched.h>
#include <string.h>

#include <sys/time.h>
#include <sys/resource.h>

#include <stdio.h>
#include <errno.h>

#include <jack/systemtest.h>

/**
 * This function checks for the existence of known frequency scaling mechanisms 
 * in this system by testing for the availability of scaling governors/
 *
 * @returns 0 if the system has no frequency scaling capabilities non-0 otherwise.
 **/
int system_has_frequencyscaling() {
  int fd;

  fd = open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", O_RDONLY);

  if (-1==fd) {
    return 0;
  }

  (void) close(fd);

  return 1;
}


static int read_string(char* filename, char* buf, size_t buflen) {
  int fd;
  ssize_t r=-1;

  memset (buf, 0, buflen);

  fd = open (filename, O_RDONLY);
  if (-1<fd) {
    r = read (fd, buf, buflen-1);
    (void) close(fd);
    
    if (-1==r) {
      fprintf(stderr, "Error while reading \"%s\": %s\n", filename, strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
  
  return (int) r;
}


static int read_int(char* filename, int* value) {
  char buf[20];

  if (0<read_string(filename, buf, 20)) {
		return (1==sscanf(buf, "%d", value));
  }

  return 0;
}


/**
 * This function determines wether any CPU core uses a variable clock speed if frequency 
 * scaling is available. If the governor for all cores is either "powersave" or
 * "performance", the CPU frequency can be assumed to be static. This is also the case
 * if scaling_min_freq and scaling_max_freq are set to the same value.
 *
 * @returns 0 if system doesn't use frequency scaling at the moment, non-0 otherwise
 **/
int system_uses_frequencyscaling() {
  int cpu=0, done=0, min, max;
  char filename[256], buf[256];

  while (!done) {
    (void) snprintf(filename, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpu);
    if (0<read_string(filename, buf, 256)) {
      if ((0!=strcmp("performance", buf)) && 
					(0!=strcmp("powersafe", buf))) {
				// So it's neither the "performance" nor the "powersafe" governor
				(void) snprintf(filename, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
				if (read_int(filename, &min)) {
					(void) snprintf(filename, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
					if (read_int(filename, &max)) {
						if (min!=max) {
							// wrong governor AND different frequency limits -> scaling
							return 1;
						}
					} 
				}
      }
    } else {
      // couldn't open file -> no more cores
      done = 1;
    }
    cpu++;
  }
  
  // couldn't find anything that points to scaling
  return 0;
}


static gid_t get_group_by_name(const char* name) {
  struct group* grp;
	gid_t res = 0;

  while ((0==res) && (NULL != (grp = getgrent()))) {
    if (0==strcmp(name, grp->gr_name)) {
      res = grp->gr_gid;
    }
  }

	endgrent();

  return res;
}

/***
 * Checks for a definition in /etc/security/limits.conf that looks
 * as if it allows RT scheduling priority.
 *
 * @returns 1 if there appears to be such a line
 **/
int system_has_rtprio_limits_conf ()
{
	const char* limits = "/etc/security/limits.conf";
	char cmd[100];

	snprintf (cmd, sizeof (cmd), "grep -q 'rtprio *[0-9][0-9]*' %s", limits);
	if (system (cmd) == 0) {
		return 1;
	}
	return 0;
}


/**
 * Checks for the existence of the 'audio' group on this system
 *
 * @returns 0 is there is no 'audio' group, the group id otherwise
 **/
int system_has_audiogroup() {
	return get_group_by_name("audio") || get_group_by_name ("jackuser");
}


/**
 * Tests wether the owner of this process is in the 'audio' group.
 *
 * @returns 0 if the owner of this process is not in the audio group, non-0 otherwise
 **/
int system_user_in_audiogroup() {
  gid_t* list = (gid_t*) malloc(MAX_GROUPS * sizeof(gid_t));
  int num_groups, i=0, found=0;
	unsigned int gid;

  if (NULL==list) {
    perror("Cannot allocate group list structure");
    exit(EXIT_FAILURE);
  }

  gid = get_group_by_name("audio");
  if (0==gid) {
    fprintf(stderr, "No audio group found\n");
    exit(EXIT_FAILURE);
  }
  
  num_groups = getgroups(MAX_GROUPS, list);
  
  while (i<num_groups) {
    if (list[i]==gid) {
      found = 1;
      i = num_groups;
    }
    
    i++;
  }
  
  free(list);

  return found;
}


/**
 * Determines wether the owner of this process can enable rt priority.
 *
 * @returns 0 if this process can not be switched to rt prio, non-0 otherwise
 **/
int system_user_can_rtprio() {
  int min_prio;
  struct sched_param schparam;

  memset(&schparam, 0, sizeof(struct sched_param));

  if (-1 == (min_prio = sched_get_priority_min(SCHED_RR))) {
    perror("sched_get_priority");
    exit(EXIT_FAILURE);
  }
  schparam.sched_priority = min_prio;  

  if (0 == sched_setscheduler(0, SCHED_RR, &schparam)) {
    // TODO: restore previous state
    schparam.sched_priority = 0;
    if (0 != sched_setscheduler(0, SCHED_OTHER, &schparam)) {
      perror("sched_setscheduler");
      exit(EXIT_FAILURE);
    }<commit_msg>systemtest should use SCHED_FIFO not SCHED_RR<commit_after>/**
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Set of functions to gather system information for the jack setup wizard.
 * 
 * TODO: Test for rt prio availability
 *
 * @author <NAME>, <EMAIL>
 *
 **/

/** maximum number of groups a user can be a member of **/
#define MAX_GROUPS 100

#include <fcntl.h>

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>

#include <sched.h>
#include <string.h>

#include <sys/time.h>
#include <sys/resource.h>

#include <stdio.h>
#include <errno.h>

#include <jack/systemtest.h>

/**
 * This function checks for the existence of known frequency scaling mechanisms 
 * in this system by testing for the availability of scaling governors/
 *
 * @returns 0 if the system has no frequency scaling capabilities non-0 otherwise.
 **/
int system_has_frequencyscaling() {
  int fd;

  fd = open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", O_RDONLY);

  if (-1==fd) {
    return 0;
  }

  (void) close(fd);

  return 1;
}


static int read_string(char* filename, char* buf, size_t buflen) {
  int fd;
  ssize_t r=-1;

  memset (buf, 0, buflen);

  fd = open (filename, O_RDONLY);
  if (-1<fd) {
    r = read (fd, buf, buflen-1);
    (void) close(fd);
    
    if (-1==r) {
      fprintf(stderr, "Error while reading \"%s\": %s\n", filename, strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
  
  return (int) r;
}


static int read_int(char* filename, int* value) {
  char buf[20];

  if (0<read_string(filename, buf, 20)) {
		return (1==sscanf(buf, "%d", value));
  }

  return 0;
}


/**
 * This function determines wether any CPU core uses a variable clock speed if frequency 
 * scaling is available. If the governor for all cores is either "powersave" or
 * "performance", the CPU frequency can be assumed to be static. This is also the case
 * if scaling_min_freq and scaling_max_freq are set to the same value.
 *
 * @returns 0 if system doesn't use frequency scaling at the moment, non-0 otherwise
 **/
int system_uses_frequencyscaling() {
  int cpu=0, done=0, min, max;
  char filename[256], buf[256];

  while (!done) {
    (void) snprintf(filename, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpu);
    if (0<read_string(filename, buf, 256)) {
      if ((0!=strcmp("performance", buf)) && 
					(0!=strcmp("powersafe", buf))) {
				// So it's neither the "performance" nor the "powersafe" governor
				(void) snprintf(filename, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_min_freq", cpu);
				if (read_int(filename, &min)) {
					(void) snprintf(filename, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
					if (read_int(filename, &max)) {
						if (min!=max) {
							// wrong governor AND different frequency limits -> scaling
							return 1;
						}
					} 
				}
      }
    } else {
      // couldn't open file -> no more cores
      done = 1;
    }
    cpu++;
  }
  
  // couldn't find anything that points to scaling
  return 0;
}


static gid_t get_group_by_name(const char* name) {
  struct group* grp;
	gid_t res = 0;

  while ((0==res) && (NULL != (grp = getgrent()))) {
    if (0==strcmp(name, grp->gr_name)) {
      res = grp->gr_gid;
    }
  }

	endgrent();

  return res;
}

/***
 * Checks for a definition in /etc/security/limits.conf that looks
 * as if it allows RT scheduling priority.
 *
 * @returns 1 if there appears to be such a line
 **/
int system_has_rtprio_limits_conf ()
{
	const char* limits = "/etc/security/limits.conf";
	char cmd[100];

	snprintf (cmd, sizeof (cmd), "grep -q 'rtprio *[0-9][0-9]*' %s", limits);
	if (system (cmd) == 0) {
		return 1;
	}
	return 0;
}


/**
 * Checks for the existence of the 'audio' group on this system
 *
 * @returns 0 is there is no 'audio' group, the group id otherwise
 **/
int system_has_audiogroup() {
	return get_group_by_name("audio") || get_group_by_name ("jackuser");
}


/**
 * Tests wether the owner of this process is in the 'audio' group.
 *
 * @returns 0 if the owner of this process is not in the audio group, non-0 otherwise
 **/
int system_user_in_audiogroup() {
  gid_t* list = (gid_t*) malloc(MAX_GROUPS * sizeof(gid_t));
  int num_groups, i=0, found=0;
	unsigned int gid;

  if (NULL==list) {
    perror("Cannot allocate group list structure");
    exit(EXIT_FAILURE);
  }

  gid = get_group_by_name("audio");
  if (0==gid) {
    fprintf(stderr, "No audio group found\n");
    exit(EXIT_FAILURE);
  }
  
  num_groups = getgroups(MAX_GROUPS, list);
  
  while (i<num_groups) {
    if (list[i]==gid) {
      found = 1;
      i = num_groups;
    }
    
    i++;
  }
  
  free(list);

  return found;
}


/**
 * Determines wether the owner of this process can enable rt priority.
 *
 * @returns 0 if this process can not be switched to rt prio, non-0 otherwise
 **/
int system_user_can_rtprio() {
  int min_prio;
  struct sched_param schparam;

  memset(&schparam, 0, sizeof(struct sched_param));

  if (-1 == (min_prio = sched_get_priority_min(SCHED_FIFO))) {
    perror("sched_get_priority");
    exit(EXIT_FAILURE);
  }
  schparam.sched_priority = min_prio;  

  if (0 == sched_setscheduler(0, SCHED_FIFO, &schparam)) {
    // TODO: restore previous state
    schparam.sched_priority = 0;
    if (0 != sched_setscheduler(0, SCHED_OTHER, &schparam)) {
      perror("sched_setscheduler");
      exit(EXIT_FAILURE);
    }
{'fraction_non_alphanumeric': 0.1281415343915344, 'fraction_numerical': 0.01455026455026455, 'mean_word_length': 3.3389526542324246, 'pattern_counts': {'":': 2, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4531682', 'n_tokens_mistral': 4374, 'n_tokens_neox': 4090, 'n_words': 1341}