Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
simulate pfix=ram only for the currently installed version
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Jan 8, 2022
1 parent e79d9ba commit ef3c9c4
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions frugalify.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,55 @@ static int oom_score_adj(void)
return 0;
}

static int get_ver(char *buf, size_t len)
{
static char *const argv[] = {
"/bin/sh",
"-c",
". /etc/DISTRO_SPECS && echo -n \"$DISTRO_VERSION\"",
NULL
};
int pipefd[2];
ssize_t out;
autoclose int infd = -1, outfd = -1;
pid_t pid;
int status;

if (pipe(pipefd) < 0)
return -1;

infd = pipefd[0];
outfd = pipefd[1];

pid = fork();
switch (pid) {
case 0:
if (dup2(outfd, STDOUT_FILENO) == STDOUT_FILENO)
execv(argv[0], argv);

exit(EXIT_FAILURE);

case -1:
return -1;
}

if (waitpid(pid, &status, 0) != pid)
return -1;

out = read(infd, buf, len - 1);
if ((out <= 0) || (out == (len - 1)))
return -1;

if ((buf[0] < '0') || (buf[0] > '9'))
return -1;

buf[out] = '\0';
return 0;
}

static void do_pfixram(char **sfs, const int nsfs)
{
static char ver[sizeof("999.999.999")];
struct stat stbuf;
sigset_t mask;
const char *base;
Expand All @@ -488,6 +535,9 @@ static void do_pfixram(char **sfs, const int nsfs)
if (minsize <= 0)
return;

if (get_ver(ver, sizeof(ver)) < 0)
ver[0] = '\0';

for (i = nsfs -1; i >= 0; --i) {
base = strrchr(sfs[i], '/');
if (base)
Expand All @@ -502,6 +552,9 @@ static void do_pfixram(char **sfs, const int nsfs)
(strncmp(base, "nlsx_", sizeof("nlsx_") - 1) == 0))
continue;

if (ver[0] && !strstr(base, ver))
continue;

fd = open(sfs[i], O_RDONLY);
if (fd < 0)
continue;
Expand Down

0 comments on commit ef3c9c4

Please sign in to comment.