Skip to content

Commit 949c0a1

Browse files
committed
Better handling of get_activity_position() return value
Function get_activity_position() can return -1 on error, ie. when corresponding activity hasn't been found in array. This return code was sometimes not tested because we knew that the activity existed. So we directly used the return code as array index. Strengthen things now: If such an activity is not found then display an internal error message and exit. Signed-off-by: Sebastien GODARD <[email protected]>
1 parent 076eccb commit 949c0a1

File tree

9 files changed

+72
-89
lines changed

9 files changed

+72
-89
lines changed

common.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,21 +602,19 @@ unsigned long long get_per_cpu_interval(struct stats_cpu *scc,
602602

603603
/*
604604
***************************************************************************
605-
* Unhandled situation: Panic and exit.
605+
* Unhandled situation: Panic and exit. Should never happen.
606606
*
607607
* IN:
608608
* @function Function name where situation occured.
609609
* @error_code Error code.
610610
***************************************************************************
611611
*/
612-
#ifdef DEBUG
613612
void sysstat_panic(const char *function, int error_code)
614613
{
615-
fprintf(stderr, "sysstat: %s[%d]: Last chance handler...\n",
614+
fprintf(stderr, "sysstat: %s[%d]: Internal error...\n",
616615
function, error_code);
617616
exit(1);
618617
}
619-
#endif
620618

621619
/*
622620
***************************************************************************

common.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@
123123

124124
#define MINIMUM(a,b) ((a) < (b) ? (a) : (b))
125125

126-
#ifdef DEBUG
127126
#define PANIC(m) sysstat_panic(__FUNCTION__, m)
128-
#else
129-
#define PANIC(m)
130-
#endif
131127

132128
/* Number of ticks per second */
133129
#define HZ hz
@@ -211,9 +207,7 @@ extern void
211207
print_version(void);
212208
extern char *
213209
strtolower(char *);
214-
#ifdef DEBUG
215210
extern void
216211
sysstat_panic(const char *, int);
217-
#endif
218212

219213
#endif /* _COMMON_H */

sa.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@
6666

6767

6868
/* Macro used to flag an activity that should be collected */
69-
#define COLLECT_ACTIVITY(m) act[get_activity_position(act, m)]->options |= AO_COLLECTED
69+
#define COLLECT_ACTIVITY(m) act[get_activity_position(act, m, EXIT_IF_NOT_FOUND)]->options |= AO_COLLECTED
7070

7171
/* Macro used to flag an activity that should be selected */
72-
#define SELECT_ACTIVITY(m) act[get_activity_position(act, m)]->options |= AO_SELECTED
72+
#define SELECT_ACTIVITY(m) act[get_activity_position(act, m, EXIT_IF_NOT_FOUND)]->options |= AO_SELECTED
7373

7474

7575
/*
@@ -208,16 +208,18 @@
208208
#define MAX_ARGV_NR 32
209209

210210
/* Miscellaneous constants */
211-
#define USE_SADC 0
212-
#define USE_SA_FILE 1
213-
#define NO_TM_START 0
214-
#define NO_TM_END 0
215-
#define NO_RESET 0
216-
#define NON_FATAL 0
217-
#define FATAL 1
218-
#define C_SAR 0
219-
#define C_SADF 1
220-
#define ALL_ACTIVITIES ~0U
211+
#define USE_SADC 0
212+
#define USE_SA_FILE 1
213+
#define NO_TM_START 0
214+
#define NO_TM_END 0
215+
#define NO_RESET 0
216+
#define NON_FATAL 0
217+
#define FATAL 1
218+
#define C_SAR 0
219+
#define C_SADF 1
220+
#define ALL_ACTIVITIES ~0U
221+
#define EXIT_IF_NOT_FOUND 1
222+
#define RESUME_IF_NOT_FOUND 0
221223

222224
#define SOFT_SIZE 0
223225
#define HARD_SIZE 1
@@ -866,7 +868,7 @@ extern void
866868
extern int
867869
get_activity_nr(struct activity * [], unsigned int, int);
868870
extern int
869-
get_activity_position(struct activity * [], unsigned int);
871+
get_activity_position(struct activity * [], unsigned int, int);
870872
extern char *
871873
get_devname(unsigned int, unsigned int, int);
872874
extern void

sa_common.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -913,26 +913,28 @@ void free_bitmaps(struct activity *act[])
913913
* IN:
914914
* @act Array of activities.
915915
* @act_flag Activity flag to look for.
916+
* @stop TRUE if sysstat should exit when activity is not found.
916917
*
917918
* RETURNS:
918919
* Position of activity in array, or -1 if not found (this may happen when
919920
* reading data from a system activity file created by another version of
920921
* sysstat).
921922
***************************************************************************
922923
*/
923-
int get_activity_position(struct activity *act[], unsigned int act_flag)
924+
int get_activity_position(struct activity *act[], unsigned int act_flag, int stop)
924925
{
925926
int i;
926927

927928
for (i = 0; i < NR_ACT; i++) {
928929
if (act[i]->id == act_flag)
929-
break;
930+
return i;
930931
}
931932

932-
if (i == NR_ACT)
933-
return -1;
933+
if (stop) {
934+
PANIC((int) act_flag);
935+
}
934936

935-
return i;
937+
return -1;
936938
}
937939

938940
/*
@@ -1010,7 +1012,7 @@ void select_default_activity(struct activity *act[])
10101012
{
10111013
int p;
10121014

1013-
p = get_activity_position(act, A_CPU);
1015+
p = get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND);
10141016

10151017
/* Default is CPU activity... */
10161018
if (!get_activity_nr(act, AO_SELECTED, COUNT_ACTIVITIES)) {
@@ -1162,8 +1164,8 @@ void copy_structures(struct activity *act[], unsigned int id_seq[],
11621164
if (!id_seq[i])
11631165
continue;
11641166

1165-
if (((p = get_activity_position(act, id_seq[i])) < 0) ||
1166-
(act[p]->nr < 1) || (act[p]->nr2 < 1)) {
1167+
p = get_activity_position(act, id_seq[i], EXIT_IF_NOT_FOUND);
1168+
if ((act[p]->nr < 1) || (act[p]->nr2 < 1)) {
11671169
PANIC(1);
11681170
}
11691171

@@ -1193,7 +1195,7 @@ void read_file_stat_bunch(struct activity *act[], int curr, int ifd, int act_nr,
11931195

11941196
for (i = 0; i < act_nr; i++, fal++) {
11951197

1196-
if (((p = get_activity_position(act, fal->id)) < 0) ||
1198+
if (((p = get_activity_position(act, fal->id, RESUME_IF_NOT_FOUND)) < 0) ||
11971199
(act[p]->magic != fal->magic)) {
11981200
/*
11991201
* Ignore current activity in file, which is unknown to
@@ -1221,7 +1223,7 @@ void read_file_stat_bunch(struct activity *act[], int curr, int ifd, int act_nr,
12211223
sa_fread(ifd, act[p]->buf[curr], act[p]->fsize * act[p]->nr * act[p]->nr2, HARD_SIZE);
12221224
}
12231225
else {
1224-
PANIC(act[p]->nr);
1226+
PANIC(p);
12251227
}
12261228
}
12271229
}
@@ -1337,7 +1339,7 @@ void check_file_actlst(int *ifd, char *dfile, struct activity *act[],
13371339
handle_invalid_sa_file(ifd, file_magic, dfile, 0);
13381340
}
13391341

1340-
if ((p = get_activity_position(act, fal->id)) < 0)
1342+
if ((p = get_activity_position(act, fal->id, RESUME_IF_NOT_FOUND)) < 0)
13411343
/* Unknown activity */
13421344
continue;
13431345

@@ -1440,7 +1442,7 @@ int reallocate_vol_act_structures(struct activity *act[], unsigned int act_nr,
14401442
{
14411443
int j, p;
14421444

1443-
if ((p = get_activity_position(act, act_id)) < 0)
1445+
if ((p = get_activity_position(act, act_id, RESUME_IF_NOT_FOUND)) < 0)
14441446
/* Ignore unknown activity */
14451447
return -1;
14461448

@@ -1538,15 +1540,15 @@ int parse_sar_opt(char *argv[], int *opt, struct activity *act[],
15381540

15391541
/* Force '-P ALL -I XALL -r ALL -u ALL' */
15401542

1541-
p = get_activity_position(act, A_MEMORY);
1543+
p = get_activity_position(act, A_MEMORY, EXIT_IF_NOT_FOUND);
15421544
act[p]->opt_flags |= AO_F_MEM_AMT + AO_F_MEM_DIA +
15431545
AO_F_MEM_SWAP + AO_F_MEM_ALL;
15441546

1545-
p = get_activity_position(act, A_IRQ);
1547+
p = get_activity_position(act, A_IRQ, EXIT_IF_NOT_FOUND);
15461548
set_bitmap(act[p]->bitmap->b_array, ~0,
15471549
BITMAP_SIZE(act[p]->bitmap->b_size));
15481550

1549-
p = get_activity_position(act, A_CPU);
1551+
p = get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND);
15501552
set_bitmap(act[p]->bitmap->b_array, ~0,
15511553
BITMAP_SIZE(act[p]->bitmap->b_size));
15521554
act[p]->opt_flags = AO_F_CPU_ALL;
@@ -1573,7 +1575,7 @@ int parse_sar_opt(char *argv[], int *opt, struct activity *act[],
15731575
break;
15741576

15751577
case 'H':
1576-
p = get_activity_position(act, A_HUGE);
1578+
p = get_activity_position(act, A_HUGE, EXIT_IF_NOT_FOUND);
15771579
act[p]->options |= AO_SELECTED;
15781580
break;
15791581

@@ -1611,7 +1613,7 @@ int parse_sar_opt(char *argv[], int *opt, struct activity *act[],
16111613
break;
16121614

16131615
case 'r':
1614-
p = get_activity_position(act, A_MEMORY);
1616+
p = get_activity_position(act, A_MEMORY, EXIT_IF_NOT_FOUND);
16151617
act[p]->options |= AO_SELECTED;
16161618
act[p]->opt_flags |= AO_F_MEM_AMT;
16171619
if (!*(argv[*opt] + i + 1) && argv[*opt + 1] && !strcmp(argv[*opt + 1], K_ALL)) {
@@ -1622,13 +1624,13 @@ int parse_sar_opt(char *argv[], int *opt, struct activity *act[],
16221624
break;
16231625

16241626
case 'R':
1625-
p = get_activity_position(act, A_MEMORY);
1627+
p = get_activity_position(act, A_MEMORY, EXIT_IF_NOT_FOUND);
16261628
act[p]->options |= AO_SELECTED;
16271629
act[p]->opt_flags |= AO_F_MEM_DIA;
16281630
break;
16291631

16301632
case 'S':
1631-
p = get_activity_position(act, A_MEMORY);
1633+
p = get_activity_position(act, A_MEMORY, EXIT_IF_NOT_FOUND);
16321634
act[p]->options |= AO_SELECTED;
16331635
act[p]->opt_flags |= AO_F_MEM_SWAP;
16341636
break;
@@ -1648,7 +1650,7 @@ int parse_sar_opt(char *argv[], int *opt, struct activity *act[],
16481650
break;
16491651

16501652
case 'u':
1651-
p = get_activity_position(act, A_CPU);
1653+
p = get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND);
16521654
act[p]->options |= AO_SELECTED;
16531655
if (!*(argv[*opt] + i + 1) && argv[*opt + 1] && !strcmp(argv[*opt + 1], K_ALL)) {
16541656
(*opt)++;
@@ -1866,7 +1868,7 @@ int parse_sar_I_opt(char *argv[], int *opt, struct activity *act[])
18661868
char *t;
18671869

18681870
/* Select interrupt activity */
1869-
p = get_activity_position(act, A_IRQ);
1871+
p = get_activity_position(act, A_IRQ, EXIT_IF_NOT_FOUND);
18701872
act[p]->options |= AO_SELECTED;
18711873

18721874
for (t = strtok(argv[*opt], ","); t; t = strtok(NULL, ",")) {
@@ -1924,7 +1926,7 @@ int parse_sa_P_opt(char *argv[], int *opt, unsigned int *flags, struct activity
19241926
int i, p;
19251927
char *t;
19261928

1927-
p = get_activity_position(act, A_CPU);
1929+
p = get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND);
19281930

19291931
if (argv[++(*opt)]) {
19301932

sa_conv.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ int upgrade_header_section(char dfile[], int fd, int stdfd,
488488
return -1;
489489
}
490490

491-
if ((p = get_activity_position(act, fal->id)) >= 0) {
491+
if ((p = get_activity_position(act, fal->id, RESUME_IF_NOT_FOUND)) >= 0) {
492492
/* This is a known activity, maybe with an unknown format */
493493

494494
if (IS_VOLATILE(act[p]->options) && previous_format) {
@@ -597,7 +597,7 @@ int upgrade_activity_section(int stdfd, struct activity *act[],
597597
for (i = 0; i < file_hdr->sa_act_nr; i++, fal++) {
598598

599599
file_act = *fal;
600-
if ((p = get_activity_position(act, fal->id)) >= 0) {
600+
if ((p = get_activity_position(act, fal->id, RESUME_IF_NOT_FOUND)) >= 0) {
601601
/* Update activity magic number */
602602
file_act.magic = act[p]->magic;
603603
/* Also update its size, which may have changed with recent versions */
@@ -693,7 +693,7 @@ int upgrade_restart_record(int fd, int stdfd, struct activity *act[],
693693
memset(&file_act, 0, FILE_ACTIVITY_SIZE);
694694

695695
/* Old format: Sequence of volatile activities is in vol_id_seq */
696-
p = get_activity_position(act, vol_id_seq[i]);
696+
p = get_activity_position(act, vol_id_seq[i], EXIT_IF_NOT_FOUND);
697697

698698
/* Set only the necessary fields */
699699
file_act.id = act[p]->id;
@@ -742,7 +742,7 @@ int upgrade_common_record(int fd, int stdfd, struct activity *act[],
742742
fal = file_actlst;
743743
for (i = 0; i < file_hdr->sa_act_nr; i++, fal++) {
744744

745-
if ((p = get_activity_position(act, fal->id)) < 0) {
745+
if ((p = get_activity_position(act, fal->id, RESUME_IF_NOT_FOUND)) < 0) {
746746
/* An unknown activity should still be read and written */
747747
size = (size_t) fal->size * (size_t) fal->nr * (size_t) fal->nr2;
748748
SREALLOC(buffer, void, size);

sadc.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ void setup_file_hdr(int fd)
506506
* A_CPU activity is always collected, hence its number of items is
507507
* always counted (in sa_sys_init()).
508508
*/
509-
file_hdr.sa_last_cpu_nr = act[get_activity_position(act, A_CPU)]->nr;
509+
file_hdr.sa_last_cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr;
510510

511511
/* Get system name, release number, hostname and machine architecture */
512512
uname(&header);
@@ -532,7 +532,7 @@ void setup_file_hdr(int fd)
532532
*/
533533
if (!id_seq[i])
534534
continue;
535-
if ((p = get_activity_position(act, id_seq[i])) < 0)
535+
if ((p = get_activity_position(act, id_seq[i], RESUME_IF_NOT_FOUND)) < 0)
536536
continue;
537537

538538
if (IS_COLLECTED(act[p]->options)) {
@@ -592,7 +592,7 @@ void write_vol_act_structures(int ofd)
592592
file_act.id = file_act.nr = 0;
593593
}
594594
else {
595-
p = get_activity_position(act, vol_id_seq[i]);
595+
p = get_activity_position(act, vol_id_seq[i], EXIT_IF_NOT_FOUND);
596596

597597
/*
598598
* All the fields in file_activity structure are not used.
@@ -692,7 +692,7 @@ void write_stats(int ofd)
692692

693693
if (!id_seq[i])
694694
continue;
695-
if ((p = get_activity_position(act, id_seq[i])) < 0)
695+
if ((p = get_activity_position(act, id_seq[i], RESUME_IF_NOT_FOUND)) < 0)
696696
continue;
697697

698698
if (IS_COLLECTED(act[p]->options)) {
@@ -910,7 +910,7 @@ void open_ofile(int *ofd, char ofile[], int restart_mark)
910910
handle_invalid_sa_file(ofd, &file_magic, ofile, 0);
911911
}
912912

913-
p = get_activity_position(act, file_act[i].id);
913+
p = get_activity_position(act, file_act[i].id, RESUME_IF_NOT_FOUND);
914914

915915
if ((p < 0) || (act[p]->fsize != file_act[i].size) ||
916916
(act[p]->magic != file_act[i].magic))
@@ -940,7 +940,7 @@ void open_ofile(int *ofd, char ofile[], int restart_mark)
940940

941941
for (i = 0; i < file_hdr.sa_act_nr; i++) {
942942

943-
p = get_activity_position(act, file_act[i].id);
943+
p = get_activity_position(act, file_act[i].id, EXIT_IF_NOT_FOUND);
944944

945945
/*
946946
* Force number of items (serial lines, network interfaces...)
@@ -970,7 +970,7 @@ void open_ofile(int *ofd, char ofile[], int restart_mark)
970970
vol_id_seq[j++] = 0;
971971
}
972972

973-
p = get_activity_position(act, A_CPU);
973+
p = get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND);
974974
if (!IS_COLLECTED(act[p]->options)) {
975975
/* A_CPU activity should always exist in file */
976976
goto append_error;
@@ -1020,7 +1020,7 @@ void open_ofile(int *ofd, char ofile[], int restart_mark)
10201020
void read_stats(void)
10211021
{
10221022
int i;
1023-
__nr_t cpu_nr = act[get_activity_position(act, A_CPU)]->nr;
1023+
__nr_t cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr;
10241024

10251025
/*
10261026
* Init uptime0. So if /proc/uptime cannot fill it,

sadf.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,7 @@ void main_display_loop(int ifd, struct file_activity *file_actlst, __nr_t cpu_nr
13781378
if (!id_seq[i])
13791379
continue;
13801380

1381-
if ((p = get_activity_position(act, id_seq[i])) < 0) {
1382-
/* Should never happen */
1383-
PANIC(1);
1384-
}
1381+
p = get_activity_position(act, id_seq[i], EXIT_IF_NOT_FOUND);
13851382
if (!IS_SELECTED(act[p]->options))
13861383
continue;
13871384

@@ -1466,7 +1463,7 @@ void read_stats_from_file(char dfile[])
14661463
&file_actlst, id_seq, ignore);
14671464

14681465
/* Now pick up number of proc for this file */
1469-
cpu_nr = act[get_activity_position(act, A_CPU)]->nr;
1466+
cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr;
14701467

14711468
if (DISPLAY_HDR_ONLY(flags)) {
14721469
if (*fmt[f_position]->f_header) {

0 commit comments

Comments
 (0)