Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Solaris #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/cpulimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#ifndef __sun__
#include <sys/sysctl.h>
#endif
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
Expand Down Expand Up @@ -173,9 +175,7 @@ int get_pid_max()
}
fclose(fd);
return atoi(buffer);
#elif defined __FreeBSD__
return 99998;
#elif defined __APPLE__
#else
return 99998;
#endif
}
Expand Down Expand Up @@ -319,7 +319,11 @@ int main(int argc, char **argv) {
int include_children = 0;

//get program name
#ifdef __sun__
char *p = strrchr(argv[0], (unsigned int)'/');
#else
char *p = (char*)memrchr(argv[0], (unsigned int)'/', strlen(argv[0]));
#endif
program_name = p==NULL ? argv[0] : (p+1);
//get current pid
cpulimit_pid = getpid();
Expand Down
4 changes: 4 additions & 0 deletions src/process_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

#include <assert.h>

#ifdef __sun__
#include <libgen.h>
#endif

#include "process_iterator.h"
#include "process_group.h"
#include "list.h"
Expand Down
8 changes: 7 additions & 1 deletion src/process_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef __APPLE__
#ifdef __sun__
#include <procfs.h>
#elif !defined __APPLE__
#include <sys/procfs.h>
#endif
#include <time.h>
Expand All @@ -42,6 +44,10 @@

#include "process_iterator_apple.c"

#elif defined __sun__

#include "process_iterator_solaris.c"

#else

#error Platform not supported
Expand Down
2 changes: 1 addition & 1 deletion src/process_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct process_filter {
};

struct process_iterator {
#ifdef __linux__
#if defined __linux__ || defined __sun__
DIR *dip;
int boot_time;
#elif defined __FreeBSD__
Expand Down
118 changes: 118 additions & 0 deletions src/process_iterator_solaris.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
*
* process_iterator_solaris.c
* Copyright (C) 2016 by Jim Mason <jmason at ibinx dot com>
*
* Adapted from process_iterator_linux.c
* Copyright (C) 2005-2012, by: Angelo Marletta <angelo dot marletta at gmail dot com>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

int init_process_iterator(struct process_iterator *it, struct process_filter *filter) {
// open a directory stream to /proc directory
if (!(it->dip = opendir("/proc"))) {
perror("opendir");
return -1;
}
it->filter = filter;
return 0;
}

static int read_process_info(pid_t pid, struct process *p) {
psinfo_t psinfo;
char statfile[32];

p->pid = pid;
sprintf(statfile, "/proc/%ld/psinfo", (long)pid);
FILE *fd = fopen(statfile, "r");
if (!fd) return -1;
if (!fread(&psinfo, sizeof(psinfo), 1, fd)) {
fclose(fd);
return -1;
}
fclose(fd);

p->ppid = psinfo.pr_ppid;
p->cputime = psinfo.pr_time.tv_sec * 1.0e03 + psinfo.pr_time.tv_nsec / 1.0e06;
p->starttime = psinfo.pr_start.tv_sec * 1.0e03 + psinfo.pr_start.tv_nsec / 1.0e06;
strcpy(p->command, psinfo.pr_psargs);

return 0;
}

static pid_t getppid_of(pid_t pid) {
psinfo_t psinfo;
char statfile[32];

sprintf(statfile, "/proc/%ld/psinfo", (long)pid);
FILE *fd = fopen(statfile, "r");
if (!fd) return -1;
if (!fread(&psinfo, sizeof(psinfo), 1, fd)) {
fclose(fd);
return -1;
}
fclose(fd);

return psinfo.pr_ppid;
}

static int is_child_of(pid_t child_pid, pid_t parent_pid) {
int ppid = child_pid;
while(ppid > 1 && ppid != parent_pid)
ppid = getppid_of(ppid);
return ppid == parent_pid;
}

int get_next_process(struct process_iterator *it, struct process *p) {
if (!it->dip) {
// end of processes
return -1;
}

if (it->filter->pid != 0 && !it->filter->include_children) {
int ret = read_process_info(it->filter->pid, p);
closedir(it->dip);
it->dip = NULL;
return ret;
}

// read in from /proc and seek for process dirs
struct dirent *dit;
while ((dit = readdir(it->dip))) {
p->pid = atoi(dit->d_name);
if (it->filter->pid != 0 && it->filter->pid != p->pid && !is_child_of(p->pid, it->filter->pid)) continue;
read_process_info(p->pid, p);
break;
}

if (!dit) {
// end of processes
closedir(it->dip);
it->dip = NULL;
return -1;
}

return 0;
}

int close_process_iterator(struct process_iterator *it) {
if (it->dip && closedir(it->dip) == -1) {
perror("closedir");
return 1;
}
it->dip = NULL;
return 0;
}