Merge pull request #59 from giuseppe/fix-lgtm-issues

fix issues reported by lgtm.com
This commit is contained in:
Daniel J Walsh 2019-07-29 15:28:23 -04:00 committed by GitHub
commit 7fdfeea6bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 29 deletions

View File

@ -994,12 +994,12 @@ static int setup_terminal_control_fifo()
return dummyfd;
}
static void setup_oom_handling_cgroup_v2(int container_pid)
static void setup_oom_handling_cgroup_v2(int pid)
{
_cleanup_close_ int ifd = -1;
int wd;
cgroup2_path = process_cgroup_subsystem_path(container_pid, true, "");
cgroup2_path = process_cgroup_subsystem_path(pid, true, "");
if (!cgroup2_path) {
nwarn("Failed to get cgroup path. Container may have exited");
return;
@ -1024,14 +1024,14 @@ static void setup_oom_handling_cgroup_v2(int container_pid)
g_unix_fd_add(inotify_fd, G_IO_IN, oom_cb_cgroup_v2, NULL);
}
static void setup_oom_handling_cgroup_v1(int container_pid)
static void setup_oom_handling_cgroup_v1(int pid)
{
/* Setup OOM notification for container process */
_cleanup_free_ char *memory_cgroup_path = NULL;
_cleanup_close_ int cfd = -1;
int ofd = -1; /* Not closed */
memory_cgroup_path = process_cgroup_subsystem_path(container_pid, false, "memory");
memory_cgroup_path = process_cgroup_subsystem_path(pid, false, "memory");
if (!memory_cgroup_path) {
nwarn("Failed to get memory cgroup path. Container may have exited");
return;
@ -1058,16 +1058,16 @@ static void setup_oom_handling_cgroup_v1(int container_pid)
g_unix_fd_add(oom_event_fd, G_IO_IN, oom_cb_cgroup_v1, NULL);
}
static void setup_oom_handling(int container_pid)
static void setup_oom_handling(int pid)
{
struct statfs sfs;
if (statfs("/sys/fs/cgroup", &sfs) == 0 && sfs.f_type == CGROUP2_SUPER_MAGIC) {
is_cgroup_v2 = TRUE;
setup_oom_handling_cgroup_v2(container_pid);
setup_oom_handling_cgroup_v2(pid);
return;
}
setup_oom_handling_cgroup_v1(container_pid);
setup_oom_handling_cgroup_v1(pid);
}
static void do_exit_command()
@ -1389,7 +1389,6 @@ int main(int argc, char *argv[])
if (create_pid < 0) {
pexit("Failed to fork the create command");
} else if (!create_pid) {
/* FIXME: This results in us not outputting runc error messages to crio's log. */
if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0)
pexit("Failed to set PDEATHSIG");
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)

View File

@ -412,11 +412,12 @@ static const char *stdpipe_name(stdpipe_t pipe)
static int set_k8s_timestamp(char *buf, ssize_t buflen, const char *pipename)
{
struct tm *tm;
static int tzset_called = 0;
struct tm tm;
struct timespec ts;
char off_sign = '+';
int off, len, err = -1;
if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
/* If CLOCK_REALTIME is not supported, we set nano seconds to 0 */
if (errno == EINVAL) {
@ -426,18 +427,23 @@ static int set_k8s_timestamp(char *buf, ssize_t buflen, const char *pipename)
}
}
if ((tm = localtime(&ts.tv_sec)) == NULL)
if (!tzset_called) {
tzset();
tzset_called = 1;
}
if (localtime_r(&ts.tv_sec, &tm) == NULL)
return err;
off = (int)tm->tm_gmtoff;
if (tm->tm_gmtoff < 0) {
off = (int)tm.tm_gmtoff;
if (tm.tm_gmtoff < 0) {
off_sign = '-';
off = -off;
}
len = snprintf(buf, buflen, "%d-%02d-%02dT%02d:%02d:%02d.%09ld%c%02d:%02d %s ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec, off_sign, off / 3600, (off % 3600) / 60, pipename);
len = snprintf(buf, buflen, "%d-%02d-%02dT%02d:%02d:%02d.%09ld%c%02d:%02d %s ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec, off_sign, off / 3600, (off % 3600) / 60, pipename);
if (len < buflen)
err = 0;

View File

@ -2,14 +2,14 @@
#include <string.h>
log_level_t log_level = WARN_LEVEL;
char *cid = NULL;
char *log_cid = NULL;
gboolean use_syslog = FALSE;
/* Set the log level for this call. log level defaults to warning.
parse the string value of level_name to the appropriate log_level_t enum value
*/
void set_conmon_logs(char *level_name, char *cid_, gboolean syslog_)
{
cid = cid_;
log_cid = cid_;
use_syslog = syslog_;
// log_level is initialized as Warning, no need to set anything
if (level_name == NULL)

View File

@ -30,14 +30,14 @@ typedef enum {
// Default log level is Warning, This will be configured before any logging
// should happen
extern log_level_t log_level;
extern char *cid;
extern char *log_cid;
extern gboolean use_syslog;
#define pexit(s) \
do { \
fprintf(stderr, "[conmon:e]: %s %s\n", s, strerror(errno)); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: %s %s\n", cid, s, strerror(errno)); \
syslog(LOG_ERR, "conmon %.20s <error>: %s %s\n", log_cid, s, strerror(errno)); \
exit(EXIT_FAILURE); \
} while (0)
@ -45,7 +45,7 @@ extern gboolean use_syslog;
do { \
fprintf(stderr, "[conmon:e]: " fmt " %s\n", ##__VA_ARGS__, strerror(errno)); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: " fmt ": %s\n", cid, ##__VA_ARGS__, strerror(errno)); \
syslog(LOG_ERR, "conmon %.20s <error>: " fmt ": %s\n", log_cid, ##__VA_ARGS__, strerror(errno)); \
exit(EXIT_FAILURE); \
} while (0)
@ -53,14 +53,14 @@ extern gboolean use_syslog;
do { \
fprintf(stderr, "[conmon:w]: %s %s\n", s, strerror(errno)); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <pwarn>: %s %s\n", cid, s, strerror(errno)); \
syslog(LOG_INFO, "conmon %.20s <pwarn>: %s %s\n", log_cid, s, strerror(errno)); \
} while (0)
#define nexit(s) \
do { \
fprintf(stderr, "[conmon:e] %s\n", s); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: %s\n", cid, s); \
syslog(LOG_ERR, "conmon %.20s <error>: %s\n", log_cid, s); \
exit(EXIT_FAILURE); \
} while (0)
@ -68,7 +68,7 @@ extern gboolean use_syslog;
do { \
fprintf(stderr, "[conmon:e]: " fmt "\n", ##__VA_ARGS__); \
if (use_syslog) \
syslog(LOG_ERR, "conmon %.20s <error>: " fmt " \n", cid, ##__VA_ARGS__); \
syslog(LOG_ERR, "conmon %.20s <error>: " fmt " \n", log_cid, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
@ -77,7 +77,7 @@ extern gboolean use_syslog;
do { \
fprintf(stderr, "[conmon:w]: %s\n", s); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <nwarn>: %s\n", cid, s); \
syslog(LOG_INFO, "conmon %.20s <nwarn>: %s\n", log_cid, s); \
} while (0); \
}
@ -86,7 +86,7 @@ extern gboolean use_syslog;
do { \
fprintf(stderr, "[conmon:w]: " fmt "\n", ##__VA_ARGS__); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <nwarn>: " fmt " \n", cid, ##__VA_ARGS__); \
syslog(LOG_INFO, "conmon %.20s <nwarn>: " fmt " \n", log_cid, ##__VA_ARGS__); \
} while (0); \
}
@ -95,7 +95,7 @@ extern gboolean use_syslog;
do { \
fprintf(stderr, "[conmon:i]: %s\n", s); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ninfo>: %s\n", cid, s); \
syslog(LOG_INFO, "conmon %.20s <ninfo>: %s\n", log_cid, s); \
} while (0); \
}
@ -104,7 +104,7 @@ extern gboolean use_syslog;
do { \
fprintf(stderr, "[conmon:i]: " fmt "\n", ##__VA_ARGS__); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ninfo>: " fmt " \n", cid, ##__VA_ARGS__); \
syslog(LOG_INFO, "conmon %.20s <ninfo>: " fmt " \n", log_cid, ##__VA_ARGS__); \
} while (0); \
}
@ -113,7 +113,7 @@ extern gboolean use_syslog;
do { \
fprintf(stderr, "[conmon:d]: %s\n", s); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ndebug>: %s\n", cid, s); \
syslog(LOG_INFO, "conmon %.20s <ndebug>: %s\n", log_cid, s); \
} while (0); \
}
@ -122,7 +122,7 @@ extern gboolean use_syslog;
do { \
fprintf(stderr, "[conmon:d]: " fmt "\n", ##__VA_ARGS__); \
if (use_syslog) \
syslog(LOG_INFO, "conmon %.20s <ndebug>: " fmt " \n", cid, ##__VA_ARGS__); \
syslog(LOG_INFO, "conmon %.20s <ndebug>: " fmt " \n", log_cid, ##__VA_ARGS__); \
} while (0); \
}