File: cpulimit.c

package info (click to toggle)
cpulimit 2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 164 kB
  • ctags: 74
  • sloc: ansic: 990; makefile: 44
file content (1338 lines) | stat: -rw-r--r-- 40,705 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
/**
 * This program is licensed under the GNU General Public License,
 * version 2. A copy of the license can be found in the accompanying
 * LICENSE file.
 *
 **********************************************************************
 *
 * Simple program to limit the cpu usage of a process
 * If you modify this code, send me a copy please
 *
 * Author:  Angelo Marletta
 * Date:    26/06/2005
 * Version: 1.1
 *
 * Modifications and updates by: Jesse Smith
 * Date: May 4, 2011
 * Version 1.2 
 * Date: Jan 29, 2013
 * Version 1.2 and newer
 *
 * Modifications and updates by: Hasnain Lakhani
 * Date: Mar 26, 2014
 * Version 2.1
 */


#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/resource.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <limits.h>    // for compatibility

#ifdef __APPLE__
#include <mach/clock.h>
#include <mach/mach.h>
#endif


#ifdef FREEBSD
#include <fcntl.h>
#include <kvm.h>
#include <paths.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#endif

#ifdef LINUX
#include <dirent.h>
#define PROC_FILENAME 64
#define LINE_LENGTH 256
#endif

//kernel time resolution (inverse of one jiffy interval) in Hertz
//i don't know how to detect it, then define to the default (not very clean!)
#define HZ 100

//some useful macro
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)

// For platforms without PATH_MAX
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif

#define BEST_PRIORITY -10

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

#ifndef VERSION
#define VERSION 2.4
#endif

//pid of the controlled process
pid_t pid = 0;
pid_t my_pid;     // this process's PID

//executable file name
char *program_name;
//verbose mode
int verbose = FALSE;
//lazy mode
int lazy = FALSE;
// is higher priority nice possible?
int nice_lim;

// number of CPUs we detected
int NCPU;

// quiet mode
int quiet = FALSE;

// What signal should we send to the watched process
// when cpulimit exits?
int send_signal = SIGCONT;

//reverse byte search
// void *memrchr(const void *s, int c, size_t n);

#define MAX_SIGNAL 7
const char *SIGNAL_NAME[MAX_SIGNAL] = { "SIGHUP", "SIGINT", "SIGQUIT", 
                                 "SIGKILL", "SIGTERM", "SIGSTOP", "SIGCONT" };
const int SIGNAL_VALUE[MAX_SIGNAL] = { SIGHUP, SIGINT, SIGQUIT,
                                  SIGKILL, SIGTERM, SIGSTOP, SIGCONT };


//return ta-tb in microseconds (no overflow checks!)
inline long timediff(const struct timespec *ta,const struct timespec *tb) {
    unsigned long us = (ta->tv_sec-tb->tv_sec)*1000000 + (ta->tv_nsec/1000 - tb->tv_nsec/1000);
    return us;
}


#ifdef LINUX
#include <pthread.h>

typedef struct
{
   pid_t child;   // the child of our target process
   pid_t monitor; // the LimitCPU fork monitoring the child
   void *next; 
} CHILD;

int quitting = FALSE;           // Have we receive a quit signal
int monitor_children = FALSE;   // are we monitoring children of target

// Data passed to the monitor thread
typedef struct
{
   int limit;           // per cent limit to place on a process
   char *this_program;  // copy of argv[0]
} PROGRAM_DATA;

#endif



int Check_Us(pid_t target_pid)
{
   pid_t this_pid;

   this_pid = getpid();
   if (this_pid == target_pid)
   {
      fprintf(stderr, "We cannot throttle ourselves.\n");
      exit(7);
   }
   return TRUE;
}


int waitforpid(int pid) {
	//switch to low priority
	// if (setpriority(PRIO_PROCESS,getpid(),19)!=0) {
        /*
        if ( (nice_lim < INT_MAX) && 
             (setpriority(PRIO_PROCESS, my_pid, 19) != 0) ) {
		printf("Warning: cannot renice\n");
	}
        */
	int i=0;

	while(1) {

		DIR *dip;
		struct dirent *dit;

		//open a directory stream to /proc directory
		if ((dip = opendir("/proc")) == NULL) {
			perror("opendir");
			return -1;
		}

		//read in from /proc and seek for process dirs
		while ((dit = readdir(dip)) != NULL) {
			//get pid
			if (pid==atoi(dit->d_name)) {
				//pid detected
                                Check_Us(pid);
				if (kill(pid,SIGSTOP)==0 &&  kill(pid,SIGCONT)==0) {
					//process is ok!
                                        if (closedir(dip) == -1) {
                                           perror("closedir");
                                           return -1;
                                        }
					goto done;
				}
				else {
					fprintf(stderr,"Error: Process %d detected, but you don't have permission to control it\n",pid);
				}
			}
		}

		//close the dir stream and check for errors
		if (closedir(dip) == -1) {
			perror("closedir");
			return -1;
		}

		//no suitable target found
		if (i++==0) {
			if (lazy) {
				fprintf(stderr,"No process found\n");
				exit(2);
			}
			else {
				fprintf(stderr, "Warning: no target process found. Waiting for it...\n");
			}
		}

		//sleep for a while
		sleep(2);
	}

done:
    if (!quiet)
    	printf("Process %d detected\n",pid);
	//now set high priority, if possible
	// if (setpriority(PRIO_PROCESS,getpid(),-20)!=0) {
        /*
        if ( (nice_lim < INT_MAX) &&
             (setpriority(PRIO_PROCESS, my_pid, nice_lim) != 0) ) {
		printf("Warning: cannot renice.\nTo work better you should run this program as root.\n");
	}
        */
	return 0;

}

//this function periodically scans process list and looks for executable path names
//it should be executed in a low priority context, since precise timing does not matter
//if a process is found then its pid is returned
//process: the name of the wanted process, can be an absolute path name to the executable file
//         or simply its name
//return: pid of the found process
int getpidof(const char *process) {

	//set low priority
	// if (setpriority(PRIO_PROCESS,getpid(),19)!=0) {
        /*
        if ( (nice_lim < INT_MAX) &&
             (setpriority(PRIO_PROCESS, my_pid, 19) != 0) ) {
		printf("Warning: cannot renice\n");
	}
        */
	char exelink[20];
	char exepath[PATH_MAX+1];
	int pid=0;
	int i=0;

	while(1) {

		DIR *dip;
		struct dirent *dit;

		//open a directory stream to /proc directory
		if ((dip = opendir("/proc")) == NULL) {
			perror("opendir");
			return -1;
		}

		//read in from /proc and seek for process dirs
		while ((dit = readdir(dip)) != NULL) {
			//get pid
			pid=atoi(dit->d_name);
			if (pid>0) {
				sprintf(exelink,"/proc/%d/exe",pid);
				int size=readlink(exelink,exepath,sizeof(exepath));
				if (size>0) {
					int found=0;
					if (process[0]=='/' && strncmp(exepath,process,size)==0 && size==strlen(process)) {
						//process starts with / then it's an absolute path
						found=1;
					}
					else {
						//process is the name of the executable file
						if (strncmp(exepath+size-strlen(process),process,strlen(process))==0) {
							found=1;
						}
					}
					if (found==1) {
                                        Check_Us(pid);
						if (kill(pid,SIGSTOP)==0 &&  kill(pid,SIGCONT)==0) {
							//process is ok!
                                                        if (closedir(dip) == -1) {
                                                          perror("closedir");
                                                          return -1;
                                                        }
							goto done;
						}
						else {
							fprintf(stderr,"Error: Process %d detected, but you don't have permission to control it\n",pid);
						}
					}
				}
			}
		}

		//close the dir stream and check for errors
		if (closedir(dip) == -1) {
			perror("closedir");
			return -1;
		}

		//no suitable target found
		if (i++==0) {
			if (lazy) {
				fprintf(stderr,"No process found\n");
				exit(2);
			}
			else {
				fprintf(stderr, "Warning: no target process found. Waiting for it...\n");
			}
		}

		//sleep for a while
		sleep(2);
	}

done:
    if (!quiet)
    	printf("Process %d detected\n",pid);
	//now set high priority, if possible
	// if (setpriority(PRIO_PROCESS,getpid(),-20)!=0) {
        /*
        if ( (nice_lim < INT_MAX) &&
             (setpriority(PRIO_PROCESS, my_pid, nice_lim) != 0) ) {
		printf("Warning: cannot renice.\nTo work better you should run this program as root.\n");
	}
        */
	return pid;

}

//SIGINT and SIGTERM signal handler
void quit(int sig) {
	//let the process continue if we are stopped
	kill(pid, send_signal);
        #ifdef LINUX
        if (monitor_children)
        {
            quitting = TRUE;
            printf("Asking children to quit...\n");
            sleep(2);                // wait for thread clean-up
        }
        #endif
	printf("Exiting...\n");
	exit(0);
}

// Handle a child process quitting
void Child_Done(int sig)
{
   pid_t caught_child;
   caught_child = waitpid(-1, NULL, WNOHANG);
   if (verbose)
   {
      printf("Caught child process: %d\n", (int) caught_child);
      printf("%d\n", errno);
   }
   // If this was the one process we were watching, we can quit now.
   if (caught_child == pid)
   {
      printf("Child process is finished, exiting...\n");
      exit(0);
   }
}


#ifdef FREEBSD
int getjiffies(int pid)
{
   kvm_t *my_kernel = NULL;
   struct kinfo_proc *process_data = NULL;
   int processes;
   int my_jiffies = -1;

   my_kernel = kvm_open(0, 0, 0, O_RDONLY, "kvm_open");
   if (! my_kernel)
   {
      fprintf(stderr, "Error opening kernel vm. You should be running as root.\n");
      return -1;
   }

   process_data = kvm_getprocs(my_kernel, KERN_PROC_PID, pid, &processes);
   if ( (process_data) && (processes >= 1) )
       my_jiffies = process_data->ki_runtime;
   
   kvm_close(my_kernel);
   if (my_jiffies >= 0)
     my_jiffies /= 1000;
   return my_jiffies;
}

#endif

#ifdef LINUX
//get jiffies count from /proc filesystem
int getjiffies(int pid) {
	static char stat[20];
	static char buffer[1024];
        char *p;
	sprintf(stat,"/proc/%d/stat",pid);
	FILE *f=fopen(stat,"r");
	if (f==NULL) return -1;
	p = fgets(buffer,sizeof(buffer),f);
	fclose(f);
	// char *p=buffer;
        if (p)
        {
	  p=memchr(p+1,')',sizeof(buffer)-(p-buffer));
	  int sp=12;
	  while (sp--)
		p=memchr(p+1,' ',sizeof(buffer)-(p-buffer));
	  //user mode jiffies
	  int utime=atoi(p+1);
	  p=memchr(p+1,' ',sizeof(buffer)-(p-buffer));
	  //kernel mode jiffies
	  int ktime=atoi(p+1);
	  return utime+ktime;
        }
        // could not read info
        return -1;
}
#endif


//process instant photo
struct process_screenshot {
	struct timespec when;	//timestamp
	int jiffies;	//jiffies count of the process
	int cputime;	//microseconds of work from previous screenshot to current
};

//extracted process statistics
struct cpu_usage {
	float pcpu;
	float workingrate;
};

//this function is an autonomous dynamic system
//it works with static variables (state variables of the system), that keep memory of recent past
//its aim is to estimate the cpu usage of the process
//to work properly it should be called in a fixed periodic way
//perhaps i will put it in a separate thread...
int compute_cpu_usage(int pid,int last_working_quantum,struct cpu_usage *pusage) {
	#define MEM_ORDER 10
	//circular buffer containing last MEM_ORDER process screenshots
	static struct process_screenshot ps[MEM_ORDER];
	//the last screenshot recorded in the buffer
	static int front=-1;
	//the oldest screenshot recorded in the buffer
	static int tail=0;

	if (pusage==NULL) {
		//reinit static variables
		front=-1;
		tail=0;
		return 0;
	}

	//let's advance front index and save the screenshot
	front=(front+1)%MEM_ORDER;
	int j=getjiffies(pid);
	if (j>=0) ps[front].jiffies=j;
	else return -1;	//error: pid does not exist

        #ifdef __APPLE__
        // OS X does not have clock_gettime, use clock_get_time
        clock_serv_t cclock;
        mach_timespec_t mts;
        host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
        clock_get_time(cclock, &mts);
        mach_port_deallocate(mach_task_self(), cclock);
        ps[front].when.tv_sec = mts.tv_sec;
        ps[front].when.tv_nsec = mts.tv_nsec;

        #else
        // Linux and BSD can use real time
	clock_gettime(CLOCK_REALTIME,&(ps[front].when));
	ps[front].cputime=last_working_quantum;
        #endif
	//buffer actual size is: (front-tail+MEM_ORDER)%MEM_ORDER+1
	int size=(front-tail+MEM_ORDER)%MEM_ORDER+1;

	if (size==1) {
		//not enough samples taken (it's the first one!), return -1
		pusage->pcpu=-1;
		pusage->workingrate=1;
		return 0;
	}
	else {
		//now we can calculate cpu usage, interval dt and dtwork are expressed in microseconds
		long dt=timediff(&(ps[front].when),&(ps[tail].when));
		long dtwork=0;
		int i=(tail+1)%MEM_ORDER;
		int max=(front+1)%MEM_ORDER;
		do {
			dtwork+=ps[i].cputime;
			i=(i+1)%MEM_ORDER;
		} while (i!=max);
		int used=ps[front].jiffies-ps[tail].jiffies;
		float usage=(used*1000000.0/HZ)/dtwork;
		pusage->workingrate=1.0*dtwork/dt;
		pusage->pcpu=usage*pusage->workingrate;
		if (size==MEM_ORDER)
			tail=(tail+1)%MEM_ORDER;
		return 0;
	}
	#undef MEM_ORDER
}

void print_caption() {
	printf("\n%%CPU\twork quantum\tsleep quantum\tactive rate\n");
}


void increase_priority()
{
        //find the best available nice value
        int old_priority = getpriority(PRIO_PROCESS, 0);
        int priority = old_priority;
        while ( (setpriority(PRIO_PROCESS, 0, priority-1) == 0) &&
                (priority > BEST_PRIORITY) )
        {
                priority--;
        }
        if (priority != old_priority) {
                if (verbose) printf("Priority changed to %d\n", priority);
        }
        else {
                if (verbose) printf("Warning: Cannot change priority. Run as root or renice for best results.\n");
        }


}



#ifdef LINUX
// This following functions are for detecting and limiting child
// processes on Linux.

// This function adds a new child process to our list of child processes.
CHILD *Add_Child(CHILD *all_children, pid_t new_pid)
{
    CHILD *new_child = (CHILD *) calloc(sizeof(CHILD), 1);
    CHILD *current;

    if (! new_child)
       return all_children;

    new_child->next = NULL;
    new_child->child = new_pid;
    new_child->monitor = 0;

    if (all_children)
    {
        current = all_children;
        while (current->next)
           current = current->next;
        current->next = new_child;
        return all_children;
    }
    else   // this is the first node
       return new_child;
}



// This function removes a child PID node.
CHILD *Remove_Child(CHILD *all_children, pid_t old_pid)
{
    CHILD *current, *previous = NULL;
    int found = FALSE;

    current = all_children;
    while ( (! found) && (current) )
    {
        if (current->child == old_pid)
        {
           if (previous)
               previous->next = current->next;
           else
             all_children = current->next;
           
           free(current);
           found = TRUE;
        }
        else
        {
            previous = current;
            current = current->next;
        }
     }
     return all_children;
}


// This function cleans up all remaining child nodes.
void Clean_Up_Children(CHILD *all_children)
{
   CHILD *current, *next;

   current = all_children;
   while (current)
   {
      next = current->next;
      free(current);
      current = next;
   }
}


// This function searches the linked list for a matching PID.
// It returns NULL if no match is found and a pointer to the
// node is a match is located.
CHILD *Find_Child(CHILD *children, pid_t target)
{
   CHILD *current;
   int found = FALSE;

   current = children;
   while ( (!found) && (current) )
   {
      if (current->child == target)
        found = TRUE;
      else
        current = current->next;
   }
   return current;
}



// This function returns a list of process IDs of children
// of the given process (PID). It does this by searching the /proc
// file system and looking in the /proc/pid/status file for the PPid field.
// A linked list of child PIDs is returned on success or NULL on failure or
// if no child PIDs are found.
CHILD *Find_Child_PIDs(CHILD *all_children, pid_t parent_pid)
{
    int found = FALSE;
    DIR *proc;
    struct dirent *proc_entry;
    char filename[PROC_FILENAME];
    FILE *status_file;
    char *reading_file;
    char line[256];
    pid_t new_ppid;
    int current_pid;

    proc = opendir("/proc");
    if (! proc)
       return all_children;

    proc_entry = readdir(proc);
    while (proc_entry)
    {
        snprintf(filename, PROC_FILENAME, "/proc/%s/status", proc_entry->d_name);
        status_file = fopen(filename, "r");
        if (status_file)
        {
           found = FALSE;
           reading_file = fgets(line, LINE_LENGTH, status_file);
           while ( (! found) && (reading_file) )
           {
             if (! strncmp(line, "PPid:", 5) )
             {
                 sscanf(&(line[6]), "%d", &new_ppid);
                 if (new_ppid == parent_pid)
                 {
                     sscanf(proc_entry->d_name, "%d", &current_pid);
                     if (! Find_Child(all_children, current_pid) )
                        all_children = Add_Child(all_children, current_pid);
                 }
                 found = TRUE;
             }
             else
                reading_file = fgets(line, LINE_LENGTH, status_file);
           }   // done reading status file
          
           fclose(status_file);
        }
        proc_entry = readdir(proc);
    }   // done reading proc file system
    closedir(proc);
    return all_children;
}


// This function (which should probably be called as a thread) monitors the
// system for child processes of the current target process. When a new
// child of the target is located, it is added to the CHILD list.
// New children result in a new fork of this program being spawned to
// monitor the child process and its children.

void *Monitor_Children(void *all_data)
{
   CHILD *all_children = NULL;
   CHILD *current;
   PROGRAM_DATA *program_data = (PROGRAM_DATA *) all_data;

   while (! quitting )
   {
      // Check for new child processes
      all_children = Find_Child_PIDs(all_children, pid);

      // Find any children without monitors and create a monitoring process
      // Clean out old processes while we are looking
      current = all_children;
      while (current)
      {
         // First see if the child process is still running. If not,
         // we can remote its node.
         if (current->child)
         {
             char filename[PROC_FILENAME];
             DIR *child_directory;
             snprintf(filename, PROC_FILENAME, "/proc/%d", current->child);
             child_directory = opendir(filename);
             if (child_directory)
                closedir(child_directory);
             else
             {
                if (verbose)
                  printf("Child process %d done, cleaning up.\n",
                          (int) current->child);
                all_children = Remove_Child(all_children, current->child);
             }
         }  // end of clean up children processes no longer running

         // The child process is still running, but it might not have
         // a monitor. Create a new monitoring process.
         if ( (current->child) && (! current->monitor) )
         {
              pid_t returned_pid;

              if (verbose)
                 printf("Creating monitoring process for %d\n",
                        (int) current->child);

              returned_pid = fork();
              if (returned_pid > 0)
              {
                 // parent
                 current->monitor = returned_pid;
              }
              else if (returned_pid == 0)
              {
                 // child
                 char limit_amount[16];
                 char process_identifier[16];
                 snprintf(limit_amount, 16, "%d", (int) program_data->limit);
                 snprintf(process_identifier, 16, "%d", current->child);
                 if (verbose)
                    printf("Starting monitor with: %s -l %s -p %s -z -m\n",
                            program_data->this_program, limit_amount,
                            process_identifier);
                 execl(program_data->this_program, program_data->this_program,
                       "-l", limit_amount, "-p", process_identifier, 
                       "-z", "-m", (char *) NULL);
              }
              
         }    // end of creating a new monitor
         if (verbose)
         {
             printf("Watching child: %d with %d\n", 
                   (int) current->child, (int) current->monitor);
         }
         current = current->next;
      }
      sleep(1);
   }  // end LimitCPU is still running
   pthread_exit(NULL);
}

#endif       // end of monitoring children processes on Linux



void print_usage(FILE *stream,int exit_code) {
        fprintf(stream, "CPUlimit version %1.1f\n", VERSION);
	fprintf(stream, "Usage: %s TARGET [OPTIONS...] [-- PROGRAM]\n",program_name);
	fprintf(stream, "   TARGET must be exactly one of these:\n");
	fprintf(stream, "      -p, --pid=N        pid of the process\n");
	fprintf(stream, "      -e, --exe=FILE     name of the executable program file\n");
        fprintf(stream, "                         The -e option only works when\n");
        fprintf(stream, "                         cpulimit is run with admin rights.\n");
	fprintf(stream, "      -P, --path=PATH    absolute path name of the\n");
        fprintf(stream, "                         executable program file\n");
	fprintf(stream, "   OPTIONS\n");
        fprintf(stream, "      -b  --background   run in background\n");
        fprintf(stream, "      -f  --foreground   launch target process in foreground and wait for it to exit\n");
        fprintf(stream, "      -c  --cpu=N        override the detection of CPUs on the machine.\n");
	fprintf(stream, "      -l, --limit=N      percentage of cpu allowed from 1 up.\n");
        fprintf(stream, "                         Usually 1 - %d00, but can be higher\n", NCPU);
        fprintf(stream, "                         on multi-core CPUs (mandatory)\n");
        #ifdef LINUX
        fprintf(stream, "      -m, --monitor-forks  Watch children/forks of the target process\n");
        #endif
        fprintf(stream, "      -q, --quiet        run in quiet mode (only print errors).\n");
        fprintf(stream, "      -k, --kill         kill processes going over their limit\n");
        fprintf(stream, "                         instead of just throttling them.\n");
        fprintf(stream, "      -r, --restore      Restore processes after they have\n");
        fprintf(stream, "                         been killed. Works with the -k flag.\n");
        fprintf(stream, "      -s, --signal=SIG   Send this signal to the watched process when cpulimit exits.\n");
        fprintf(stream, "                         Signal should be specificed as a number or \n");
        fprintf(stream, "                         SIGTERM, SIGCONT, SIGSTOP, etc. SIGCONT is the default.\n");
	fprintf(stream, "      -v, --verbose      show control statistics\n");
	fprintf(stream, "      -z, --lazy         exit if there is no suitable target process,\n");
        fprintf(stream, "                         or if it dies\n");
        fprintf(stream, "          --             This is the final CPUlimit option. All following\n");
        fprintf(stream, "                         options are for another program we will launch.\n");
	fprintf(stream, "      -h, --help         display this help and exit\n");
	exit(exit_code);
}



// Get the number of CPU cores on this machine.
int get_ncpu()
{
        int ncpu = 1;
#ifdef _SC_NPROCESSORS_ONLN
        ncpu = sysconf(_SC_NPROCESSORS_ONLN);
#endif
        return ncpu;
}


// This function attempts to figure out what signal we should send
// target processes based on a command line paramter. First we check
// for text such as SIGINT, SIGCONT, SIGSTOP, etc. If no match is found
// then we assume the value given is a number and use that.
int Translate_Signal(char *my_signal)
{
    int signal_value;
    int index = 0, found = FALSE;
    // first check to see if we were passed a string
    while ( (index < MAX_SIGNAL) && (! found) )
    {
        if (! strcmp(my_signal, SIGNAL_NAME[index]) )
        {
            found = TRUE;
            signal_value = SIGNAL_VALUE[index];
        }
        else
           index++;
    }

    // no value found, try a number
    if (! found)
       signal_value = atoi(my_signal);

    return signal_value;
}



int main(int argc, char **argv) {

	//get program name
	// char *p=(char*)memrchr(argv[0],(unsigned int)'/',strlen(argv[0]));
	// program_name = p==NULL?argv[0]:(p+1);
        program_name = argv[0];
        int run_in_background = FALSE;
	//parse arguments
	int next_option;
	/* A string listing valid short options letters. */
        #ifdef LINUX
	const char* short_options="p:e:P:l:c:s:bfqkmrvzh";
        PROGRAM_DATA program_data;
        #else
	const char* short_options="p:e:P:l:c:s:bfqkrvzh";
        #endif
	/* An array describing valid long options. */
	const struct option long_options[] = {
		{ "pid", required_argument, NULL, 'p' },
		{ "exe", required_argument, NULL, 'e' },
		{ "path", required_argument, NULL, 'P' },
		{ "limit", required_argument, NULL, 'l' },
                { "background", no_argument, NULL, 'b' },
                { "foreground", no_argument, NULL, 'f' },
                { "quiet", no_argument, NULL, 'q' },
		{ "verbose", no_argument, NULL, 'v' },
		{ "lazy", no_argument, NULL, 'z' },
		{ "help", no_argument, NULL, 'h' },
                { "cpu", required_argument, NULL, 'c'},
                { "signal", required_argument, NULL, 's'},
                #ifdef LINUX
                { "monitor-forks", no_argument, NULL, 'm'},
                #endif
		{ NULL, 0, NULL, 0 }
	};
	//argument variables
	const char *exe=NULL;
        const char *path=NULL;
	int perclimit=0;
	int pid_ok = FALSE;
	int process_ok = FALSE;
	int limit_ok = FALSE;
        int last_known_argument = 0;
        int kill_process = FALSE;   // kill process instead of stopping it
        int restore_process = FALSE;  // restore killed process
        int run_child_in_background = TRUE;  // run cpulimit in background when 
                                             //  we launch new process
        // struct rlimit maxlimit;

        NCPU = get_ncpu();

        opterr = 0;      // avoid unwanted error messages for unknown parameters
	do {
		next_option = getopt_long (argc, argv, short_options,long_options, NULL);
		switch(next_option) {
                        case 'b':
                                run_in_background = TRUE;
                                last_known_argument++;
                                break;
                        case 'f':
                                run_child_in_background = FALSE;
                                run_in_background = FALSE;
                                last_known_argument++;
                                break;
			case 'p':
				pid=atoi(optarg);
                                if (pid)   // valid PID
                                {
				  pid_ok = TRUE;
                                  lazy = TRUE;
                                }
                                last_known_argument += 2;
				break;
			case 'e':
				exe=optarg;
				process_ok = TRUE;
                                last_known_argument += 2;
				break;
			case 'P':
				path=optarg;
				process_ok = TRUE;
                                last_known_argument += 2;
				break;
			case 'l':
				perclimit=atoi(optarg);
				limit_ok = TRUE;
                                last_known_argument += 2;
				break;
                        case 'c':
                                NCPU = atoi(optarg);
                                last_known_argument += 2;
                                break;
                        case 's':
                                send_signal = Translate_Signal(optarg);
                                if ( (send_signal < 1) || (send_signal > 35) )
                                {
                                    fprintf(stderr, "Specified exit signal is not recognized or not within bounds (1-35). Using SIGCONT.\n");
                                    send_signal = SIGCONT;
                                }
                                last_known_argument += 2;
                        case 'k':
                                kill_process = TRUE;
                                last_known_argument++;
                                break;
                        #ifdef LINUX
                        case 'm':
                                monitor_children = TRUE;
                                last_known_argument++;
                                break;
                        #endif
                        case 'r':
                                restore_process = TRUE;
                                last_known_argument++;
                                break;

			case 'v':
				verbose = TRUE;
                                last_known_argument++;
				break;
                        case 'q':
                                quiet = TRUE;
                                last_known_argument++;
                                break;
			case 'z':
				lazy = TRUE;
                                last_known_argument++;
				break;
			case 'h':
				print_usage (stdout, 1);
                                last_known_argument++;
				break;
                        case 'o':
                                last_known_argument++;
                                next_option = -1;
                                break;
			case '?':
				print_usage (stderr, 1);
                                last_known_argument++;
				break;
			case -1:
				break;
			// default:
			//	abort();
		}
	} while(next_option != -1);

        signal(SIGCHLD, Child_Done);

        // try to launch a program passed on the command line
        // But only if we do not already have a PID to watch
        if ( (last_known_argument + 1 < argc) && (pid_ok == FALSE) )
        {
           last_known_argument++;
           // if we stopped on "--" jump to the next parameter
           if ( (last_known_argument + 1 < argc) && (! strcmp(argv[last_known_argument], "--") ) )
               last_known_argument++;
           pid_t forked_pid;
           // try to launch remaining arguments
           if (verbose)
           {
               int index = last_known_argument;
               printf("Launching %s", argv[index]);
               for (index = last_known_argument + 1; index < argc; index++)
                    printf(" %s", argv[index]);
               printf(" with limit %d\n", perclimit);
           }
           forked_pid = fork();
           if (forked_pid == -1)  // error
           {
               printf("Failed to launch specified process.\n");
               exit(1);
           }
           else if (forked_pid == 0)   // target child
           {
              execvp(argv[last_known_argument],
                     &(argv[last_known_argument]) );
              exit(2);
           }
           else     // parent who will now fork the throttler
           {
              pid_t limit_pid;
              // if we are planning to kill a process, give it
              // a running head start to avoid death at start-up
              if (kill_process)
                 sleep(5);
    
              /* The following block assumes we want to run cpulimit in the
                 background. This is the default behaviour.
              */ 
              if (run_child_in_background)
              {
                 limit_pid = fork();
                 if (limit_pid == 0)   // child cpulimit process running in background
                 {
                    pid = forked_pid;    // the first child, target process
                    lazy = TRUE;
                    pid_ok = TRUE;
                    if (verbose)
                      printf("Throttling process %d\n", (int) pid);
                 }
                 else    // parent cpulimit process which can quit
                   exit(0);
              }  // end of running in background
              else
              {
                  pid = forked_pid;
                  lazy = TRUE;
                  pid_ok = TRUE;
                  run_in_background = FALSE;
              }  // end of running in foreground

           }  // end of parent that launched target

        }      // end of launching child process

	if (!process_ok && !pid_ok) {
		fprintf(stderr,"Error: You must specify a target process\n");
		print_usage (stderr, 1);
		exit(1);
	}
	if ((exe!=NULL && path!=NULL) || (pid_ok && (exe!=NULL || path!=NULL))) {
		fprintf(stderr,"Error: You must specify exactly one target process\n");
		print_usage (stderr, 1);
		exit(1);
	}
	if (!limit_ok) {
		fprintf(stderr,"Error: You must specify a cpu limit\n");
		print_usage (stderr, 1);
		exit(1);
	}
	float limit=perclimit/100.0;
	if ( (limit <= 0.00) || (limit > NCPU) )
        {
		fprintf(stderr,"Error: limit must be in the range of 1 to %d00\n", NCPU);
		print_usage (stderr, 1);
		exit(1);
	}

        // check to see if we should fork
        if (run_in_background)
        {
             pid_t process_id;
             process_id = fork();
             if (! process_id)
                exit(0);
             else
             {
                setsid();
                process_id = fork();
                if (process_id)
                  exit(0);
             }
        }

	//parameters are all ok!
	signal(SIGINT,quit);
	signal(SIGTERM,quit);

        my_pid = getpid();
        if (verbose)
           printf("%d CPUs detected.\n", NCPU);

        increase_priority();

	//time quantum in microseconds. it's splitted in a working period and a sleeping one
	int period=100000;
	struct timespec twork,tsleep;   //working and sleeping intervals
	memset(&twork,0,sizeof(struct timespec));
	memset(&tsleep,0,sizeof(struct timespec));

wait_for_process:

	//look for the target process..or wait for it
	if (exe != NULL)
		pid=getpidof(exe);
	else if (path != NULL)
		pid=getpidof(path);
	else 
		waitforpid(pid);

	
	//process detected...let's play

	//init compute_cpu_usage internal stuff
	compute_cpu_usage(0,0,NULL);
	//main loop counter
	int i=0;

	struct timespec startwork,endwork;
	long workingtime=0;		//last working time in microseconds

	if (verbose) print_caption();

	float pcpu_avg=0;

        // On Linux we can monitor child processes of the target
        #ifdef LINUX
        if (monitor_children)
        {
           pthread_t my_thread;
           int thread_status;
           if (verbose)
              printf("Starting fork monitoring thread...\n");
           program_data.this_program = argv[0];
           program_data.limit = perclimit;
           thread_status = pthread_create(&my_thread, NULL, 
                                          Monitor_Children, &program_data);
           if ( (thread_status) && (verbose) )
              printf("Creating fork monitoring thread failed.\n");
        }
        #endif

	//here we should already have high priority, for time precision
	while(1) {

		//estimate how much the controlled process is using the cpu in its working interval
		struct cpu_usage cu;
		if (compute_cpu_usage(pid,workingtime,&cu)==-1) {
            if (!quiet)
    			fprintf(stderr,"Process %d dead!\n",pid);
			if (lazy) exit(2);
			//wait until our process appears
			goto wait_for_process;		
		}

		//cpu actual usage of process (range 0-1)
		float pcpu=cu.pcpu;
		//rate at which we are keeping active the process (range 0-1)
		float workingrate=cu.workingrate;

		//adjust work and sleep time slices
		if (pcpu>0) {
			twork.tv_nsec=min(period*limit*1000/pcpu*workingrate,period*1000);
		}
		else if (pcpu==0) {
			twork.tv_nsec=period*1000;
		}
		else if (pcpu==-1) {
			//not yet a valid idea of cpu usage
			pcpu=limit;
			workingrate=limit;
			twork.tv_nsec=min(period*limit*1000,period*1000);
		}
		tsleep.tv_nsec=period*1000-twork.tv_nsec;

		//update average usage
		pcpu_avg=(pcpu_avg*i+pcpu)/(i+1);

		if (verbose && i%10==0 && i>0) {
			printf("%0.2f%%\t%6ld us\t%6ld us\t%0.2f%%\n",pcpu*100,twork.tv_nsec/1000,tsleep.tv_nsec/1000,workingrate*100);
                        if (i%200 == 0)
                           print_caption();
		}

		// if (limit<1 && limit>0) {
                // printf("Comparing %f to %f\n", pcpu, limit);
                if (pcpu < limit)
                {
                        // printf("Continue\n");
			//resume process
			if (kill(pid,SIGCONT)!=0) {
                             if (!quiet)
    				fprintf(stderr,"Process %d dead!\n",pid);
	                     if (lazy) exit(2);
				//wait until our process appears
				goto wait_for_process;
			}
		}

                #ifdef __APPLE_
                // OS X does not have clock_gettime, use clock_get_time
                clock_serv_t cclock;
                mach_timespec_t mts;
                host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
                clock_get_time(cclock, &mts);
                mach_port_deallocate(mach_task_self(), cclock);
                startwork.tv_sec = mts.tv_sec;
                startwork.tv_nsec = mts.tv_nsec;

                #else
		clock_gettime(CLOCK_REALTIME,&startwork);
                #endif

		nanosleep(&twork,NULL);		//now process is working
                #ifdef __APPLE__
                // OS X does not have clock_gettime, use clock_get_time
                // clock_serv_t cclock;
                // mach_timespec_t mts;
                host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
                clock_get_time(cclock, &mts);
                mach_port_deallocate(mach_task_self(), cclock);
                endwork.tv_sec = mts.tv_sec;
                endwork.tv_nsec = mts.tv_nsec;

                #else
		clock_gettime(CLOCK_REALTIME,&endwork);
                #endif
		workingtime=timediff(&endwork,&startwork);

		// if (limit<1) {
                // printf("Checking %f vs %f\n", pcpu, limit);
                if (pcpu > limit)
                {
                     // When over our limit we may run into
                     // situations where we want to kill
                     // the offending process, then restart it
                     if (kill_process)
                     {
                         kill(pid, SIGKILL);
                         if (!quiet)
                             fprintf(stderr, "Process %d killed.\n", pid);
                         if ( (lazy) && (! restore_process) ) 
                              exit(2);
                         // restart killed process
                         if (restore_process)
                         {
                             pid_t new_process;
                             new_process = fork();
                             if (new_process == -1)
                             {
                              fprintf(stderr, "Failed to restore killed process.\n");
                             }
                             else if (new_process == 0)
                             {
                                // child which becomes new process
                                if (verbose)
                                   printf("Relaunching %s\n",
                                          argv[last_known_argument]);
                                execvp(argv[last_known_argument],
                                       &(argv[last_known_argument]) ); 
                             }
                             else // parent
                             {
                                // we need to track new process
                                pid = new_process;
                                // avoid killing child process
                                sleep(5);
                             }
                         }
                     }
                     // do not kill process, just throttle it
                     else
                     {

                        // printf("Stop\n");
			//stop process, it has worked enough
			if (kill(pid,SIGSTOP)!=0) {
                            if (!quiet)
    				fprintf(stderr,"Process %d dead!\n", pid);
		            if (lazy) exit(2);
				//wait until our process appears
				goto wait_for_process;
			}
			nanosleep(&tsleep,NULL);	//now process is sleeping
                      }   // end of throttle process
		}         // end of process using too much CPU
		i++;
	}

   return 0;
}