Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by rezo on 9/25/15.
- //
- #include <string.h>
- #include <stdlib.h>
- #include <sys/resource.h>
- #include <stdio.h>
- #include <errno.h>
- #include <unistd.h>
- #include "utils.h"
- int getRealNum(char option, char* options);
- int dolimits(long long int value,int setCommand, int resource, int isHardLimit,int flag,int wout,int count) ;
- int getRlimit(int flag, struct rlimit *rl,int wout);
- long long int convertValue(long long int show, int flag,int get);
- int dolimits(long long int value,int setCommand, int resource, int isHardLimit, int flag,int wout,int count)
- {
- struct rlimit rl ;
- if (setCommand){ // setting limit
- value = convertValue(value,flag,0);
- getRlimit(resource,&rl,wout);
- if(isHardLimit){
- rl.rlim_max = (rlim_t) flag;
- rl.rlim_cur = (rlim_t) value;
- } else{
- rl.rlim_cur = (rlim_t) value;
- }
- int er = setrlimit (resource, &rl);
- if (er!=0) {
- char * s = "\n cannot set limit\n";
- if(!write(wout,s,strlen(s)))
- _exit(EXIT_FAILURE);
- char buf[3] ;
- memset(buf, 0, sizeof (buf));
- sprintf(buf, "%d", errno);
- if(!write(wout,s,strlen(s)))
- _exit(EXIT_FAILURE);
- }
- } else{ // getting limit
- char *strings[15]={"core file size (blocks, -c) ",
- "data seg size (kbytes, -d) ",
- "scheduling priority (-e) ",
- "file size (blocks, -f) ",
- "pending signals (-i) ",
- "max locked memory (kbytes, -l) ",
- "max memory size (kbytes, -m) ",
- "open files (-n) ",
- "POSIX message queues (bytes, -q) ",
- "real-time priority (-r) ",
- "stack size (kbytes, -s) ",
- "cpu time (seconds, -t) ",
- "max user processes (-u) ",
- "virtual memory (kbytes, -v) ",
- "file locks (-x) ",
- };
- getRlimit(resource, &rl,wout);
- long long int toShow = (long long int) rl.rlim_cur ;
- if(isHardLimit){
- toShow = (long long int) rl.rlim_max;
- }
- if (count>1) {
- if (!write(wout, strings[flag], strlen(strings[flag])))
- _exit(EXIT_FAILURE);
- }
- if (toShow ==-1) {
- if( !write(wout, "unlimited", 9))
- _exit(EXIT_FAILURE);
- }
- else {
- toShow = convertValue(toShow,flag,1);
- writeInt((int) toShow,wout);
- }
- if( !write(wout," \n",2))
- _exit(EXIT_FAILURE);
- }
- return 0 ;
- }
- long long int convertValue(long long int show, int flag,int get) {
- flag ++;
- if(flag==2||flag==6||flag==7||flag==11||flag==14) {
- if (get)
- return show / 1024;
- else
- return show * 1024;
- }
- return show;
- }
- int getRlimit(int flag, struct rlimit *rl,int wout) {
- int getlstatus = getrlimit (flag, rl);
- if (getlstatus !=0) {
- char * s = "cannot get limit" ;
- ssize_t s1 = write(wout,s,strlen(s));
- if (s1<=1){
- _exit(EXIT_FAILURE);
- }
- return errno ;
- }
- return 0;
- }
- int parseGoodOpts(int startI, char **argv, int argn,int limits [10] , char* options, int wout,int count){
- if(argn>3){
- perror("usage: [-HScdflmnstuv limit] argn > 3");
- exit( EXIT_FAILURE );
- }
- int flag = 2;
- int isHard = 0;
- int toSet = 0 ;
- int value = 0 ;
- char mode = 'S';
- char * valueString = NULL ;
- if (argn==3){
- toSet = 1 ;
- mode = argv[startI + 2][1] ;
- flag = getRealNum(argv[startI][1],options);
- valueString = argv[startI+1];
- } else if (argn == 2) {
- if (argv[startI][0] != '-') {
- toSet = 1 ;
- valueString = argv[startI];
- mode = argv[startI+1][1] ;
- } else {
- flag = getRealNum(argv[startI][1],options);
- if(argv[startI+1][0]=='-') {
- toSet = 0 ;
- mode = argv[startI+1][1] ;
- } else {
- toSet = 1;
- valueString = argv[startI+1];
- }
- }
- }else if (argn == 1){
- /* for (int i = startI; i <startI+argn ; ++i) {
- printf("%s",argv[i]);
- }*/
- if (argv[startI ][0] == '-') {
- char optionName = argv[startI][1];
- int optionNumber = getRealNum(optionName,options);
- if( optionNumber == -3){
- Assert(0, "usage: [-HScdflmnstuv limit]");
- } else{
- switch (optionNumber){
- case -2: {
- toSet = 0;
- isHard = 1;
- break;
- }
- case -1:
- isHard = 0 ;
- toSet = 0;
- break;
- default: flag =optionNumber;
- break;
- }
- }
- } else{
- toSet =1 ;
- valueString = argv[startI];
- }
- }
- if (valueString!=NULL) {
- const char *strInt = valueString;
- char *end;
- value = (int) strtol(strInt, &end, 10);
- } else value =0 ;
- if(mode=='H'){
- isHard =1;
- }
- if(mode=='S'){
- isHard =0;
- }
- int resource = limits[flag-1];
- if(flag==0){
- for (int i = 0; i < 15; ++i) {
- dolimits(0,0,limits[i],isHard,flag+i,wout,2);
- }
- return 0;
- }else
- return dolimits(value,toSet, resource,isHard,flag-1,wout,count);
- }
- int
- getRealNum(char option, char* options){
- char* pointer = strchr(options, option);
- if(pointer==NULL){
- Assert(0, "usage: [-HScdflmnstuv limit]");
- } else {
- int ans = (int) (pointer - options) - 2;
- return ans ;
- }
- return -3;
- }
- int
- execute_ulimits(char** args, int size, int wout)
- {
- if(size==1){
- dolimits(0,0,RLIMIT_FSIZE,0,2,wout,1);
- }
- char * options = "HSacdefilmnqrstuvx";
- int limits [15] = {RLIMIT_CORE,RLIMIT_DATA,RLIMIT_NICE,RLIMIT_FSIZE,RLIMIT_SIGPENDING,RLIMIT_MEMLOCK,RLIMIT_RSS,RLIMIT_NOFILE,
- RLIMIT_MSGQUEUE,RLIMIT_RTPRIO,RLIMIT_STACK,RLIMIT_CPU,RLIMIT_NPROC,RLIMIT_AS,RLIMIT_LOCKS};
- int count = 0 ;
- for (int j = 1; j < size; ++j) {
- if (args[j][0]=='-'&&strlen(args[j])==2&&getRealNum(args[j][1],options)>=0){
- count++;
- }
- }
- int commandNum = 1 ;
- for ( int i = 1; i < size; ++i ) {
- char* fullOption = args[i];
- if( fullOption[0] == '-'){
- if( strlen(fullOption) > 2) { //option with '-'
- Assert(0, "usage: [-HScdflmnstuv limit]");
- } else { //valid option form
- char optionName = fullOption[1];
- int optionNumber = getRealNum(optionName,options);
- if( optionNumber == -3){
- Assert(0, "usage: [-HScdflmnstuv limit]");
- } else if(optionNumber>=0&&i >1) {
- parseGoodOpts(commandNum, args, i-commandNum,limits,options,wout,count);
- commandNum = i;
- if(i!=size-1)
- continue;
- }
- }
- }
- if(i==size-1){
- parseGoodOpts(commandNum, args, i-commandNum+1,limits,options,wout,count);
- }
- }
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement