Use clicon_err() consistently and drop perror()

This commit is contained in:
Philip Prindeville 2023-08-23 13:48:04 -06:00 committed by Olof Hagsand
parent 9d9528d503
commit 959e060995
5 changed files with 65 additions and 59 deletions

View file

@ -1029,13 +1029,13 @@ main(int argc,
daemonized errors OK. Before this stage, errors are logged on stderr daemonized errors OK. Before this stage, errors are logged on stderr
also */ also */
if (foreground==0){ if (foreground==0){
clicon_log_init(__PROGRAM__, dbg?LOG_DEBUG:LOG_INFO,
logdst==CLICON_LOG_FILE?CLICON_LOG_FILE:CLICON_LOG_SYSLOG);
/* Call plugin callbacks just before fork/daemonization */ /* Call plugin callbacks just before fork/daemonization */
if (clixon_plugin_pre_daemon_all(h) < 0) if (clixon_plugin_pre_daemon_all(h) < 0)
goto done; goto done;
clicon_log_init(__PROGRAM__, dbg?LOG_DEBUG:LOG_INFO,
logdst==CLICON_LOG_FILE?CLICON_LOG_FILE:CLICON_LOG_SYSLOG);
if (daemon(0, 0) < 0){ if (daemon(0, 0) < 0){
fprintf(stderr, "config: daemon"); clicon_err(OE_UNIX, errno, "daemon");
exit(-1); exit(-1);
} }
} }

View file

@ -669,7 +669,7 @@ restconf_clixon_backend(clicon_handle h,
while (1){ while (1){
if (clicon_hello_req(h, "cl:restconf", NULL, &id) < 0){ if (clicon_hello_req(h, "cl:restconf", NULL, &id) < 0){
if (errno == ENOENT){ if (errno == ENOENT){
fprintf(stderr, "waiting"); clicon_err(OE_UNIX, errno, "waiting");
sleep(1); sleep(1);
continue; continue;
} }

View file

@ -214,18 +214,18 @@ clixon_proc_socket(char **argv,
close(sp[0]); close(sp[0]);
close(0); close(0);
if (dup2(sp[1], STDIN_FILENO) < 0){ if (dup2(sp[1], STDIN_FILENO) < 0){
perror("dup2"); clicon_err(OE_UNIX, errno, "dup2(STDIN)");
return -1; return -1;
} }
close(1); close(1);
if (dup2(sp[1], STDOUT_FILENO) < 0){ if (dup2(sp[1], STDOUT_FILENO) < 0){
perror("dup2"); clicon_err(OE_UNIX, errno, "dup2(STDOUT)");
return -1; return -1;
} }
close(sp[1]); close(sp[1]);
if (execvp(argv[0], argv) < 0){ if (execvp(argv[0], argv) < 0){
perror("execvp"); clicon_err(OE_UNIX, errno, "execvp(%s)", argv[0]);
return -1; return -1;
} }
exit(-1); /* Shouldnt reach here */ exit(-1); /* Shouldnt reach here */
@ -366,7 +366,7 @@ clixon_proc_background(char **argv,
goto done; goto done;
} }
if (execvp(argv[0], argv) < 0) { if (execvp(argv[0], argv) < 0) {
clicon_err(OE_UNIX, errno, "execv(%s)", argv[0]); clicon_err(OE_UNIX, errno, "execvp(%s)", argv[0]);
exit(1); exit(1);
} }
/* Not reached */ /* Not reached */
@ -606,19 +606,19 @@ clixon_process_pid(clicon_handle h,
pe = _proc_entry_list; pe = _proc_entry_list;
do { do {
if (strcmp(pe->pe_name, name) == 0) { if (strcmp(pe->pe_name, name) == 0) {
isrunning = 0; isrunning = 0;
if (proc_op_run(pe->pe_pid, &isrunning) < 0) if (proc_op_run(pe->pe_pid, &isrunning) < 0)
goto done; goto done;
if (!isrunning) if (!isrunning)
goto done; goto done;
*pid = pe->pe_pid; *pid = pe->pe_pid;
retval = 0; retval = 0;
break; break;
} }
pe = NEXTQ(process_entry_t *, pe); pe = NEXTQ(process_entry_t *, pe);
} while (pe != _proc_entry_list); } while (pe != _proc_entry_list);
done: done:
@ -657,45 +657,45 @@ clixon_process_operation(clicon_handle h,
goto ok; goto ok;
if ((pe = _proc_entry_list) != NULL) if ((pe = _proc_entry_list) != NULL)
do { do {
if (strcmp(pe->pe_name, name) == 0){ if (strcmp(pe->pe_name, name) == 0){
/* Call wrapper function that eg changes op1 based on config */ /* Call wrapper function that eg changes op1 based on config */
op = op0; op = op0;
if (wrapit && pe->pe_callback != NULL) if (wrapit && pe->pe_callback != NULL)
if (pe->pe_callback(h, pe, &op) < 0) if (pe->pe_callback(h, pe, &op) < 0)
goto done;
if (op == PROC_OP_START || op == PROC_OP_STOP || op == PROC_OP_RESTART){
pe->pe_operation = op;
clicon_debug(1, "%s scheduling name: %s pid:%d op: %s", __FUNCTION__,
name, pe->pe_pid,
clicon_int2str(proc_operation_map, pe->pe_operation));
if (pe->pe_state==PROC_STATE_RUNNING &&
(op == PROC_OP_STOP || op == PROC_OP_RESTART)){
isrunning = 0;
if (proc_op_run(pe->pe_pid, &isrunning) < 0)
goto done; goto done;
if (isrunning) { if (op == PROC_OP_START || op == PROC_OP_STOP || op == PROC_OP_RESTART){
clicon_log(LOG_NOTICE, "Killing old process %s with pid: %d", pe->pe_operation = op;
pe->pe_name, pe->pe_pid); /* XXX pid may be 0 */ clicon_debug(1, "%s scheduling name: %s pid:%d op: %s", __FUNCTION__,
kill(pe->pe_pid, SIGTERM); name, pe->pe_pid,
delay = 1; clicon_int2str(proc_operation_map, pe->pe_operation));
if (pe->pe_state==PROC_STATE_RUNNING &&
(op == PROC_OP_STOP || op == PROC_OP_RESTART)){
isrunning = 0;
if (proc_op_run(pe->pe_pid, &isrunning) < 0)
goto done;
if (isrunning) {
clicon_log(LOG_NOTICE, "Killing old process %s with pid: %d",
pe->pe_name, pe->pe_pid); /* XXX pid may be 0 */
kill(pe->pe_pid, SIGTERM);
delay = 1;
}
clicon_debug(1, "%s %s(%d) %s --%s--> %s", __FUNCTION__,
pe->pe_name, pe->pe_pid,
clicon_int2str(proc_state_map, pe->pe_state),
clicon_int2str(proc_operation_map, pe->pe_operation),
clicon_int2str(proc_state_map, PROC_STATE_EXITING)
);
pe->pe_state = PROC_STATE_EXITING; /* Keep operation stop/restart */
} }
clicon_debug(1, "%s %s(%d) %s --%s--> %s", __FUNCTION__, sched++;/* start: immediate stop/restart: not immediate: wait timeout */
pe->pe_name, pe->pe_pid,
clicon_int2str(proc_state_map, pe->pe_state),
clicon_int2str(proc_operation_map, pe->pe_operation),
clicon_int2str(proc_state_map, PROC_STATE_EXITING)
);
pe->pe_state = PROC_STATE_EXITING; /* Keep operation stop/restart */
} }
sched++;/* start: immediate stop/restart: not immediate: wait timeout */ else{
clicon_debug(1, "%s name:%s op %s cancelled by wrap", __FUNCTION__, name, clicon_int2str(proc_operation_map, op0));
}
break; /* hit break here */
} }
else{ pe = NEXTQ(process_entry_t *, pe);
clicon_debug(1, "%s name:%s op %s cancelled by wrap", __FUNCTION__, name, clicon_int2str(proc_operation_map, op0)); } while (pe != _proc_entry_list);
}
break; /* hit break here */
}
pe = NEXTQ(process_entry_t *, pe);
} while (pe != _proc_entry_list);
if (sched && clixon_process_sched_register(h, delay) < 0) if (sched && clixon_process_sched_register(h, delay) < 0)
goto done; goto done;
ok: ok:

View file

@ -173,6 +173,8 @@ drop_priv_temp(uid_t new_uid)
#ifdef HAVE_GETRESUID #ifdef HAVE_GETRESUID
int retval = -1; int retval = -1;
clicon_debug(CLIXON_DBG_DEFAULT, "%s uid:%u", __FUNCTION__, new_uid);
/* XXX: implicit declaration of function 'setresuid' on travis */ /* XXX: implicit declaration of function 'setresuid' on travis */
if (setresuid(-1, new_uid, geteuid()) < 0){ if (setresuid(-1, new_uid, geteuid()) < 0){
clicon_err(OE_UNIX, errno, "setresuid"); clicon_err(OE_UNIX, errno, "setresuid");
@ -186,7 +188,7 @@ drop_priv_temp(uid_t new_uid)
done: done:
return retval; return retval;
#else #else
clicon_debug(1, "%s Drop privileges not implemented on this platform since getresuid is not available", __FUNCTION__); clicon_debug(CLIXON_DBG_DEFAULT, "%s Drop privileges not implemented on this platform since getresuid is not available", __FUNCTION__);
return 0; return 0;
#endif #endif
} }
@ -203,6 +205,8 @@ drop_priv_perm(uid_t new_uid)
uid_t euid; uid_t euid;
uid_t suid; uid_t suid;
clicon_debug(CLIXON_DBG_DEFAULT, "%s uid:%u", __FUNCTION__, new_uid);
if (setresuid(new_uid, new_uid, new_uid) < 0){ if (setresuid(new_uid, new_uid, new_uid) < 0){
clicon_err(OE_UNIX, errno, "setresuid"); clicon_err(OE_UNIX, errno, "setresuid");
goto done; goto done;
@ -221,7 +225,7 @@ drop_priv_perm(uid_t new_uid)
done: done:
return retval; return retval;
#else #else
clicon_debug(1, "%s Drop privileges not implemented on this platform since getresuid is not available", __FUNCTION__); clicon_debug(CLIXON_DBG_DEFAULT, "%s Drop privileges not implemented on this platform since getresuid is not available", __FUNCTION__);
return 0; return 0;
#endif #endif
} }
@ -236,6 +240,8 @@ restore_priv(void)
uid_t euid; uid_t euid;
uid_t suid; uid_t suid;
clicon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
if (getresuid(&ruid, &euid, &suid) < 0){ if (getresuid(&ruid, &euid, &suid) < 0){
clicon_err(OE_UNIX, errno, "setresuid"); clicon_err(OE_UNIX, errno, "setresuid");
goto done; goto done;
@ -252,7 +258,7 @@ restore_priv(void)
done: done:
return retval; return retval;
#else #else
clicon_debug(1, "%s Drop privileges not implemented on this platform since getresuid is not available", __FUNCTION__); clicon_debug(CLIXON_DBG_DEFAULT, "%s Drop privileges not implemented on this platform since getresuid is not available", __FUNCTION__);
return 0; return 0;
#endif #endif
} }

View file

@ -282,7 +282,7 @@ main(int argc,
xc = xvec[i]; xc = xvec[i];
fprintf(stdout, "%d: ", i); fprintf(stdout, "%d: ", i);
clixon_xml2file(stdout, xc, 0, 0, NULL, fprintf, 0, 0); clixon_xml2file(stdout, xc, 0, 0, NULL, fprintf, 0, 0);
fprintf(stdout, "\n"); fputc('\n', stdout);
fflush(stdout); fflush(stdout);
} }
retval = 0; retval = 0;