mirror of
https://github.com/troglobit/finit.git
synced 2026-10-21 11:29:42 +00:00
Replace atoi() with atonum() --> better error handling.
Friends don't let friends use atoi(). Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
ccdab464c3
commit
2adec264af
2 files changed
+31
-5
No files matched your search
@@ -116,6 +116,26 @@ void ifconfig(char *ifname, char *addr, char *mask, int up)
|
||||
close(sock);
|
||||
}
|
||||
|
||||
/**
|
||||
* atonum - Convert string to integer
|
||||
* @str: String value to convert.
|
||||
*
|
||||
* Returns:
|
||||
* The converted (positive) number, or -1 on error, with @errno set.
|
||||
*/
|
||||
int atonum(char *str)
|
||||
{
|
||||
int val;
|
||||
char *end;
|
||||
|
||||
errno = 0;
|
||||
val = strtol(str, &end, 10);
|
||||
if (errno || end == str)
|
||||
return -1;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pidfile_read - Reads a PID value from a pidfile.
|
||||
@@ -294,11 +314,15 @@ int procname_kill(char *name, int signo)
|
||||
|
||||
if (strncmp(pname, name, strlen(pname) - 1) == 0) {
|
||||
int error = errno;
|
||||
int pid = atonum(entry->d_name);
|
||||
|
||||
if (kill(atoi(entry->d_name), signo)) {
|
||||
FLOG_ERROR("Failed signalling(%d) %s: %s!", signo, name, strerror(error));
|
||||
if (-1 == pid) {
|
||||
FLOG_ERROR("Failed converting %s to a PID: %s", entry->d_name, strerror(error));
|
||||
} else {
|
||||
result++; /* Track number of processes we deliver the signal to. */
|
||||
if (kill(pid, signo))
|
||||
FLOG_ERROR("Failed signalling(%d) %s: %s!", signo, name, strerror(error));
|
||||
else
|
||||
result++; /* Track number of processes we deliver the signal to. */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,11 @@
|
||||
#include <syslog.h>
|
||||
|
||||
#define DO_LOG(level, fmt, args...) \
|
||||
{ \
|
||||
do { \
|
||||
openlog("finit", LOG_CONS | LOG_PID, LOG_DAEMON); \
|
||||
syslog(level, fmt, ##args); \
|
||||
closelog(); \
|
||||
}
|
||||
} while (0)
|
||||
|
||||
#define FLOG_DEBUG(fmt, args...) DO_LOG(LOG_DEBUG, fmt, ##args)
|
||||
#define FLOG_INFO(fmt, args...) DO_LOG(LOG_INFO, fmt, ##args)
|
||||
@@ -94,6 +94,8 @@ extern int verbose;
|
||||
int makepath (char *path);
|
||||
void ifconfig (char *ifname, char *addr, char *mask, int up);
|
||||
|
||||
int atonum (char *str);
|
||||
|
||||
pid_t pidfile_read (char *pidfile);
|
||||
pid_t pidfile_poll (char *cmd, char *path);
|
||||
|
||||
|
||||
Reference in new issue
Block a user