Skip to content
Snippets Groups Projects
Select Git revision
  • 5fe5bbb45546d7ae7c16e413cc6514d46cc3d9ee
  • master default protected
2 results

compctld.c

Blame
  • compctld.c 5.90 KiB
    #define _GNU_SOURCE /* struct ucred */
    #include <assert.h>
    #include <errno.h>
    #include <signal.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <syslog.h>
    #include <unistd.h>
    
    #include "cgroup.h"
    #include "common.h"
    
    
    #define begins_with(s_, a_) (!strncmp(s_, a_, sizeof(s_) - 1))
    
    
    void
    logperror(const char *s)
    {
    	syslog(LOG_ERR, "%s: %s", s, strerror(errno));
    }
    
    void
    memory_limits(size_t *minuser, size_t *mincomp, size_t *maxcomp, size_t *total)
    {
    	FILE *f = fopen("/proc/meminfo", "r");
    	char line[1024];
    	while (fgets(line, sizeof(line), f)) {
    		if (begins_with("MemTotal:", line)) {
    			*total = 0;
    			sscanf(line, "MemTotal:%zu", total);
    			*total *= 1024;
    			break;
    		}
    	}
    	fclose(f);
    
    	*minuser = *total * split_ratio;
    	if (*minuser < static_minfree)
    		*minuser = static_minfree;
    	if (*minuser > static_maxfree)
    		*minuser = static_maxfree;
    
    	*mincomp = static_minfree;
    	*maxcomp = *total - *minuser;
    	if (*maxcomp < 0) *maxcomp = 0;
    	/* maxcomp < mincomp may happen; they are used in different
    	 * settings. */
    }
    
    size_t
    get_default_mem_limit(void)
    {
    	size_t minuser, mincomp, maxcomp, total;
    	memory_limits(&minuser, &mincomp, &maxcomp, &total);
    	return maxcomp;
    }
    
    
    void
    cgroup_init(void)
    {
    	if (cgroup_setup(chier, "memory") < 0)
    		exit(EXIT_FAILURE);
    	int ret = cgroup_create(chier, cgroup);
    	if (ret < 0)