Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. //
  2. // Created by rezo on 9/25/15.
  3. //
  4.  
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <sys/resource.h>
  8. #include <stdio.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include "utils.h"
  12.  
  13.  
  14. int getRealNum(char option, char* options);
  15. int dolimits(long long int value,int setCommand, int resource, int isHardLimit,int flag,int wout,int count) ;
  16.  
  17.  
  18. int getRlimit(int flag, struct rlimit *rl,int wout);
  19.  
  20. long long int convertValue(long long int show, int flag,int get);
  21.  
  22. int dolimits(long long int value,int setCommand, int resource, int isHardLimit, int flag,int wout,int count)
  23. {
  24. struct rlimit rl ;
  25. if (setCommand){ // setting limit
  26. value = convertValue(value,flag,0);
  27. getRlimit(resource,&rl,wout);
  28. if(isHardLimit){
  29. rl.rlim_max = (rlim_t) flag;
  30. rl.rlim_cur = (rlim_t) value;
  31. } else{
  32. rl.rlim_cur = (rlim_t) value;
  33. }
  34.  
  35. int er = setrlimit (resource, &rl);
  36. if (er!=0) {
  37. char * s = "\n cannot set limit\n";
  38. if(!write(wout,s,strlen(s)))
  39. _exit(EXIT_FAILURE);
  40. char buf[3] ;
  41. memset(buf, 0, sizeof (buf));
  42. sprintf(buf, "%d", errno);
  43. if(!write(wout,s,strlen(s)))
  44. _exit(EXIT_FAILURE);
  45.  
  46. }
  47.  
  48.  
  49. } else{ // getting limit
  50.  
  51. char *strings[15]={"core file size (blocks, -c) ",
  52. "data seg size (kbytes, -d) ",
  53. "scheduling priority (-e) ",
  54. "file size (blocks, -f) ",
  55. "pending signals (-i) ",
  56. "max locked memory (kbytes, -l) ",
  57. "max memory size (kbytes, -m) ",
  58. "open files (-n) ",
  59. "POSIX message queues (bytes, -q) ",
  60. "real-time priority (-r) ",
  61. "stack size (kbytes, -s) ",
  62. "cpu time (seconds, -t) ",
  63. "max user processes (-u) ",
  64. "virtual memory (kbytes, -v) ",
  65. "file locks (-x) ",
  66. };
  67. getRlimit(resource, &rl,wout);
  68.  
  69. long long int toShow = (long long int) rl.rlim_cur ;
  70. if(isHardLimit){
  71. toShow = (long long int) rl.rlim_max;
  72. }
  73.  
  74.  
  75. if (count>1) {
  76. if (!write(wout, strings[flag], strlen(strings[flag])))
  77. _exit(EXIT_FAILURE);
  78. }
  79.  
  80. if (toShow ==-1) {
  81.  
  82. if( !write(wout, "unlimited", 9))
  83. _exit(EXIT_FAILURE);
  84. }
  85. else {
  86. toShow = convertValue(toShow,flag,1);
  87. writeInt((int) toShow,wout);
  88. }
  89. if( !write(wout," \n",2))
  90. _exit(EXIT_FAILURE);
  91.  
  92.  
  93.  
  94.  
  95. }
  96. return 0 ;
  97. }
  98.  
  99. long long int convertValue(long long int show, int flag,int get) {
  100.  
  101. flag ++;
  102. if(flag==2||flag==6||flag==7||flag==11||flag==14) {
  103. if (get)
  104. return show / 1024;
  105. else
  106. return show * 1024;
  107. }
  108. return show;
  109. }
  110.  
  111. int getRlimit(int flag, struct rlimit *rl,int wout) {
  112. int getlstatus = getrlimit (flag, rl);
  113. if (getlstatus !=0) {
  114. char * s = "cannot get limit" ;
  115. ssize_t s1 = write(wout,s,strlen(s));
  116. if (s1<=1){
  117. _exit(EXIT_FAILURE);
  118. }
  119. return errno ;
  120. }
  121. return 0;
  122. }
  123.  
  124.  
  125.  
  126. int parseGoodOpts(int startI, char **argv, int argn,int limits [10] , char* options, int wout,int count){
  127.  
  128. if(argn>3){
  129. perror("usage: [-HScdflmnstuv limit] argn > 3");
  130. exit( EXIT_FAILURE );
  131. }
  132. int flag = 2;
  133. int isHard = 0;
  134. int toSet = 0 ;
  135. int value = 0 ;
  136. char mode = 'S';
  137. char * valueString = NULL ;
  138. if (argn==3){
  139. toSet = 1 ;
  140. mode = argv[startI + 2][1] ;
  141. flag = getRealNum(argv[startI][1],options);
  142. valueString = argv[startI+1];
  143. } else if (argn == 2) {
  144. if (argv[startI][0] != '-') {
  145. toSet = 1 ;
  146. valueString = argv[startI];
  147. mode = argv[startI+1][1] ;
  148. } else {
  149. flag = getRealNum(argv[startI][1],options);
  150. if(argv[startI+1][0]=='-') {
  151. toSet = 0 ;
  152. mode = argv[startI+1][1] ;
  153. } else {
  154. toSet = 1;
  155. valueString = argv[startI+1];
  156. }
  157. }
  158. }else if (argn == 1){
  159.  
  160. /* for (int i = startI; i <startI+argn ; ++i) {
  161. printf("%s",argv[i]);
  162. }*/
  163. if (argv[startI ][0] == '-') {
  164. char optionName = argv[startI][1];
  165. int optionNumber = getRealNum(optionName,options);
  166. if( optionNumber == -3){
  167. Assert(0, "usage: [-HScdflmnstuv limit]");
  168. } else{
  169. switch (optionNumber){
  170. case -2: {
  171. toSet = 0;
  172. isHard = 1;
  173. break;
  174. }
  175. case -1:
  176. isHard = 0 ;
  177. toSet = 0;
  178. break;
  179. default: flag =optionNumber;
  180. break;
  181.  
  182. }
  183. }
  184. } else{
  185. toSet =1 ;
  186. valueString = argv[startI];
  187. }
  188.  
  189.  
  190. }
  191.  
  192. if (valueString!=NULL) {
  193. const char *strInt = valueString;
  194. char *end;
  195. value = (int) strtol(strInt, &end, 10);
  196. } else value =0 ;
  197. if(mode=='H'){
  198. isHard =1;
  199. }
  200. if(mode=='S'){
  201. isHard =0;
  202. }
  203. int resource = limits[flag-1];
  204. if(flag==0){
  205. for (int i = 0; i < 15; ++i) {
  206. dolimits(0,0,limits[i],isHard,flag+i,wout,2);
  207. }
  208. return 0;
  209. }else
  210.  
  211. return dolimits(value,toSet, resource,isHard,flag-1,wout,count);
  212. }
  213.  
  214.  
  215. int
  216. getRealNum(char option, char* options){
  217. char* pointer = strchr(options, option);
  218. if(pointer==NULL){
  219. Assert(0, "usage: [-HScdflmnstuv limit]");
  220. } else {
  221. int ans = (int) (pointer - options) - 2;
  222. return ans ;
  223. }
  224. return -3;
  225.  
  226. }
  227.  
  228.  
  229. int
  230. execute_ulimits(char** args, int size, int wout)
  231. {
  232.  
  233. if(size==1){
  234. dolimits(0,0,RLIMIT_FSIZE,0,2,wout,1);
  235. }
  236.  
  237. char * options = "HSacdefilmnqrstuvx";
  238. int limits [15] = {RLIMIT_CORE,RLIMIT_DATA,RLIMIT_NICE,RLIMIT_FSIZE,RLIMIT_SIGPENDING,RLIMIT_MEMLOCK,RLIMIT_RSS,RLIMIT_NOFILE,
  239. RLIMIT_MSGQUEUE,RLIMIT_RTPRIO,RLIMIT_STACK,RLIMIT_CPU,RLIMIT_NPROC,RLIMIT_AS,RLIMIT_LOCKS};
  240. int count = 0 ;
  241. for (int j = 1; j < size; ++j) {
  242. if (args[j][0]=='-'&&strlen(args[j])==2&&getRealNum(args[j][1],options)>=0){
  243. count++;
  244. }
  245. }
  246.  
  247. int commandNum = 1 ;
  248. for ( int i = 1; i < size; ++i ) {
  249. char* fullOption = args[i];
  250. if( fullOption[0] == '-'){
  251. if( strlen(fullOption) > 2) { //option with '-'
  252. Assert(0, "usage: [-HScdflmnstuv limit]");
  253. } else { //valid option form
  254. char optionName = fullOption[1];
  255. int optionNumber = getRealNum(optionName,options);
  256. if( optionNumber == -3){
  257. Assert(0, "usage: [-HScdflmnstuv limit]");
  258. } else if(optionNumber>=0&&i >1) {
  259. parseGoodOpts(commandNum, args, i-commandNum,limits,options,wout,count);
  260. commandNum = i;
  261. if(i!=size-1)
  262. continue;
  263. }
  264. }
  265. }
  266. if(i==size-1){
  267. parseGoodOpts(commandNum, args, i-commandNum+1,limits,options,wout,count);
  268. }
  269.  
  270. }
  271.  
  272. return 0 ;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement