diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_apt.c | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c index 16c34ca..288d37c 100644 --- a/plugins/check_apt.c +++ b/plugins/check_apt.c | |||
@@ -103,8 +103,9 @@ int main(int argc, char **argv) { | |||
103 | /* Parse extra opts if any */ | 103 | /* Parse extra opts if any */ |
104 | argv = np_extra_opts(&argc, argv, progname); | 104 | argv = np_extra_opts(&argc, argv, progname); |
105 | 105 | ||
106 | if (process_arguments(argc, argv) == ERROR) | 106 | if (process_arguments(argc, argv) == ERROR) { |
107 | usage_va(_("Could not parse arguments")); | 107 | usage_va(_("Could not parse arguments")); |
108 | } | ||
108 | 109 | ||
109 | /* Set signal handling and alarm timeout */ | 110 | /* Set signal handling and alarm timeout */ |
110 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 111 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
@@ -115,8 +116,9 @@ int main(int argc, char **argv) { | |||
115 | alarm(timeout_interval); | 116 | alarm(timeout_interval); |
116 | 117 | ||
117 | /* if they want to run apt-get update first... */ | 118 | /* if they want to run apt-get update first... */ |
118 | if (do_update) | 119 | if (do_update) { |
119 | result = run_update(); | 120 | result = run_update(); |
121 | } | ||
120 | 122 | ||
121 | /* apt-get upgrade */ | 123 | /* apt-get upgrade */ |
122 | result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list)); | 124 | result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list)); |
@@ -138,12 +140,14 @@ int main(int argc, char **argv) { | |||
138 | qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp); | 140 | qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp); |
139 | qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp); | 141 | qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp); |
140 | 142 | ||
141 | for (int i = 0; i < sec_count; i++) | 143 | for (int i = 0; i < sec_count; i++) { |
142 | printf("%s (security)\n", secpackages_list[i]); | 144 | printf("%s (security)\n", secpackages_list[i]); |
145 | } | ||
143 | 146 | ||
144 | if (only_critical == false) { | 147 | if (only_critical == false) { |
145 | for (int i = 0; i < packages_available - sec_count; i++) | 148 | for (int i = 0; i < packages_available - sec_count; i++) { |
146 | printf("%s\n", packages_list[i]); | 149 | printf("%s\n", packages_list[i]); |
150 | } | ||
147 | } | 151 | } |
148 | } | 152 | } |
149 | 153 | ||
@@ -174,8 +178,9 @@ int process_arguments(int argc, char **argv) { | |||
174 | while (1) { | 178 | while (1) { |
175 | c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); | 179 | c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); |
176 | 180 | ||
177 | if (c == -1 || c == EOF || c == 1) | 181 | if (c == -1 || c == EOF || c == 1) { |
178 | break; | 182 | break; |
183 | } | ||
179 | 184 | ||
180 | switch (c) { | 185 | switch (c) { |
181 | case 'h': | 186 | case 'h': |
@@ -194,16 +199,18 @@ int process_arguments(int argc, char **argv) { | |||
194 | upgrade = DIST_UPGRADE; | 199 | upgrade = DIST_UPGRADE; |
195 | if (optarg != NULL) { | 200 | if (optarg != NULL) { |
196 | upgrade_opts = strdup(optarg); | 201 | upgrade_opts = strdup(optarg); |
197 | if (upgrade_opts == NULL) | 202 | if (upgrade_opts == NULL) { |
198 | die(STATE_UNKNOWN, "strdup failed"); | 203 | die(STATE_UNKNOWN, "strdup failed"); |
204 | } | ||
199 | } | 205 | } |
200 | break; | 206 | break; |
201 | case 'U': | 207 | case 'U': |
202 | upgrade = UPGRADE; | 208 | upgrade = UPGRADE; |
203 | if (optarg != NULL) { | 209 | if (optarg != NULL) { |
204 | upgrade_opts = strdup(optarg); | 210 | upgrade_opts = strdup(optarg); |
205 | if (upgrade_opts == NULL) | 211 | if (upgrade_opts == NULL) { |
206 | die(STATE_UNKNOWN, "strdup failed"); | 212 | die(STATE_UNKNOWN, "strdup failed"); |
213 | } | ||
207 | } | 214 | } |
208 | break; | 215 | break; |
209 | case 'n': | 216 | case 'n': |
@@ -213,8 +220,9 @@ int process_arguments(int argc, char **argv) { | |||
213 | do_update = true; | 220 | do_update = true; |
214 | if (optarg != NULL) { | 221 | if (optarg != NULL) { |
215 | update_opts = strdup(optarg); | 222 | update_opts = strdup(optarg); |
216 | if (update_opts == NULL) | 223 | if (update_opts == NULL) { |
217 | die(STATE_UNKNOWN, "strdup failed"); | 224 | die(STATE_UNKNOWN, "strdup failed"); |
225 | } | ||
218 | } | 226 | } |
219 | break; | 227 | break; |
220 | case 'l': | 228 | case 'l': |
@@ -264,8 +272,9 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
264 | /* initialize ereg as it is possible it is printed while uninitialized */ | 272 | /* initialize ereg as it is possible it is printed while uninitialized */ |
265 | memset(&ereg, '\0', sizeof(ereg.buffer)); | 273 | memset(&ereg, '\0', sizeof(ereg.buffer)); |
266 | 274 | ||
267 | if (upgrade == NO_UPGRADE) | 275 | if (upgrade == NO_UPGRADE) { |
268 | return STATE_OK; | 276 | return STATE_OK; |
277 | } | ||
269 | 278 | ||
270 | /* compile the regexps */ | 279 | /* compile the regexps */ |
271 | if (do_include != NULL) { | 280 | if (do_include != NULL) { |
@@ -310,11 +319,13 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
310 | } | 319 | } |
311 | 320 | ||
312 | *pkglist = malloc(sizeof(char *) * chld_out.lines); | 321 | *pkglist = malloc(sizeof(char *) * chld_out.lines); |
313 | if (!pkglist) | 322 | if (!pkglist) { |
314 | die(STATE_UNKNOWN, "malloc failed!\n"); | 323 | die(STATE_UNKNOWN, "malloc failed!\n"); |
324 | } | ||
315 | *secpkglist = malloc(sizeof(char *) * chld_out.lines); | 325 | *secpkglist = malloc(sizeof(char *) * chld_out.lines); |
316 | if (!secpkglist) | 326 | if (!secpkglist) { |
317 | die(STATE_UNKNOWN, "malloc failed!\n"); | 327 | die(STATE_UNKNOWN, "malloc failed!\n"); |
328 | } | ||
318 | 329 | ||
319 | /* parse the output, which should only consist of lines like | 330 | /* parse the output, which should only consist of lines like |
320 | * | 331 | * |
@@ -338,8 +349,9 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
338 | pc++; | 349 | pc++; |
339 | if (regexec(&sreg, chld_out.line[i], 0, NULL, 0) == 0) { | 350 | if (regexec(&sreg, chld_out.line[i], 0, NULL, 0) == 0) { |
340 | spc++; | 351 | spc++; |
341 | if (verbose) | 352 | if (verbose) { |
342 | printf("*"); | 353 | printf("*"); |
354 | } | ||
343 | (*secpkglist)[spc - 1] = pkg_name(chld_out.line[i]); | 355 | (*secpkglist)[spc - 1] = pkg_name(chld_out.line[i]); |
344 | } else { | 356 | } else { |
345 | (*pkglist)[pc - spc - 1] = pkg_name(chld_out.line[i]); | 357 | (*pkglist)[pc - spc - 1] = pkg_name(chld_out.line[i]); |
@@ -363,11 +375,13 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg | |||
363 | } | 375 | } |
364 | } | 376 | } |
365 | } | 377 | } |
366 | if (do_include != NULL) | 378 | if (do_include != NULL) { |
367 | regfree(&ireg); | 379 | regfree(&ireg); |
380 | } | ||
368 | regfree(&sreg); | 381 | regfree(&sreg); |
369 | if (do_exclude != NULL) | 382 | if (do_exclude != NULL) { |
370 | regfree(&ereg); | 383 | regfree(&ereg); |
384 | } | ||
371 | free(cmdline); | 385 | free(cmdline); |
372 | return result; | 386 | return result; |
373 | } | 387 | } |
@@ -426,8 +440,9 @@ char *pkg_name(char *line) { | |||
426 | } | 440 | } |
427 | 441 | ||
428 | pkg = malloc(sizeof(char) * (len + 1)); | 442 | pkg = malloc(sizeof(char) * (len + 1)); |
429 | if (!pkg) | 443 | if (!pkg) { |
430 | die(STATE_UNKNOWN, "malloc failed!\n"); | 444 | die(STATE_UNKNOWN, "malloc failed!\n"); |
445 | } | ||
431 | 446 | ||
432 | strncpy(pkg, start, len); | 447 | strncpy(pkg, start, len); |
433 | pkg[len] = '\0'; | 448 | pkg[len] = '\0'; |
@@ -442,14 +457,16 @@ char *add_to_regexp(char *expr, const char *next) { | |||
442 | 457 | ||
443 | if (expr == NULL) { | 458 | if (expr == NULL) { |
444 | re = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1)); | 459 | re = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1)); |
445 | if (!re) | 460 | if (!re) { |
446 | die(STATE_UNKNOWN, "malloc failed!\n"); | 461 | die(STATE_UNKNOWN, "malloc failed!\n"); |
462 | } | ||
447 | sprintf(re, "(%s)", next); | 463 | sprintf(re, "(%s)", next); |
448 | } else { | 464 | } else { |
449 | /* resize it, adding an extra char for the new '|' separator */ | 465 | /* resize it, adding an extra char for the new '|' separator */ |
450 | re = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1)); | 466 | re = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1)); |
451 | if (!re) | 467 | if (!re) { |
452 | die(STATE_UNKNOWN, "realloc failed!\n"); | 468 | die(STATE_UNKNOWN, "realloc failed!\n"); |
469 | } | ||
453 | /* append it starting at ')' in the old re */ | 470 | /* append it starting at ')' in the old re */ |
454 | sprintf((char *)(re + strlen(re) - 1), "|%s)", next); | 471 | sprintf((char *)(re + strlen(re) - 1), "|%s)", next); |
455 | } | 472 | } |
@@ -465,24 +482,27 @@ char *construct_cmdline(upgrade_type u, const char *opts) { | |||
465 | 482 | ||
466 | switch (u) { | 483 | switch (u) { |
467 | case UPGRADE: | 484 | case UPGRADE: |
468 | if (opts == NULL) | 485 | if (opts == NULL) { |
469 | opts_ptr = UPGRADE_DEFAULT_OPTS; | 486 | opts_ptr = UPGRADE_DEFAULT_OPTS; |
470 | else | 487 | } else { |
471 | opts_ptr = opts; | 488 | opts_ptr = opts; |
489 | } | ||
472 | aptcmd = "upgrade"; | 490 | aptcmd = "upgrade"; |
473 | break; | 491 | break; |
474 | case DIST_UPGRADE: | 492 | case DIST_UPGRADE: |
475 | if (opts == NULL) | 493 | if (opts == NULL) { |
476 | opts_ptr = UPGRADE_DEFAULT_OPTS; | 494 | opts_ptr = UPGRADE_DEFAULT_OPTS; |
477 | else | 495 | } else { |
478 | opts_ptr = opts; | 496 | opts_ptr = opts; |
497 | } | ||
479 | aptcmd = "dist-upgrade"; | 498 | aptcmd = "dist-upgrade"; |
480 | break; | 499 | break; |
481 | case NO_UPGRADE: | 500 | case NO_UPGRADE: |
482 | if (opts == NULL) | 501 | if (opts == NULL) { |
483 | opts_ptr = UPDATE_DEFAULT_OPTS; | 502 | opts_ptr = UPDATE_DEFAULT_OPTS; |
484 | else | 503 | } else { |
485 | opts_ptr = opts; | 504 | opts_ptr = opts; |
505 | } | ||
486 | aptcmd = "update"; | 506 | aptcmd = "update"; |
487 | break; | 507 | break; |
488 | } | 508 | } |
@@ -492,8 +512,9 @@ char *construct_cmdline(upgrade_type u, const char *opts) { | |||
492 | len += strlen(aptcmd) + 1; /* "upgrade\0" */ | 512 | len += strlen(aptcmd) + 1; /* "upgrade\0" */ |
493 | 513 | ||
494 | cmd = (char *)malloc(sizeof(char) * len); | 514 | cmd = (char *)malloc(sizeof(char) * len); |
495 | if (cmd == NULL) | 515 | if (cmd == NULL) { |
496 | die(STATE_UNKNOWN, "malloc failed"); | 516 | die(STATE_UNKNOWN, "malloc failed"); |
517 | } | ||
497 | sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd); | 518 | sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd); |
498 | return cmd; | 519 | return cmd; |
499 | } | 520 | } |