fprintf(stderr, "%s\n", chld_err.line[i]);
}
}
}
regfree(&ireg);
regfree(&sreg);
if(do_exclude!=NULL) regfree(&ereg);
free(cmdline);
return result;
}
/* run an apt-get update (needs root) */
int run_update(void){
int i=0, result=STATE_UNKNOWN;
struct output chld_out, chld_err;
char *cmdline;
/* run the upgrade */
cmdline = construct_cmdline(NO_UPGRADE, update_opts);
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
* a critical error. */
if(result != 0){
exec_warning=1;
result = STATE_CRITICAL;
fprintf(stderr, _("'%s' exited with non-zero status.\n"),
cmdline);
}
if(verbose){
for(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;
result = max_state(result, STATE_WARNING);
if(verbose){
for(i = 0; i < chld_err.lines; i++) {
fprintf(stderr, "%s\n", chld_err.line[i]);
}
}
}
free(cmdline);
return result;
}
char* add_to_regexp(char *expr, const char *next){
char *re=NULL;
if(expr==NULL){
re=malloc(sizeof(char)*(strlen("^Inst () ")+strlen(next)+1));
if(!re) die(STATE_UNKNOWN, "malloc failed!\n");
sprintf(re, "^Inst (%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");
/* append it starting at ')' in the old re */
sprintf((char*)(re+strlen(re)-2), "|%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;
switch(u){
case 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";
break;
case NO_UPGRADE:
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" */
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;
}
=4= |