-
Notifications
You must be signed in to change notification settings - Fork 113
/
various.c
1168 lines (992 loc) · 26.5 KB
/
various.c
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
/*
** ATOP - System & Process Monitor
**
** The program 'atop' offers the possibility to view the activity of
** the system on system-level as well as process-level.
**
** This source-file contains various functions to a.o. format the
** time-of-day, the cpu-time consumption and the memory-occupation.
** ==========================================================================
** Author: Gerlof Langeveld
** E-mail: [email protected]
** Date: November 1996
** LINUX-port: June 2000
** --------------------------------------------------------------------------
** Copyright (C) 2000-2022 Gerlof Langeveld
**
** 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, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
** --------------------------------------------------------------------------
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <signal.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include "atop.h"
#include "acctproc.h"
int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
static unsigned long long getbootlinux(long);
/*
** Function convtime() converts a value (number of seconds since
** 1-1-1970) to an ascii-string in the format hh:mm:ss, stored in
** chartim (9 bytes long).
*/
char *
convtime(time_t utime, char *chartim)
{
struct tm *tt;
tt = localtime(&utime);
snprintf(chartim, 9, "%02d:%02d:%02d", tt->tm_hour, tt->tm_min, tt->tm_sec);
return chartim;
}
/*
** Function convdate() converts a value (number of seconds since
** 1-1-1970) to an ascii-string in the format yyyy/mm/dd, stored in
** chardat (11 bytes long).
*/
char *
convdate(time_t utime, char *chardat)
{
struct tm *tt;
tt = localtime(&utime);
snprintf(chardat, 11, "%04u/%02u/%02u",
(tt->tm_year+1900)%10000, (tt->tm_mon+1)%100, tt->tm_mday%100);
return chardat;
}
/*
** Convert a string in format [YYYYMMDD]hh[:]mm[:][ss] into an epoch time value or
** when only the value hh[:]mm was given, take this time from midnight.
**
** Arguments: String with date-time in format [YYYYMMDD]hh[:]mm[:][ss]
** or hh[:]mm[:][ss].
**
** Pointer to time_t containing 0 or current epoch time.
**
** Return-value: 0 - Wrong input-format
** 1 - Success
*/
int
getbranchtime(char *itim, time_t *newtime)
{
register int ilen = strlen(itim);
int hours, minutes, seconds;
time_t epoch;
struct tm tm;
memset(&tm, 0, sizeof tm);
/*
** verify length of input string
*/
if (ilen != 4 && ilen != 5 && // hhmm or hh:mm
ilen != 6 && ilen != 8 && // hhmmss or hh:mm:ss
ilen != 12 && ilen != 13 && // YYYYMMDDhhmm or YYYYMMDDhh:mm
ilen != 14 && ilen != 16) // YYYYMMDDhhmmss or YYYYMMDDhh:mm:ss
return 0; // wrong date-time format
/*
** check string syntax for absolute time specified as
** YYYYMMDDhh:mm:ss or YYYYMMDDhhmmss
*/
if ( sscanf(itim, "%4d%2d%2d%2d:%2d:%2d", &tm.tm_year, &tm.tm_mon,
&tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6 ||
sscanf(itim, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon,
&tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6 )
{
tm.tm_year -= 1900;
tm.tm_mon -= 1;
if (tm.tm_year < 100 || tm.tm_mon < 0 || tm.tm_mon > 11 ||
tm.tm_mday < 1 || tm.tm_mday > 31 ||
tm.tm_hour < 0 || tm.tm_hour > 23 ||
tm.tm_min < 0 || tm.tm_min > 59 ||
tm.tm_sec < 0 || tm.tm_sec > 59 )
{
return 0; // wrong date-time format
}
tm.tm_isdst = -1;
if ((epoch = mktime(&tm)) == -1)
return 0; // wrong date-time format
// correct date-time format
*newtime = epoch;
return 1;
}
/*
** check string syntax for absolute time specified as
** YYYYMMDDhh:mm or YYYYMMDDhhmm
*/
if ( sscanf(itim, "%4d%2d%2d%2d:%2d", &tm.tm_year, &tm.tm_mon,
&tm.tm_mday, &tm.tm_hour, &tm.tm_min) == 5 ||
sscanf(itim, "%4d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon,
&tm.tm_mday, &tm.tm_hour, &tm.tm_min) == 5 )
{
tm.tm_year -= 1900;
tm.tm_mon -= 1;
if (tm.tm_year < 100 || tm.tm_mon < 0 || tm.tm_mon > 11 ||
tm.tm_mday < 1 || tm.tm_mday > 31 ||
tm.tm_hour < 0 || tm.tm_hour > 23 ||
tm.tm_min < 0 || tm.tm_min > 59 )
{
return 0; // wrong date-time format
}
tm.tm_isdst = -1;
if ((epoch = mktime(&tm)) == -1)
return 0; // wrong date-time format
// correct date-time format
*newtime = epoch;
return 1;
}
/*
** check string syntax for relative time specified as
** hh:mm:ss or hhmmss
*/
if ( sscanf(itim, "%2d:%2d:%2d", &hours, &minutes, &seconds) == 3 ||
sscanf(itim, "%2d%2d%2d", &hours, &minutes, &seconds) == 3 )
{
if ( hours < 0 || hours > 23 ||
minutes < 0 || minutes > 59 ||
seconds < 0 || seconds > 59 )
return 0; // wrong date-time format
/*
** when the new time is already filled with an epoch time,
** the relative time will be on the same day as indicated by
** that epoch time
** when the new time is the time within a day or 0, the new
** time will be stored again as the time within a day.
*/
if (*newtime <= SECONDSINDAY) // time within the day?
{
*newtime = (hours * 3600) + (minutes * 60) + seconds;
if (*newtime >= SECONDSINDAY)
*newtime = SECONDSINDAY-1;
return 1;
}
else
{
*newtime = normalize_epoch(*newtime,
(hours*3600) + (minutes*60) + seconds);
return 1;
}
}
/*
** check string syntax for relative time specified as
** hh:mm or hhmm
*/
if ( sscanf(itim, "%2d:%2d", &hours, &minutes) == 2 ||
sscanf(itim, "%2d%2d", &hours, &minutes) == 2 )
{
if ( hours < 0 || hours > 23 || minutes < 0 || minutes > 59 )
return 0; // wrong date-time format
/*
** when the new time is already filled with an epoch time,
** the relative time will be on the same day as indicated by
** that epoch time
** when the new time is the time within a day or 0, the new
** time will be stored again as the time within a day.
*/
if (*newtime <= SECONDSINDAY) // time within the day?
{
*newtime = (hours * 3600) + (minutes * 60);
if (*newtime >= SECONDSINDAY)
*newtime = SECONDSINDAY-1;
return 1;
}
else
{
*newtime = normalize_epoch(*newtime,
(hours*3600) + (minutes*60));
return 1;
}
}
return 0; // wrong date-time format
}
/*
** Normalize an epoch time with the number of seconds within a day
** Return-value: Normalized epoch
*/
time_t
normalize_epoch(time_t epoch, long secondsinday)
{
struct tm tm;
localtime_r(&epoch, &tm); // convert epoch to tm
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = secondsinday;
tm.tm_isdst = -1;
return mktime(&tm); // convert tm to epoch
}
/*
** Function val2valstr() converts a positive value to an ascii-string of a
** fixed number of positions; if the value does not fit, it will be formatted
** to exponent-notation (as short as possible, so not via the standard printf-
** formatters %f or %e). The offered buffer should have a length of width+1.
** The value might even be printed as an average for the interval-time.
*/
char *
val2valstr(count_t value, char *strvalue, int width, int avg, int nsecs)
{
count_t maxval, remain = 0;
unsigned short exp = 0;
char *suffix = "";
int strsize = width+1;
if (avg && nsecs)
{
value = (value + (nsecs/2)) / nsecs; /* rounded value */
width = width - 2; /* subtract two positions for '/s' */
suffix = "/s";
}
if (value < 0) // no negative value expected
{
snprintf(strvalue, strsize, "%*s%s", width, "?", suffix);
return strvalue;
}
maxval = pow(10.0, width) - 1;
if (value < maxval)
{
snprintf(strvalue, strsize, "%*lld%s", width, value, suffix);
}
else
{
if (width < 3)
{
/*
** cannot avoid ignoring width
*/
snprintf(strvalue, strsize, "%lld%s", value, suffix);
}
else
{
/*
** calculate new maximum value for the string,
** calculating space for 'e' (exponent) + one digit
*/
width -= 2;
maxval = pow(10.0, width) - 1;
while (value > maxval)
{
exp++;
remain = value % 10;
value /= 10;
}
if (remain >= 5 && value < maxval)
value++;
snprintf(strvalue, strsize, "%*llde%hd%s",
width%100, value, exp%100, suffix);
}
}
return strvalue;
}
#define DAYSECS (24*60*60)
#define HOURSECS (60*60)
#define MINSECS (60)
/*
** Function val2elapstr() converts a value (number of seconds)
** to an ascii-string of up to max 13 positions in NNNdNNhNNmNNs
** stored in strvalue (at least 14 positions).
** returnvalue: number of bytes stored
*/
int
val2elapstr(int value, char *strvalue)
{
char *p = strvalue;
int rv, n = 14;
if (value >= DAYSECS)
{
rv = snprintf(p, n, "%dd", value/DAYSECS);
p += rv;
n -= rv;
}
if (value >= HOURSECS)
{
rv = snprintf(p, n, "%dh", (value%DAYSECS)/HOURSECS);
p += rv;
n -= rv;
}
if (value >= MINSECS)
{
rv = snprintf(p, n, "%dm", (value%HOURSECS)/MINSECS);
p += rv;
n -= rv;
}
rv = snprintf(p, n, "%ds", (value%MINSECS));
p += rv;
n -= rv;
return p - strvalue;
}
/*
** Function val2cpustr() converts a value (number of milliseconds)
** to an ascii-string of 6 positions in milliseconds or minute-seconds or
** hours-minutes, stored in strvalue (at least 7 positions).
*/
#define MAXMSEC (count_t)100000
#define MAXSEC (count_t)6000
#define MAXMIN (count_t)6000
char *
val2cpustr(count_t value, char *strvalue)
{
if (value < MAXMSEC)
{
snprintf(strvalue, 7, "%2llu.%02llus",
(value/1000)%100, value%1000/10);
}
else
{
/*
** millisecs irrelevant; round to seconds
*/
value = (value + 500) / 1000;
if (value < MAXSEC)
{
snprintf(strvalue, 7, "%2llum%02llus",
(value/60)%100, value%60);
}
else
{
/*
** seconds irrelevant; round to minutes
*/
value = (value + 30) / 60;
if (value < MAXMIN)
{
snprintf(strvalue, 7, "%2lluh%02llum",
(value/60)%100, value%60);
}
else
{
/*
** minutes irrelevant; round to hours
*/
value = (value + 30) / 60;
snprintf(strvalue, 7, "%2llud%02lluh",
(value/24)%100, value%24);
}
}
}
return strvalue;
}
/*
** Function val2Hzstr() converts a value (in MHz)
** to an ascii-string.
** The result-string is placed in the area pointed to strvalue,
** which should be able to contain 7 positions plus null byte.
*/
char *
val2Hzstr(count_t value, char *strvalue)
{
char *fformat;
if (value < 1000)
{
snprintf(strvalue, 8, "%4lluMHz", value%10000);
}
else
{
double fval=value/1000.0; // fval is double in GHz
char prefix='G';
if (fval >= 1000.0) // prepare for the future
{
prefix='T';
fval /= 1000.0;
}
if (fval < 10.0)
{
fformat = "%4.2f%cHz";
}
else
{
if (fval < 100.0)
fformat = "%4.1f%cHz";
else
fformat = "%4.0f%cHz";
}
snprintf(strvalue, 8, fformat, fval, prefix);
}
return strvalue;
}
/*
** Function val2memstr() converts a value (number of bytes)
** to an ascii-string in a specific format (indicated by pformat).
** The result-string is placed in the area pointed to strvalue,
** which should be able to contain at least 7 positions.
*/
#define ONEKBYTE 1024
#define ONEMBYTE 1048576
#define ONEGBYTE 1073741824L
#define ONETBYTE 1099511627776LL
#define ONEPBYTE 1125899906842624LL
#define ONEEBYTE 1152921504606846976LL
#define MAXLONGLONG 9223372036854775807LL
#define MAXBYTE 999
#define MAXKBYTE (ONEKBYTE*999L)
#define MAXKBYTE9 (ONEKBYTE*9L)
#define MAXMBYTE (ONEMBYTE*999L)
#define MAXMBYTE9 (ONEMBYTE*9L)
#define MAXGBYTE (ONEGBYTE*999LL)
#define MAXGBYTE9 (ONEGBYTE*9LL)
#define MAXTBYTE (ONETBYTE*999LL)
#define MAXTBYTE9 (ONETBYTE*9LL)
#define MAXPBYTE (ONEPBYTE*999LL)
#define MAXPBYTE9 (ONEPBYTE*9LL)
#define MAXEBYTE (ONEEBYTE*999LL)
#define MAXEBYTE8 (ONEEBYTE*7LL+(ONEEBYTE-1))
char *
val2memstr(count_t value, char *strvalue, int pformat, int avgval, int nsecs)
{
char aformat; /* advised format */
count_t verifyval;
char *suffix = "";
int basewidth = 6;
/*
** notice that the value can be negative, in which case the
** modulo-value should be evaluated and an extra position should
** be reserved for the sign
*/
if (value < 0)
verifyval = -value * 10;
else
verifyval = value;
/*
** verify if printed value is required per second (average) or total
*/
if (avgval && nsecs)
{
value = llround((double)((double)value/(double)nsecs));
verifyval = llround((double)((double)verifyval/(double)nsecs));
basewidth -= 2;
suffix = "/s";
if (verifyval <= MAXBYTE) /* bytes ? */
aformat = BFORMAT;
else
if (verifyval <= MAXKBYTE9) /* kbytes 1-9 ? */
aformat = KBFORMAT;
else
if (verifyval <= MAXKBYTE) /* kbytes ? */
aformat = KBFORMAT_INT;
else
if (verifyval <= MAXMBYTE9) /* mbytes 1-9 ? */
aformat = MBFORMAT;
else
if (verifyval <= MAXMBYTE) /* mbytes 10-999 ? */
aformat = MBFORMAT_INT;
else
if (verifyval <= MAXGBYTE9) /* gbytes 1-9 ? */
aformat = GBFORMAT;
else
if (verifyval <= MAXGBYTE) /* gbytes 10-999 ? */
aformat = GBFORMAT_INT;
else
if (verifyval <= MAXTBYTE9) /* tbytes 1-9 ? */
aformat = TBFORMAT;
else
if (verifyval <= MAXTBYTE) /* tbytes 10-999? */
aformat = TBFORMAT_INT;
else
if (verifyval <= MAXPBYTE9) /* pbytes 1-9 ? */
aformat = PBFORMAT;
else
if (verifyval <= MAXPBYTE) /* pbytes 10-999 ? */
aformat = PBFORMAT_INT;
else
if (verifyval <= MAXEBYTE8) /* ebytes 1-8 ? */
aformat = EBFORMAT;
else
aformat = OVFORMAT; /* max long long */
} else
/*
** printed value per interval (normal mode)
*/
{
/*
** determine which format will be used on bases of the value itself
*/
if (verifyval <= MAXBYTE) /* bytes ? */
aformat = BFORMAT;
else
if (verifyval <= MAXKBYTE) /* kbytes ? */
aformat = KBFORMAT;
else
if (verifyval <= MAXMBYTE) /* mbytes ? */
aformat = MBFORMAT;
else
if (verifyval <= MAXGBYTE) /* gbytes ? */
aformat = GBFORMAT;
else
if (verifyval <= MAXTBYTE) /* tbytes? */
aformat = TBFORMAT;
else
if (verifyval <= MAXPBYTE) /* pbytes? */
aformat = PBFORMAT;
else
aformat = EBFORMAT; /* ebytes! */
}
/*
** check if this is also the preferred format
*/
if (aformat <= pformat)
aformat = pformat;
switch (aformat)
{
case BFORMAT:
snprintf(strvalue, 7, "%*lldB%s",
basewidth-1, value, suffix);
break;
case KBFORMAT:
snprintf(strvalue, 7, "%*.1lfK%s",
basewidth-1, (double)((double)value/ONEKBYTE), suffix);
break;
case KBFORMAT_INT:
snprintf(strvalue, 7, "%*lldK%s",
basewidth-1, llround((double)((double)value/ONEKBYTE)), suffix);
break;
case MBFORMAT:
snprintf(strvalue, 7, "%*.1lfM%s",
basewidth-1, (double)((double)value/ONEMBYTE), suffix);
break;
case MBFORMAT_INT:
snprintf(strvalue, 7, "%*lldM%s",
basewidth-1, llround((double)((double)value/ONEMBYTE)), suffix);
break;
case GBFORMAT:
snprintf(strvalue, 7, "%*.1lfG%s",
basewidth-1, (double)((double)value/ONEGBYTE), suffix);
break;
case GBFORMAT_INT:
snprintf(strvalue, 7, "%*lldG%s",
basewidth-1, llround((double)((double)value/ONEGBYTE)), suffix);
break;
case TBFORMAT:
snprintf(strvalue, 7, "%*.1lfT%s",
basewidth-1, (double)((double)value/ONETBYTE), suffix);
break;
case TBFORMAT_INT:
snprintf(strvalue, 7, "%*lldT%s",
basewidth-1, llround((double)((double)value/ONETBYTE)), suffix);
break;
case PBFORMAT:
snprintf(strvalue, 7, "%*.1lfP%s",
basewidth-1, (double)((double)value/ONEPBYTE), suffix);
break;
case PBFORMAT_INT:
snprintf(strvalue, 7, "%*lldP%s",
basewidth-1, llround((double)((double)value/ONEPBYTE)), suffix);
break;
case EBFORMAT:
snprintf(strvalue, 7, "%*.1lfE%s",
basewidth-1, (double)((double)value/ONEEBYTE), suffix);
break;
default:
snprintf(strvalue, 7, "OVFLOW");
}
// check if overflow occurred during the formatting
// by checking the last byte of the formatted string
//
switch ( *(strvalue+5) )
{
case 's': // in case of per-second value
case 'B':
case 'K':
case 'M':
case 'G':
case 'T':
case 'P':
case 'E':
break;
default:
snprintf(strvalue, 7, "OVFLOW");
}
return strvalue;
}
/*
** Function numeric() checks if the ascii-string contains
** a numeric (positive) value.
** Returns 1 (true) if so, or 0 (false).
*/
int
numeric(char *ns)
{
register char *s = ns;
while (*s)
if (*s < '0' || *s > '9')
return(0); /* false */
else
s++;
return(1); /* true */
}
/*
** Function getboot() returns the boot-time of this system
** as number of jiffies since 1-1-1970.
*/
unsigned long long
getboot(void)
{
static unsigned long long boottime;
if (!boottime) /* do this only once */
boottime = getbootlinux(hertz);
return boottime;
}
/*
** LINUX SPECIFIC:
** Determine boot-time of this system (as number of jiffies since 1-1-1970).
*/
static unsigned long long
getbootlinux(long hertz)
{
int cpid;
char tmpbuf[1280];
FILE *fp;
unsigned long startticks;
unsigned long long bootjiffies = 0;
struct timespec ts;
/*
** dirty hack to get the boottime, since the
** Linux 2.6 kernel (2.6.5) does not return a proper
** boottime-value with the times() system call :-(
*/
if ( (cpid = fork()) == 0 )
{
/*
** child just waiting to be killed by parent
*/
pause();
}
else
{
/*
** parent determines start-time (in jiffies since boot)
** of the child and calculates the boottime in jiffies
** since 1-1-1970
*/
(void) clock_gettime(CLOCK_REALTIME, &ts); // get current
bootjiffies = 1LL * ts.tv_sec * hertz +
1LL * ts.tv_nsec * hertz / 1000000000LL;
snprintf(tmpbuf, sizeof tmpbuf, "/proc/%d/stat", cpid);
if ( (fp = fopen(tmpbuf, "r")) != NULL)
{
if ( fscanf(fp, "%*d (%*[^)]) %*c %*d %*d %*d %*d "
"%*d %*d %*d %*d %*d %*d %*d %*d "
"%*d %*d %*d %*d %*d %*d %lu",
&startticks) == 1)
{
bootjiffies -= startticks;
}
fclose(fp);
}
/*
** kill the child and get rid of the zombie
*/
kill(cpid, SIGKILL);
(void) wait((int *)0);
}
return bootjiffies;
}
/*
** generic pointer verification after malloc
*/
void
ptrverify(const void *ptr, const char *errormsg, ...)
{
if (!ptr)
{
va_list args;
acctswoff();
netatop_signoff();
generic_end();
va_start(args, errormsg);
vfprintf(stderr, errormsg, args);
va_end(args);
exit(13);
}
}
/*
** cleanup, give error message and exit
*/
void
mcleanstop(int exitcode, const char *errormsg, ...)
{
va_list args;
acctswoff();
netatop_signoff();
generic_end();
va_start(args, errormsg);
vfprintf(stderr, errormsg, args);
va_end(args);
exit(exitcode);
}
/*
** cleanup and exit
*/
void
cleanstop(int exitcode)
{
acctswoff();
netatop_signoff();
generic_end();
exit(exitcode);
}
/*
** determine if we are running with root privileges
** returns: boolean
*/
int
rootprivs(void)
{
uid_t ruid, euid, suid;
getresuid(&ruid, &euid, &suid);
return !suid;
}
/*
** drop the root privileges that might be obtained via setuid-bit
**
** this action may only fail with errno EPERM (normal situation when
** atop has not been started with setuid-root privs); when this
** action fails with EAGAIN or ENOMEM, atop should not continue
** without root privs being dropped...
*/
int
droprootprivs(void)
{
if (seteuid( getuid() ) == -1 && errno != EPERM)
return 0; /* false */
else
return 1; /* true */
}
/*
** regain the root privileges that might be dropped earlier
*/
void
regainrootprivs(void)
{
int liResult;
// this will fail for non-privileged processes
liResult = seteuid(0);
if (liResult != 0)
{
}
}
/*
** try to set the highest OOM priority
*/
void
set_oom_score_adj(void)
{
int fd;
char val[] = "-999"; /* suggested by Gerlof, always set -999 */
/*
** set OOM score adj to avoid to lost necessary log of system.
** ignored if not running under superuser privileges!
*/
fd = open("/proc/self/oom_score_adj", O_RDWR);
if ( fd < 0 ) {
return;
}
if ( write(fd, val, strlen(val)) < 0 )
;
close(fd);
}
/* hypervisor enum, move this into header if actually in use */
enum {
HYPER_NONE = 0,
HYPER_XEN,
HYPER_KVM,
HYPER_MSHV,
HYPER_VMWARE,
HYPER_IBM,
HYPER_VSERVER,
HYPER_UML,
HYPER_INNOTEK,
HYPER_HITACHI,
HYPER_PARALLELS,
HYPER_VBOX,
HYPER_OS400,
HYPER_PHYP,
HYPER_SPAR,
HYPER_WSL,
};
#if defined(__x86_64__) || defined(__i386__)
#define HYPERVISOR_INFO_LEAF 0x40000000
static inline void
x86_cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
__asm__(
#if defined(__PIC__) && defined(__i386__)
"xchg %%ebx, %%esi;"
"cpuid;"
"xchg %%esi, %%ebx;"
: "=S" (*ebx),
#else
"cpuid;"
: "=b" (*ebx),
#endif
"=a" (*eax),
"=c" (*ecx),
"=d" (*edx)
: "1" (op), "c"(0));
}
static int
get_hypervisor(void)
{
unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0, hyper = HYPER_NONE;
char hyper_vendor_id[13];
memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id));
x86_cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx);
memcpy(hyper_vendor_id + 0, &ebx, 4);
memcpy(hyper_vendor_id + 4, &ecx, 4);
memcpy(hyper_vendor_id + 8, &edx, 4);
hyper_vendor_id[12] = '\0';
if (!hyper_vendor_id[0])
return hyper;
if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12))