From 962053e6ab58ade05a43eb222a0308770ce89634 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lorenz=20K=C3=A4stle?=
<12514511+RincewindsHat@users.noreply.github.com>
Date: Fri, 8 Nov 2024 10:48:58 +0100
Subject: check_apt: clang-format
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index 5e4021b..02af1a4 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -42,14 +42,14 @@ const char *email = "devel@monitoring-plugins.org";
typedef enum { UPGRADE, DIST_UPGRADE, NO_UPGRADE } upgrade_type;
/* Character for hidden input file option (for testing). */
-#define INPUT_FILE_OPT CHAR_MAX+1
+#define INPUT_FILE_OPT CHAR_MAX + 1
/* the default opts can be overridden via the cmdline */
#define UPGRADE_DEFAULT_OPTS "-o 'Debug::NoLocking=true' -s -qq"
-#define UPDATE_DEFAULT_OPTS "-q"
+#define UPDATE_DEFAULT_OPTS "-q"
/* until i commit the configure.in patch which gets this, i'll define
* it here as well */
#ifndef PATH_TO_APTGET
-# define PATH_TO_APTGET "/usr/bin/apt-get"
+# define PATH_TO_APTGET "/usr/bin/apt-get"
#endif /* PATH_TO_APTGET */
/* String found at the beginning of the apt output lines we're interested in */
#define PKGINST_PREFIX "Inst "
@@ -62,91 +62,84 @@ void print_help(void);
void print_usage(void);
/* construct the appropriate apt-get cmdline */
-char* construct_cmdline(upgrade_type u, const char *opts);
+char *construct_cmdline(upgrade_type u, const char *opts);
/* run an apt-get update */
int run_update(void);
/* run an apt-get upgrade */
int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist);
/* add another clause to a regexp */
-char* add_to_regexp(char *expr, const char *next);
+char *add_to_regexp(char *expr, const char *next);
/* extract package name from Inst line */
-char* pkg_name(char *line);
+char *pkg_name(char *line);
/* string comparison function for qsort */
int cmpstringp(const void *p1, const void *p2);
/* configuration variables */
-static int verbose = 0; /* -v */
-static bool list = false; /* list packages available for upgrade */
-static bool do_update = false; /* whether to call apt-get update */
-static bool only_critical = false; /* whether to warn about non-critical updates */
+static int verbose = 0; /* -v */
+static bool list = false; /* list packages available for upgrade */
+static bool do_update = false; /* whether to call apt-get update */
+static bool only_critical = false; /* whether to warn about non-critical updates */
static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */
-static char *upgrade_opts = NULL; /* options to override defaults for upgrade */
-static char *update_opts = NULL; /* options to override defaults for update */
-static char *do_include = NULL; /* regexp to only include certain packages */
-static char *do_exclude = NULL; /* regexp to only exclude certain packages */
-static char *do_critical = NULL; /* regexp specifying critical packages */
-static char *input_filename = NULL; /* input filename for testing */
+static char *upgrade_opts = NULL; /* options to override defaults for upgrade */
+static char *update_opts = NULL; /* options to override defaults for update */
+static char *do_include = NULL; /* regexp to only include certain packages */
+static char *do_exclude = NULL; /* regexp to only exclude certain packages */
+static char *do_critical = NULL; /* regexp specifying critical packages */
+static char *input_filename = NULL; /* input filename for testing */
/* number of packages available for upgrade to return WARNING status */
static int packages_warning = 1;
/* other global variables */
-static int stderr_warning = 0; /* if a cmd issued output on stderr */
-static int exec_warning = 0; /* if a cmd exited non-zero */
+static int stderr_warning = 0; /* if a cmd issued output on stderr */
+static int exec_warning = 0; /* if a cmd exited non-zero */
-int main (int argc, char **argv) {
- int result=STATE_UNKNOWN, packages_available=0, sec_count=0;
- char **packages_list=NULL, **secpackages_list=NULL;
+int main(int argc, char **argv) {
+ int result = STATE_UNKNOWN, packages_available = 0, sec_count = 0;
+ char **packages_list = NULL, **secpackages_list = NULL;
/* Parse extra opts if any */
- argv=np_extra_opts(&argc, argv, progname);
+ argv = np_extra_opts(&argc, argv, progname);
if (process_arguments(argc, argv) == ERROR)
usage_va(_("Could not parse arguments"));
/* Set signal handling and alarm timeout */
- if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
+ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
usage_va(_("Cannot catch SIGALRM"));
}
/* handle timeouts gracefully... */
- alarm (timeout_interval);
+ alarm(timeout_interval);
/* if they want to run apt-get update first... */
- if(do_update) result = run_update();
+ if (do_update)
+ result = run_update();
/* apt-get upgrade */
result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list));
- if(sec_count > 0){
+ if (sec_count > 0) {
result = max_state(result, STATE_CRITICAL);
- } else if(packages_available >= packages_warning && only_critical == false){
+ } else if (packages_available >= packages_warning && only_critical == false) {
result = max_state(result, STATE_WARNING);
- } else if(result > STATE_UNKNOWN){
+ } else if (result > STATE_UNKNOWN) {
result = STATE_UNKNOWN;
}
printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s|available_upgrades=%d;;;0 critical_updates=%d;;;0\n"),
- state_text(result),
- packages_available,
- (upgrade==DIST_UPGRADE)?"dist-upgrade":"upgrade",
- sec_count,
- (stderr_warning)?" warnings detected":"",
- (stderr_warning && exec_warning)?",":"",
- (exec_warning)?" errors detected":"",
- (stderr_warning||exec_warning)?".":"",
- packages_available,
- sec_count
- );
-
- if(list) {
- qsort(secpackages_list, sec_count, sizeof(char*), cmpstringp);
- qsort(packages_list, packages_available-sec_count, sizeof(char*), cmpstringp);
-
- for(int i = 0; i < sec_count; i++)
+ state_text(result), packages_available, (upgrade == DIST_UPGRADE) ? "dist-upgrade" : "upgrade", sec_count,
+ (stderr_warning) ? " warnings detected" : "", (stderr_warning && exec_warning) ? "," : "",
+ (exec_warning) ? " errors detected" : "", (stderr_warning || exec_warning) ? "." : "", packages_available, sec_count);
+
+ if (list) {
+ qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp);
+ qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp);
+
+ for (int i = 0; i < sec_count; i++)
printf("%s (security)\n", secpackages_list[i]);
if (only_critical == false) {
- for(int i = 0; i < packages_available - sec_count; i++)
+ for (int i = 0; i < packages_available - sec_count; i++)
printf("%s\n", packages_list[i]);
}
}
@@ -155,34 +148,33 @@ int main (int argc, char **argv) {
}
/* process command-line arguments */
-int process_arguments (int argc, char **argv) {
+int process_arguments(int argc, char **argv) {
int c;
- static struct option longopts[] = {
- {"version", no_argument, 0, 'V'},
- {"help", no_argument, 0, 'h'},
- {"verbose", no_argument, 0, 'v'},
- {"timeout", required_argument, 0, 't'},
- {"update", optional_argument, 0, 'u'},
- {"upgrade", optional_argument, 0, 'U'},
- {"no-upgrade", no_argument, 0, 'n'},
- {"dist-upgrade", optional_argument, 0, 'd'},
- {"list", no_argument, false, 'l'},
- {"include", required_argument, 0, 'i'},
- {"exclude", required_argument, 0, 'e'},
- {"critical", required_argument, 0, 'c'},
- {"only-critical", no_argument, 0, 'o'},
- {"input-file", required_argument, 0, INPUT_FILE_OPT},
- {"packages-warning", required_argument, 0, 'w'},
- {0, 0, 0, 0}
- };
-
- while(1) {
+ static struct option longopts[] = {{"version", no_argument, 0, 'V'},
+ {"help", no_argument, 0, 'h'},
+ {"verbose", no_argument, 0, 'v'},
+ {"timeout", required_argument, 0, 't'},
+ {"update", optional_argument, 0, 'u'},
+ {"upgrade", optional_argument, 0, 'U'},
+ {"no-upgrade", no_argument, 0, 'n'},
+ {"dist-upgrade", optional_argument, 0, 'd'},
+ {"list", no_argument, false, 'l'},
+ {"include", required_argument, 0, 'i'},
+ {"exclude", required_argument, 0, 'e'},
+ {"critical", required_argument, 0, 'c'},
+ {"only-critical", no_argument, 0, 'o'},
+ {"input-file", required_argument, 0, INPUT_FILE_OPT},
+ {"packages-warning", required_argument, 0, 'w'},
+ {0, 0, 0, 0}};
+
+ while (1) {
c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL);
- if(c == -1 || c == EOF || c == 1) break;
+ if (c == -1 || c == EOF || c == 1)
+ break;
- switch(c) {
+ switch (c) {
case 'h':
print_help();
exit(STATE_UNKNOWN);
@@ -193,46 +185,49 @@ int process_arguments (int argc, char **argv) {
verbose++;
break;
case 't':
- timeout_interval=atoi(optarg);
+ timeout_interval = atoi(optarg);
break;
case 'd':
- upgrade=DIST_UPGRADE;
- if(optarg!=NULL){
- upgrade_opts=strdup(optarg);
- if(upgrade_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
+ upgrade = DIST_UPGRADE;
+ if (optarg != NULL) {
+ upgrade_opts = strdup(optarg);
+ if (upgrade_opts == NULL)
+ die(STATE_UNKNOWN, "strdup failed");
}
break;
case 'U':
- upgrade=UPGRADE;
- if(optarg!=NULL){
- upgrade_opts=strdup(optarg);
- if(upgrade_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
+ upgrade = UPGRADE;
+ if (optarg != NULL) {
+ upgrade_opts = strdup(optarg);
+ if (upgrade_opts == NULL)
+ die(STATE_UNKNOWN, "strdup failed");
}
break;
case 'n':
- upgrade=NO_UPGRADE;
+ upgrade = NO_UPGRADE;
break;
case 'u':
- do_update=true;
- if(optarg!=NULL){
- update_opts=strdup(optarg);
- if(update_opts==NULL) die(STATE_UNKNOWN, "strdup failed");
+ do_update = true;
+ if (optarg != NULL) {
+ update_opts = strdup(optarg);
+ if (update_opts == NULL)
+ die(STATE_UNKNOWN, "strdup failed");
}
break;
case 'l':
- list=true;
+ list = true;
break;
case 'i':
- do_include=add_to_regexp(do_include, optarg);
+ do_include = add_to_regexp(do_include, optarg);
break;
case 'e':
- do_exclude=add_to_regexp(do_exclude, optarg);
+ do_exclude = add_to_regexp(do_exclude, optarg);
break;
case 'c':
- do_critical=add_to_regexp(do_critical, optarg);
+ do_critical = add_to_regexp(do_critical, optarg);
break;
case 'o':
- only_critical=true;
+ only_critical = true;
break;
case INPUT_FILE_OPT:
input_filename = optarg;
@@ -249,46 +244,44 @@ int process_arguments (int argc, char **argv) {
return OK;
}
-
/* run an apt-get upgrade */
-int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist){
- int result=STATE_UNKNOWN, regres=0, pc=0, spc=0;
+int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist) {
+ int result = STATE_UNKNOWN, regres = 0, pc = 0, spc = 0;
struct output chld_out, chld_err;
regex_t ireg, ereg, sreg;
- char *cmdline=NULL, rerrbuf[64];
+ char *cmdline = NULL, rerrbuf[64];
/* initialize ereg as it is possible it is printed while uninitialized */
memset(&ereg, '\0', sizeof(ereg.buffer));
- if(upgrade==NO_UPGRADE) return STATE_OK;
+ if (upgrade == NO_UPGRADE)
+ return STATE_OK;
/* compile the regexps */
if (do_include != NULL) {
- regres=regcomp(&ireg, do_include, REG_EXTENDED);
- if (regres!=0) {
+ regres = regcomp(&ireg, do_include, REG_EXTENDED);
+ if (regres != 0) {
regerror(regres, &ireg, rerrbuf, 64);
die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
}
}
- if(do_exclude!=NULL){
- regres=regcomp(&ereg, do_exclude, REG_EXTENDED);
- if(regres!=0) {
+ if (do_exclude != NULL) {
+ regres = regcomp(&ereg, do_exclude, REG_EXTENDED);
+ if (regres != 0) {
regerror(regres, &ereg, rerrbuf, 64);
- die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"),
- progname, rerrbuf);
+ die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
}
}
const char *crit_ptr = (do_critical != NULL) ? do_critical : SECURITY_RE;
- regres=regcomp(&sreg, crit_ptr, REG_EXTENDED);
- if(regres!=0) {
+ regres = regcomp(&sreg, crit_ptr, REG_EXTENDED);
+ if (regres != 0) {
regerror(regres, &ereg, rerrbuf, 64);
- die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"),
- progname, rerrbuf);
+ die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
}
- cmdline=construct_cmdline(upgrade, upgrade_opts);
+ cmdline = construct_cmdline(upgrade, upgrade_opts);
if (input_filename != NULL) {
/* read input from a file for testing */
result = cmd_file_read(input_filename, &chld_out, 0);
@@ -300,17 +293,18 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
/* apt-get upgrade only changes exit status if there is an
* internal error when run in dry-run mode. therefore we will
* treat such an error as UNKNOWN */
- if(result != 0){
- exec_warning=1;
+ if (result != 0) {
+ exec_warning = 1;
result = STATE_UNKNOWN;
- fprintf(stderr, _("'%s' exited with non-zero status.\n"),
- cmdline);
+ fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline);
}
- *pkglist=malloc(sizeof(char *) * chld_out.lines);
- if(!pkglist) die(STATE_UNKNOWN, "malloc failed!\n");
- *secpkglist=malloc(sizeof(char *) * chld_out.lines);
- if(!secpkglist) die(STATE_UNKNOWN, "malloc failed!\n");
+ *pkglist = malloc(sizeof(char *) * chld_out.lines);
+ if (!pkglist)
+ die(STATE_UNKNOWN, "malloc failed!\n");
+ *secpkglist = malloc(sizeof(char *) * chld_out.lines);
+ if (!secpkglist)
+ die(STATE_UNKNOWN, "malloc failed!\n");
/* parse the output, which should only consist of lines like
*
@@ -321,54 +315,56 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
* we may need to switch to the --print-uris output format,
* in which case the logic here will slightly change.
*/
- for(size_t i = 0; i < chld_out.lines; i++) {
- if(verbose){
+ for (size_t i = 0; i < chld_out.lines; i++) {
+ if (verbose) {
printf("%s\n", chld_out.line[i]);
}
/* if it is a package we care about */
if (strncmp(PKGINST_PREFIX, chld_out.line[i], strlen(PKGINST_PREFIX)) == 0 &&
- (do_include == NULL || regexec(&ireg, chld_out.line[i], 0, NULL, 0) == 0)) {
+ (do_include == NULL || regexec(&ireg, chld_out.line[i], 0, NULL, 0) == 0)) {
/* if we're not excluding, or it's not in the
* list of stuff to exclude */
- if(do_exclude==NULL ||
- regexec(&ereg, chld_out.line[i], 0, NULL, 0)!=0){
+ if (do_exclude == NULL || regexec(&ereg, chld_out.line[i], 0, NULL, 0) != 0) {
pc++;
- if(regexec(&sreg, chld_out.line[i], 0, NULL, 0)==0){
+ if (regexec(&sreg, chld_out.line[i], 0, NULL, 0) == 0) {
spc++;
- if(verbose) printf("*");
- (*secpkglist)[spc-1] = pkg_name(chld_out.line[i]);
+ if (verbose)
+ printf("*");
+ (*secpkglist)[spc - 1] = pkg_name(chld_out.line[i]);
} else {
- (*pkglist)[pc-spc-1] = pkg_name(chld_out.line[i]);
+ (*pkglist)[pc - spc - 1] = pkg_name(chld_out.line[i]);
}
- if(verbose){
+ if (verbose) {
printf("*%s\n", chld_out.line[i]);
}
}
}
}
- *pkgcount=pc;
- *secpkgcount=spc;
+ *pkgcount = pc;
+ *secpkgcount = spc;
/* If we get anything on stderr, at least set warning */
if (input_filename == NULL && chld_err.buflen) {
- stderr_warning=1;
+ stderr_warning = 1;
result = max_state(result, STATE_WARNING);
- if(verbose){
- for(size_t i = 0; i < chld_err.lines; i++) {
+ if (verbose) {
+ for (size_t i = 0; i < chld_err.lines; i++) {
fprintf(stderr, "%s\n", chld_err.line[i]);
}
}
}
- if (do_include != NULL) regfree(&ireg);
+ if (do_include != NULL)
+ regfree(&ireg);
regfree(&sreg);
- if(do_exclude!=NULL) regfree(&ereg);
+ if (do_exclude != NULL)
+ regfree(&ereg);
free(cmdline);
return result;
}
/* run an apt-get update (needs root) */
-int run_update(void){
- int result=STATE_UNKNOWN;
+int run_update(void) {
+ int result = STATE_UNKNOWN;
struct output chld_out, chld_err;
char *cmdline;
@@ -378,25 +374,24 @@ int run_update(void){
/* apt-get update changes exit status if it can't fetch packages.
* since we were explicitly asked to do so, this is treated as
* a critical error. */
- if(result != 0){
- exec_warning=1;
+ if (result != 0) {
+ exec_warning = 1;
result = STATE_CRITICAL;
- fprintf(stderr, _("'%s' exited with non-zero status.\n"),
- cmdline);
+ fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline);
}
- if(verbose){
- for(size_t i = 0; i < chld_out.lines; i++) {
+ if (verbose) {
+ for (size_t i = 0; i < chld_out.lines; i++) {
printf("%s\n", chld_out.line[i]);
}
}
/* If we get anything on stderr, at least set warning */
- if(chld_err.buflen){
- stderr_warning=1;
+ if (chld_err.buflen) {
+ stderr_warning = 1;
result = max_state(result, STATE_WARNING);
- if(verbose){
- for(size_t i = 0; i < chld_err.lines; i++) {
+ if (verbose) {
+ for (size_t i = 0; i < chld_err.lines; i++) {
fprintf(stderr, "%s\n", chld_err.line[i]);
}
}
@@ -405,158 +400,161 @@ int run_update(void){
return result;
}
-char* pkg_name(char *line){
- char *start=NULL, *space=NULL, *pkg=NULL;
- int len=0;
+char *pkg_name(char *line) {
+ char *start = NULL, *space = NULL, *pkg = NULL;
+ int len = 0;
start = line + strlen(PKGINST_PREFIX);
len = strlen(start);
space = index(start, ' ');
- if(space!=NULL){
+ if (space != NULL) {
len = space - start;
}
- pkg=malloc(sizeof(char)*(len+1));
- if(!pkg) die(STATE_UNKNOWN, "malloc failed!\n");
+ pkg = malloc(sizeof(char) * (len + 1));
+ if (!pkg)
+ die(STATE_UNKNOWN, "malloc failed!\n");
strncpy(pkg, start, len);
- pkg[len]='\0';
+ pkg[len] = '\0';
return pkg;
}
-int cmpstringp(const void *p1, const void *p2){
- return strcmp(* (char * const *) p1, * (char * const *) p2);
-}
+int cmpstringp(const void *p1, const void *p2) { return strcmp(*(char *const *)p1, *(char *const *)p2); }
-char* add_to_regexp(char *expr, const char *next){
- char *re=NULL;
+char *add_to_regexp(char *expr, const char *next) {
+ char *re = NULL;
- if(expr==NULL){
- re=malloc(sizeof(char)*(strlen("()")+strlen(next)+1));
- if(!re) die(STATE_UNKNOWN, "malloc failed!\n");
+ if (expr == NULL) {
+ re = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1));
+ if (!re)
+ die(STATE_UNKNOWN, "malloc failed!\n");
sprintf(re, "(%s)", next);
} else {
/* resize it, adding an extra char for the new '|' separator */
- re=realloc(expr, sizeof(char)*(strlen(expr)+1+strlen(next)+1));
- if(!re) die(STATE_UNKNOWN, "realloc failed!\n");
+ re = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1));
+ if (!re)
+ die(STATE_UNKNOWN, "realloc failed!\n");
/* append it starting at ')' in the old re */
- sprintf((char*)(re+strlen(re)-1), "|%s)", next);
+ sprintf((char *)(re + strlen(re) - 1), "|%s)", next);
}
return re;
}
-char* construct_cmdline(upgrade_type u, const char *opts){
- int len=0;
- const char *opts_ptr=NULL, *aptcmd=NULL;
- char *cmd=NULL;
+char *construct_cmdline(upgrade_type u, const char *opts) {
+ int len = 0;
+ const char *opts_ptr = NULL, *aptcmd = NULL;
+ char *cmd = NULL;
- switch(u){
+ switch (u) {
case UPGRADE:
- if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS;
- else opts_ptr=opts;
- aptcmd="upgrade";
+ if (opts == NULL)
+ opts_ptr = UPGRADE_DEFAULT_OPTS;
+ else
+ opts_ptr = opts;
+ aptcmd = "upgrade";
break;
case DIST_UPGRADE:
- if(opts==NULL) opts_ptr=UPGRADE_DEFAULT_OPTS;
- else opts_ptr=opts;
- aptcmd="dist-upgrade";
+ if (opts == NULL)
+ opts_ptr = UPGRADE_DEFAULT_OPTS;
+ else
+ opts_ptr = opts;
+ aptcmd = "dist-upgrade";
break;
case NO_UPGRADE:
- if(opts==NULL) opts_ptr=UPDATE_DEFAULT_OPTS;
- else opts_ptr=opts;
- aptcmd="update";
+ if (opts == NULL)
+ opts_ptr = UPDATE_DEFAULT_OPTS;
+ else
+ opts_ptr = opts;
+ aptcmd = "update";
break;
}
- len+=strlen(PATH_TO_APTGET)+1; /* "/usr/bin/apt-get " */
- len+=strlen(opts_ptr)+1; /* "opts " */
- len+=strlen(aptcmd)+1; /* "upgrade\0" */
+ len += strlen(PATH_TO_APTGET) + 1; /* "/usr/bin/apt-get " */
+ len += strlen(opts_ptr) + 1; /* "opts " */
+ len += strlen(aptcmd) + 1; /* "upgrade\0" */
- cmd=(char*)malloc(sizeof(char)*len);
- if(cmd==NULL) die(STATE_UNKNOWN, "malloc failed");
+ cmd = (char *)malloc(sizeof(char) * len);
+ if (cmd == NULL)
+ die(STATE_UNKNOWN, "malloc failed");
sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd);
return cmd;
}
/* informative help message */
-void
-print_help (void)
-{
- print_revision(progname, NP_VERSION);
-
- printf(_(COPYRIGHT), copyright, email);
-
- printf("%s\n", _("This plugin checks for software updates on systems that use"));
- printf("%s\n", _("package management systems based on the apt-get(8) command"));
- printf("%s\n", _("found in Debian GNU/Linux"));
-
- printf ("\n\n");
-
- print_usage();
-
- printf(UT_HELP_VRSN);
- printf(UT_EXTRA_OPTS);
-
- printf(UT_PLUG_TIMEOUT, timeout_interval);
-
- printf (" %s\n", "-n, --no-upgrade");
- printf (" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least)."));
- printf (" %s\n", "-l, --list");
- printf (" %s\n", _("List packages available for upgrade. Packages are printed sorted by"));
- printf (" %s\n", _("name with security packages listed first."));
- printf (" %s\n", "-i, --include=REGEXP");
- printf (" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times"));
- printf (" %s\n", _("the values will be combined together. Any packages matching this list"));
- printf (" %s\n", _("cause the plugin to return WARNING status. Others will be ignored."));
- printf (" %s\n", _("Default is to include all packages."));
- printf (" %s\n", "-e, --exclude=REGEXP");
- printf (" %s\n", _("Exclude packages matching REGEXP from the list of packages that would"));
- printf (" %s\n", _("otherwise be included. Can be specified multiple times; the values"));
- printf (" %s\n", _("will be combined together. Default is to exclude no packages."));
- printf (" %s\n", "-c, --critical=REGEXP");
- printf (" %s\n", _("If the full package information of any of the upgradable packages match"));
- printf (" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified"));
- printf (" %s\n", _("multiple times like above. Default is a regexp matching security"));
- printf (" %s\n", _("upgrades for Debian and Ubuntu:"));
- printf (" \t%s\n", SECURITY_RE);
- printf (" %s\n", _("Note that the package must first match the include list before its"));
- printf (" %s\n", _("information is compared against the critical list."));
- printf (" %s\n", "-o, --only-critical");
- printf (" %s\n", _("Only warn about upgrades matching the critical list. The total number"));
- printf (" %s\n", _("of upgrades will be printed, but any non-critical upgrades will not cause"));
- printf (" %s\n", _("the plugin to return WARNING status."));
- printf (" %s\n", "-w, --packages-warning");
- printf (" %s\n", _("Minimum number of packages available for upgrade to return WARNING status."));
- printf (" %s\n\n", _("Default is 1 package."));
-
- printf ("%s\n\n", _("The following options require root privileges and should be used with care:"));
- printf (" %s\n", "-u, --update=OPTS");
- printf (" %s\n", _("First perform an 'apt-get update'. An optional OPTS parameter overrides"));
- printf (" %s\n", _("the default options. Note: you may also need to adjust the global"));
- printf (" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get"));
- printf (" %s\n", _("upgrade is expected to take longer than the default timeout."));
- printf (" %s\n", "-U, --upgrade=OPTS");
- printf (" %s\n", _("Perform an upgrade. If an optional OPTS argument is provided,"));
- printf (" %s\n", _("apt-get will be run with these command line options instead of the"));
- printf (" %s", _("default "));
- printf ("(%s).\n", UPGRADE_DEFAULT_OPTS);
- printf (" %s\n", _("Note that you may be required to have root privileges if you do not use"));
- printf (" %s\n", _("the default options, which will only run a simulation and NOT perform the upgrade"));
- printf (" %s\n", "-d, --dist-upgrade=OPTS");
- printf (" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS"));
- printf (" %s\n", _("can be provided to override the default options."));
-
- printf(UT_SUPPORT);
+void print_help(void) {
+ print_revision(progname, NP_VERSION);
+
+ printf(_(COPYRIGHT), copyright, email);
+
+ printf("%s\n", _("This plugin checks for software updates on systems that use"));
+ printf("%s\n", _("package management systems based on the apt-get(8) command"));
+ printf("%s\n", _("found in Debian GNU/Linux"));
+
+ printf("\n\n");
+
+ print_usage();
+
+ printf(UT_HELP_VRSN);
+ printf(UT_EXTRA_OPTS);
+
+ printf(UT_PLUG_TIMEOUT, timeout_interval);
+
+ printf(" %s\n", "-n, --no-upgrade");
+ printf(" %s\n", _("Do not run the upgrade. Probably not useful (without -u at least)."));
+ printf(" %s\n", "-l, --list");
+ printf(" %s\n", _("List packages available for upgrade. Packages are printed sorted by"));
+ printf(" %s\n", _("name with security packages listed first."));
+ printf(" %s\n", "-i, --include=REGEXP");
+ printf(" %s\n", _("Include only packages matching REGEXP. Can be specified multiple times"));
+ printf(" %s\n", _("the values will be combined together. Any packages matching this list"));
+ printf(" %s\n", _("cause the plugin to return WARNING status. Others will be ignored."));
+ printf(" %s\n", _("Default is to include all packages."));
+ printf(" %s\n", "-e, --exclude=REGEXP");
+ printf(" %s\n", _("Exclude packages matching REGEXP from the list of packages that would"));
+ printf(" %s\n", _("otherwise be included. Can be specified multiple times; the values"));
+ printf(" %s\n", _("will be combined together. Default is to exclude no packages."));
+ printf(" %s\n", "-c, --critical=REGEXP");
+ printf(" %s\n", _("If the full package information of any of the upgradable packages match"));
+ printf(" %s\n", _("this REGEXP, the plugin will return CRITICAL status. Can be specified"));
+ printf(" %s\n", _("multiple times like above. Default is a regexp matching security"));
+ printf(" %s\n", _("upgrades for Debian and Ubuntu:"));
+ printf(" \t%s\n", SECURITY_RE);
+ printf(" %s\n", _("Note that the package must first match the include list before its"));
+ printf(" %s\n", _("information is compared against the critical list."));
+ printf(" %s\n", "-o, --only-critical");
+ printf(" %s\n", _("Only warn about upgrades matching the critical list. The total number"));
+ printf(" %s\n", _("of upgrades will be printed, but any non-critical upgrades will not cause"));
+ printf(" %s\n", _("the plugin to return WARNING status."));
+ printf(" %s\n", "-w, --packages-warning");
+ printf(" %s\n", _("Minimum number of packages available for upgrade to return WARNING status."));
+ printf(" %s\n\n", _("Default is 1 package."));
+
+ printf("%s\n\n", _("The following options require root privileges and should be used with care:"));
+ printf(" %s\n", "-u, --update=OPTS");
+ printf(" %s\n", _("First perform an 'apt-get update'. An optional OPTS parameter overrides"));
+ printf(" %s\n", _("the default options. Note: you may also need to adjust the global"));
+ printf(" %s\n", _("timeout (with -t) to prevent the plugin from timing out if apt-get"));
+ printf(" %s\n", _("upgrade is expected to take longer than the default timeout."));
+ printf(" %s\n", "-U, --upgrade=OPTS");
+ printf(" %s\n", _("Perform an upgrade. If an optional OPTS argument is provided,"));
+ printf(" %s\n", _("apt-get will be run with these command line options instead of the"));
+ printf(" %s", _("default "));
+ printf("(%s).\n", UPGRADE_DEFAULT_OPTS);
+ printf(" %s\n", _("Note that you may be required to have root privileges if you do not use"));
+ printf(" %s\n", _("the default options, which will only run a simulation and NOT perform the upgrade"));
+ printf(" %s\n", "-d, --dist-upgrade=OPTS");
+ printf(" %s\n", _("Perform a dist-upgrade instead of normal upgrade. Like with -U OPTS"));
+ printf(" %s\n", _("can be provided to override the default options."));
+
+ printf(UT_SUPPORT);
}
-
/* simple usage heading */
-void
-print_usage(void)
-{
- printf ("%s\n", _("Usage:"));
- printf ("%s [[-d|-u|-U]opts] [-n] [-l] [-t timeout] [-w packages-warning]\n", progname);
+void print_usage(void) {
+ printf("%s\n", _("Usage:"));
+ printf("%s [[-d|-u|-U]opts] [-n] [-l] [-t timeout] [-w packages-warning]\n", progname);
}
--
cgit v0.10-9-g596f
From d7671b8a4a8f7a46f5576be7d6d6644c9cd77000 Mon Sep 17 00:00:00 2001
From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com>
Date: Sat, 26 Oct 2024 22:52:23 +0200
Subject: check_apt: mindless linter fixes
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index 02af1a4..16c34ca 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -57,7 +57,7 @@ typedef enum { UPGRADE, DIST_UPGRADE, NO_UPGRADE } upgrade_type;
#define SECURITY_RE "^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)"
/* some standard functions */
-int process_arguments(int, char **);
+int process_arguments(int /*argc*/, char ** /*argv*/);
void print_help(void);
void print_usage(void);
@@ -94,8 +94,11 @@ static int stderr_warning = 0; /* if a cmd issued output on stderr */
static int exec_warning = 0; /* if a cmd exited non-zero */
int main(int argc, char **argv) {
- int result = STATE_UNKNOWN, packages_available = 0, sec_count = 0;
- char **packages_list = NULL, **secpackages_list = NULL;
+ int result = STATE_UNKNOWN;
+ int packages_available = 0;
+ int sec_count = 0;
+ char **packages_list = NULL;
+ char **secpackages_list = NULL;
/* Parse extra opts if any */
argv = np_extra_opts(&argc, argv, progname);
@@ -246,10 +249,17 @@ int process_arguments(int argc, char **argv) {
/* run an apt-get upgrade */
int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist) {
- int result = STATE_UNKNOWN, regres = 0, pc = 0, spc = 0;
- struct output chld_out, chld_err;
- regex_t ireg, ereg, sreg;
- char *cmdline = NULL, rerrbuf[64];
+ int result = STATE_UNKNOWN;
+ int regres = 0;
+ int pc = 0;
+ int spc = 0;
+ struct output chld_out;
+ struct output chld_err;
+ regex_t ireg;
+ regex_t ereg;
+ regex_t sreg;
+ char *cmdline = NULL;
+ char rerrbuf[64];
/* initialize ereg as it is possible it is printed while uninitialized */
memset(&ereg, '\0', sizeof(ereg.buffer));
@@ -365,7 +375,8 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
/* run an apt-get update (needs root) */
int run_update(void) {
int result = STATE_UNKNOWN;
- struct output chld_out, chld_err;
+ struct output chld_out;
+ struct output chld_err;
char *cmdline;
/* run the update */
@@ -401,7 +412,9 @@ int run_update(void) {
}
char *pkg_name(char *line) {
- char *start = NULL, *space = NULL, *pkg = NULL;
+ char *start = NULL;
+ char *space = NULL;
+ char *pkg = NULL;
int len = 0;
start = line + strlen(PKGINST_PREFIX);
@@ -446,7 +459,8 @@ char *add_to_regexp(char *expr, const char *next) {
char *construct_cmdline(upgrade_type u, const char *opts) {
int len = 0;
- const char *opts_ptr = NULL, *aptcmd = NULL;
+ const char *opts_ptr = NULL;
+ const char *aptcmd = NULL;
char *cmd = NULL;
switch (u) {
--
cgit v0.10-9-g596f
From b43e913b2c2a380ef59ed3046cac1f0f92b65a9d Mon Sep 17 00:00:00 2001
From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com>
Date: Sat, 26 Oct 2024 22:54:15 +0200
Subject: clang-format: always add braces
diff --git a/.clang-format b/.clang-format
index 877a53b..ca411ed 100644
--- a/.clang-format
+++ b/.clang-format
@@ -9,3 +9,4 @@ IndentPPDirectives: AfterHash
SortIncludes: Never
AllowShortEnumsOnASingleLine: false
BinPackArguments: true
+InsertBraces: true
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) {
/* Parse extra opts if any */
argv = np_extra_opts(&argc, argv, progname);
- if (process_arguments(argc, argv) == ERROR)
+ if (process_arguments(argc, argv) == ERROR) {
usage_va(_("Could not parse arguments"));
+ }
/* Set signal handling and alarm timeout */
if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
@@ -115,8 +116,9 @@ int main(int argc, char **argv) {
alarm(timeout_interval);
/* if they want to run apt-get update first... */
- if (do_update)
+ if (do_update) {
result = run_update();
+ }
/* apt-get upgrade */
result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list));
@@ -138,12 +140,14 @@ int main(int argc, char **argv) {
qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp);
qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp);
- for (int i = 0; i < sec_count; i++)
+ for (int i = 0; i < sec_count; i++) {
printf("%s (security)\n", secpackages_list[i]);
+ }
if (only_critical == false) {
- for (int i = 0; i < packages_available - sec_count; i++)
+ for (int i = 0; i < packages_available - sec_count; i++) {
printf("%s\n", packages_list[i]);
+ }
}
}
@@ -174,8 +178,9 @@ int process_arguments(int argc, char **argv) {
while (1) {
c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL);
- if (c == -1 || c == EOF || c == 1)
+ if (c == -1 || c == EOF || c == 1) {
break;
+ }
switch (c) {
case 'h':
@@ -194,16 +199,18 @@ int process_arguments(int argc, char **argv) {
upgrade = DIST_UPGRADE;
if (optarg != NULL) {
upgrade_opts = strdup(optarg);
- if (upgrade_opts == NULL)
+ if (upgrade_opts == NULL) {
die(STATE_UNKNOWN, "strdup failed");
+ }
}
break;
case 'U':
upgrade = UPGRADE;
if (optarg != NULL) {
upgrade_opts = strdup(optarg);
- if (upgrade_opts == NULL)
+ if (upgrade_opts == NULL) {
die(STATE_UNKNOWN, "strdup failed");
+ }
}
break;
case 'n':
@@ -213,8 +220,9 @@ int process_arguments(int argc, char **argv) {
do_update = true;
if (optarg != NULL) {
update_opts = strdup(optarg);
- if (update_opts == NULL)
+ if (update_opts == NULL) {
die(STATE_UNKNOWN, "strdup failed");
+ }
}
break;
case 'l':
@@ -264,8 +272,9 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
/* initialize ereg as it is possible it is printed while uninitialized */
memset(&ereg, '\0', sizeof(ereg.buffer));
- if (upgrade == NO_UPGRADE)
+ if (upgrade == NO_UPGRADE) {
return STATE_OK;
+ }
/* compile the regexps */
if (do_include != NULL) {
@@ -310,11 +319,13 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
}
*pkglist = malloc(sizeof(char *) * chld_out.lines);
- if (!pkglist)
+ if (!pkglist) {
die(STATE_UNKNOWN, "malloc failed!\n");
+ }
*secpkglist = malloc(sizeof(char *) * chld_out.lines);
- if (!secpkglist)
+ if (!secpkglist) {
die(STATE_UNKNOWN, "malloc failed!\n");
+ }
/* parse the output, which should only consist of lines like
*
@@ -338,8 +349,9 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
pc++;
if (regexec(&sreg, chld_out.line[i], 0, NULL, 0) == 0) {
spc++;
- if (verbose)
+ if (verbose) {
printf("*");
+ }
(*secpkglist)[spc - 1] = pkg_name(chld_out.line[i]);
} else {
(*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
}
}
}
- if (do_include != NULL)
+ if (do_include != NULL) {
regfree(&ireg);
+ }
regfree(&sreg);
- if (do_exclude != NULL)
+ if (do_exclude != NULL) {
regfree(&ereg);
+ }
free(cmdline);
return result;
}
@@ -426,8 +440,9 @@ char *pkg_name(char *line) {
}
pkg = malloc(sizeof(char) * (len + 1));
- if (!pkg)
+ if (!pkg) {
die(STATE_UNKNOWN, "malloc failed!\n");
+ }
strncpy(pkg, start, len);
pkg[len] = '\0';
@@ -442,14 +457,16 @@ char *add_to_regexp(char *expr, const char *next) {
if (expr == NULL) {
re = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1));
- if (!re)
+ if (!re) {
die(STATE_UNKNOWN, "malloc failed!\n");
+ }
sprintf(re, "(%s)", next);
} else {
/* resize it, adding an extra char for the new '|' separator */
re = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1));
- if (!re)
+ if (!re) {
die(STATE_UNKNOWN, "realloc failed!\n");
+ }
/* append it starting at ')' in the old re */
sprintf((char *)(re + strlen(re) - 1), "|%s)", next);
}
@@ -465,24 +482,27 @@ char *construct_cmdline(upgrade_type u, const char *opts) {
switch (u) {
case UPGRADE:
- if (opts == NULL)
+ if (opts == NULL) {
opts_ptr = UPGRADE_DEFAULT_OPTS;
- else
+ } else {
opts_ptr = opts;
+ }
aptcmd = "upgrade";
break;
case DIST_UPGRADE:
- if (opts == NULL)
+ if (opts == NULL) {
opts_ptr = UPGRADE_DEFAULT_OPTS;
- else
+ } else {
opts_ptr = opts;
+ }
aptcmd = "dist-upgrade";
break;
case NO_UPGRADE:
- if (opts == NULL)
+ if (opts == NULL) {
opts_ptr = UPDATE_DEFAULT_OPTS;
- else
+ } else {
opts_ptr = opts;
+ }
aptcmd = "update";
break;
}
@@ -492,8 +512,9 @@ char *construct_cmdline(upgrade_type u, const char *opts) {
len += strlen(aptcmd) + 1; /* "upgrade\0" */
cmd = (char *)malloc(sizeof(char) * len);
- if (cmd == NULL)
+ if (cmd == NULL) {
die(STATE_UNKNOWN, "malloc failed");
+ }
sprintf(cmd, "%s %s %s", PATH_TO_APTGET, opts_ptr, aptcmd);
return cmd;
}
--
cgit v0.10-9-g596f
From ebb4bf4bb4c14807878dab488a66868f7f5751e3 Mon Sep 17 00:00:00 2001
From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com>
Date: Sat, 26 Oct 2024 22:57:58 +0200
Subject: check_apt: declare local functions static
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index 288d37c..eb08a06 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -57,22 +57,22 @@ typedef enum { UPGRADE, DIST_UPGRADE, NO_UPGRADE } upgrade_type;
#define SECURITY_RE "^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)"
/* some standard functions */
-int process_arguments(int /*argc*/, char ** /*argv*/);
-void print_help(void);
+static int process_arguments(int /*argc*/, char ** /*argv*/);
+static void print_help(void);
void print_usage(void);
/* construct the appropriate apt-get cmdline */
-char *construct_cmdline(upgrade_type u, const char *opts);
+static char *construct_cmdline(upgrade_type u, const char *opts);
/* run an apt-get update */
-int run_update(void);
+static int run_update(void);
/* run an apt-get upgrade */
-int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist);
+static int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist);
/* add another clause to a regexp */
-char *add_to_regexp(char *expr, const char *next);
+static char *add_to_regexp(char *expr, const char *next);
/* extract package name from Inst line */
-char *pkg_name(char *line);
+static char *pkg_name(char *line);
/* string comparison function for qsort */
-int cmpstringp(const void *p1, const void *p2);
+static int cmpstringp(const void *p1, const void *p2);
/* configuration variables */
static int verbose = 0; /* -v */
--
cgit v0.10-9-g596f
From 75ff5b3b51a76081196f41c4b428dc3db50f2664 Mon Sep 17 00:00:00 2001
From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com>
Date: Sat, 26 Oct 2024 23:18:26 +0200
Subject: check_apt: localize variables
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index eb08a06..c2c693e 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -94,12 +94,6 @@ static int stderr_warning = 0; /* if a cmd issued output on stderr */
static int exec_warning = 0; /* if a cmd exited non-zero */
int main(int argc, char **argv) {
- int result = STATE_UNKNOWN;
- int packages_available = 0;
- int sec_count = 0;
- char **packages_list = NULL;
- char **secpackages_list = NULL;
-
/* Parse extra opts if any */
argv = np_extra_opts(&argc, argv, progname);
@@ -115,11 +109,16 @@ int main(int argc, char **argv) {
/* handle timeouts gracefully... */
alarm(timeout_interval);
+ int result = STATE_UNKNOWN;
/* if they want to run apt-get update first... */
if (do_update) {
result = run_update();
}
+ int packages_available = 0;
+ int sec_count = 0;
+ char **packages_list = NULL;
+ char **secpackages_list = NULL;
/* apt-get upgrade */
result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list));
@@ -156,8 +155,6 @@ int main(int argc, char **argv) {
/* process command-line arguments */
int process_arguments(int argc, char **argv) {
- int c;
-
static struct option longopts[] = {{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
@@ -175,14 +172,14 @@ int process_arguments(int argc, char **argv) {
{"packages-warning", required_argument, 0, 'w'},
{0, 0, 0, 0}};
- while (1) {
- c = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL);
+ while (true) {
+ int option_char = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL);
- if (c == -1 || c == EOF || c == 1) {
+ if (option_char == -1 || option_char == EOF || option_char == 1) {
break;
}
- switch (c) {
+ switch (option_char) {
case 'h':
print_help();
exit(STATE_UNKNOWN);
@@ -257,18 +254,7 @@ int process_arguments(int argc, char **argv) {
/* run an apt-get upgrade */
int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist) {
- int result = STATE_UNKNOWN;
- int regres = 0;
- int pc = 0;
- int spc = 0;
- struct output chld_out;
- struct output chld_err;
- regex_t ireg;
regex_t ereg;
- regex_t sreg;
- char *cmdline = NULL;
- char rerrbuf[64];
-
/* initialize ereg as it is possible it is printed while uninitialized */
memset(&ereg, '\0', sizeof(ereg.buffer));
@@ -276,6 +262,9 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
return STATE_OK;
}
+ int regres = 0;
+ regex_t ireg;
+ char rerrbuf[64];
/* compile the regexps */
if (do_include != NULL) {
regres = regcomp(&ireg, do_include, REG_EXTENDED);
@@ -293,6 +282,7 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
}
}
+ regex_t sreg;
const char *crit_ptr = (do_critical != NULL) ? do_critical : SECURITY_RE;
regres = regcomp(&sreg, crit_ptr, REG_EXTENDED);
if (regres != 0) {
@@ -300,6 +290,10 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
}
+ int result = STATE_UNKNOWN;
+ struct output chld_out;
+ struct output chld_err;
+ char *cmdline = NULL;
cmdline = construct_cmdline(upgrade, upgrade_opts);
if (input_filename != NULL) {
/* read input from a file for testing */
@@ -336,6 +330,8 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
* we may need to switch to the --print-uris output format,
* in which case the logic here will slightly change.
*/
+ int pc = 0;
+ int spc = 0;
for (size_t i = 0; i < chld_out.lines; i++) {
if (verbose) {
printf("%s\n", chld_out.line[i]);
@@ -389,12 +385,12 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
/* run an apt-get update (needs root) */
int run_update(void) {
int result = STATE_UNKNOWN;
- struct output chld_out;
- struct output chld_err;
char *cmdline;
-
/* run the update */
cmdline = construct_cmdline(NO_UPGRADE, update_opts);
+
+ struct output chld_out;
+ struct output chld_err;
result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
/* apt-get update changes exit status if it can't fetch packages.
* since we were explicitly asked to do so, this is treated as
@@ -426,20 +422,16 @@ int run_update(void) {
}
char *pkg_name(char *line) {
- char *start = NULL;
- char *space = NULL;
- char *pkg = NULL;
- int len = 0;
+ char *start = line + strlen(PKGINST_PREFIX);
- start = line + strlen(PKGINST_PREFIX);
- len = strlen(start);
+ int len = strlen(start);
- space = index(start, ' ');
+ char *space = index(start, ' ');
if (space != NULL) {
len = space - start;
}
- pkg = malloc(sizeof(char) * (len + 1));
+ char *pkg = malloc(sizeof(char) * (len + 1));
if (!pkg) {
die(STATE_UNKNOWN, "malloc failed!\n");
}
@@ -475,10 +467,8 @@ char *add_to_regexp(char *expr, const char *next) {
}
char *construct_cmdline(upgrade_type u, const char *opts) {
- int len = 0;
const char *opts_ptr = NULL;
const char *aptcmd = NULL;
- char *cmd = NULL;
switch (u) {
case UPGRADE:
@@ -507,11 +497,12 @@ char *construct_cmdline(upgrade_type u, const char *opts) {
break;
}
+ int len = 0;
len += strlen(PATH_TO_APTGET) + 1; /* "/usr/bin/apt-get " */
len += strlen(opts_ptr) + 1; /* "opts " */
len += strlen(aptcmd) + 1; /* "upgrade\0" */
- cmd = (char *)malloc(sizeof(char) * len);
+ char *cmd = (char *)malloc(sizeof(char) * len);
if (cmd == NULL) {
die(STATE_UNKNOWN, "malloc failed");
}
--
cgit v0.10-9-g596f
From 540d609d845869a5c826f8190bdfed47db1e4259 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lorenz=20K=C3=A4stle?=
<12514511+RincewindsHat@users.noreply.github.com>
Date: Fri, 8 Nov 2024 10:50:22 +0100
Subject: check_apt: clang-format
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index c2c693e..ac89f81 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -1,33 +1,33 @@
/*****************************************************************************
-*
-* Monitoring check_apt plugin
-*
-* License: GPL
-* Copyright (c) 2006-2024 Monitoring Plugins Development Team
-*
-* Original author: Sean Finney
-*
-* Description:
-*
-* This file contains the check_apt plugin
-*
-* Check for available updates in apt package management systems
-*
-*
-* 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 3 of the License, 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, see .
-*
-*****************************************************************************/
+ *
+ * Monitoring check_apt plugin
+ *
+ * License: GPL
+ * Copyright (c) 2006-2024 Monitoring Plugins Development Team
+ *
+ * Original author: Sean Finney
+ *
+ * Description:
+ *
+ * This file contains the check_apt plugin
+ *
+ * Check for available updates in apt package management systems
+ *
+ *
+ * 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 3 of the License, 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, see .
+ *
+ *****************************************************************************/
const char *progname = "check_apt";
const char *copyright = "2006-2024";
@@ -39,7 +39,11 @@ const char *email = "devel@monitoring-plugins.org";
#include "regex.h"
/* some constants */
-typedef enum { UPGRADE, DIST_UPGRADE, NO_UPGRADE } upgrade_type;
+typedef enum {
+ UPGRADE,
+ DIST_UPGRADE,
+ NO_UPGRADE
+} upgrade_type;
/* Character for hidden input file option (for testing). */
#define INPUT_FILE_OPT CHAR_MAX + 1
--
cgit v0.10-9-g596f
From 4079ba3c474054af6c8bb2c3b426540b2de01628 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lorenz=20K=C3=A4stle?=
<12514511+RincewindsHat@users.noreply.github.com>
Date: Fri, 8 Nov 2024 11:37:30 +0100
Subject: check_apt: Purify run_upgrade function
diff --git a/plugins/check_apt.c b/plugins/check_apt.c
index ac89f81..1eda45d 100644
--- a/plugins/check_apt.c
+++ b/plugins/check_apt.c
@@ -69,8 +69,18 @@ void print_usage(void);
static char *construct_cmdline(upgrade_type u, const char *opts);
/* run an apt-get update */
static int run_update(void);
+
+typedef struct {
+ int errorcode;
+ int package_count;
+ int security_package_count;
+ char **packages_list;
+ char **secpackages_list;
+} run_upgrade_result;
+
/* run an apt-get upgrade */
-static int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist);
+static run_upgrade_result run_upgrade(void);
+
/* add another clause to a regexp */
static char *add_to_regexp(char *expr, const char *next);
/* extract package name from Inst line */
@@ -119,12 +129,14 @@ int main(int argc, char **argv) {
result = run_update();
}
- int packages_available = 0;
- int sec_count = 0;
- char **packages_list = NULL;
- char **secpackages_list = NULL;
/* apt-get upgrade */
- result = max_state(result, run_upgrade(&packages_available, &sec_count, &packages_list, &secpackages_list));
+ run_upgrade_result upgrad_res = run_upgrade();
+
+ result = max_state(result, upgrad_res.errorcode);
+ int packages_available = upgrad_res.package_count;
+ int sec_count = upgrad_res.security_package_count;
+ char **packages_list = upgrad_res.packages_list;
+ char **secpackages_list = upgrad_res.secpackages_list;
if (sec_count > 0) {
result = max_state(result, STATE_CRITICAL);
@@ -257,13 +269,18 @@ int process_arguments(int argc, char **argv) {
}
/* run an apt-get upgrade */
-int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkglist) {
+run_upgrade_result run_upgrade(void) {
regex_t ereg;
/* initialize ereg as it is possible it is printed while uninitialized */
memset(&ereg, '\0', sizeof(ereg.buffer));
+ run_upgrade_result result = {
+ .errorcode = STATE_UNKNOWN,
+ };
+
if (upgrade == NO_UPGRADE) {
- return STATE_OK;
+ result.errorcode = STATE_OK;
+ return result;
}
int regres = 0;
@@ -294,33 +311,32 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf);
}
- int result = STATE_UNKNOWN;
struct output chld_out;
struct output chld_err;
char *cmdline = NULL;
cmdline = construct_cmdline(upgrade, upgrade_opts);
if (input_filename != NULL) {
/* read input from a file for testing */
- result = cmd_file_read(input_filename, &chld_out, 0);
+ result.errorcode = cmd_file_read(input_filename, &chld_out, 0);
} else {
/* run the upgrade */
- result = np_runcmd(cmdline, &chld_out, &chld_err, 0);
+ result.errorcode = np_runcmd(cmdline, &chld_out, &chld_err, 0);
}
/* apt-get upgrade only changes exit status if there is an
* internal error when run in dry-run mode. therefore we will
* treat such an error as UNKNOWN */
- if (result != 0) {
+ if (result.errorcode != STATE_OK) {
exec_warning = 1;
- result = STATE_UNKNOWN;
+ result.errorcode = STATE_UNKNOWN;
fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline);
}
- *pkglist = malloc(sizeof(char *) * chld_out.lines);
+ char **pkglist = malloc(sizeof(char *) * chld_out.lines);
if (!pkglist) {
die(STATE_UNKNOWN, "malloc failed!\n");
}
- *secpkglist = malloc(sizeof(char *) * chld_out.lines);
+ char **secpkglist = malloc(sizeof(char *) * chld_out.lines);
if (!secpkglist) {
die(STATE_UNKNOWN, "malloc failed!\n");
}
@@ -334,8 +350,8 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
* we may need to switch to the --print-uris output format,
* in which case the logic here will slightly change.
*/
- int pc = 0;
- int spc = 0;
+ int package_counter = 0;
+ int security_package_counter = 0;
for (size_t i = 0; i < chld_out.lines; i++) {
if (verbose) {
printf("%s\n", chld_out.line[i]);
@@ -346,15 +362,15 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
/* if we're not excluding, or it's not in the
* list of stuff to exclude */
if (do_exclude == NULL || regexec(&ereg, chld_out.line[i], 0, NULL, 0) != 0) {
- pc++;
+ package_counter++;
if (regexec(&sreg, chld_out.line[i], 0, NULL, 0) == 0) {
- spc++;
+ security_package_counter++;
if (verbose) {
printf("*");
}
- (*secpkglist)[spc - 1] = pkg_name(chld_out.line[i]);
+ (secpkglist)[security_package_counter - 1] = pkg_name(chld_out.line[i]);
} else {
- (*pkglist)[pc - spc - 1] = pkg_name(chld_out.line[i]);
+ (pkglist)[package_counter - security_package_counter - 1] = pkg_name(chld_out.line[i]);
}
if (verbose) {
printf("*%s\n", chld_out.line[i]);
@@ -362,13 +378,15 @@ int run_upgrade(int *pkgcount, int *secpkgcount, char ***pkglist, char ***secpkg
}
}
}
- *pkgcount = pc;
- *secpkgcount = spc;
+ result.package_count = package_counter;
+ result.security_package_count = security_package_counter;
+ result.packages_list = pkglist;
+ result.secpackages_list = secpkglist;
/* If we get anything on stderr, at least set warning */
if (input_filename == NULL && chld_err.buflen) {
stderr_warning = 1;
- result = max_state(result, STATE_WARNING);
+ result.errorcode = max_state(result.errorcode, STATE_WARNING);
if (verbose) {
for (size_t i = 0; i < chld_err.lines; i++) {
fprintf(stderr, "%s\n", chld_err.line[i]);
--
cgit v0.10-9-g596f