You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
util-linux/misc-utils/logger.c

1317 lines
34 KiB

/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
* - added Native Language Support
* Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
* - fixed strerr(errno) in gettext calls
*/
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <getopt.h>
#include <pwd.h>
#include <signal.h>
#include <sys/uio.h>
#include "all-io.h"
#include "c.h"
#include "closestream.h"
#include "nls.h"
#include "pathnames.h"
#include "strutils.h"
#include "xalloc.h"
#include "strv.h"
#include "list.h"
#define SYSLOG_NAMES
#include <syslog.h>
#ifdef HAVE_LIBSYSTEMD
# define SD_JOURNAL_SUPPRESS_LOCATION
logger: add --socket-errors compatibility option Hello, Depending viewpoint this change is either regression fix, or re-regression in context of none-systemd init. I ack the change is sent very late to be part of v2.26, but then again the excess noise was found only because of -rc1 was tested in sysvinit environment. IMHO it would contradict purpose of having rc's if faults will not lead to fixes. I also want to point out the sysvinit scripts are broken, not the logger(1), but getting them corrected is practically impossible. Assuming sysvinit script are further developed by various teams and distributions who maintain them they should use --socket-error=on in future, and write scripts that pass without noise. Meanwhile trying to be clever when to silence errors seems like a reasonable thing to do. --->8---- From: Sami Kerola <kerolasa@iki.fi> Date: Sat, 14 Feb 2015 19:05:55 +0000 Subject: [PATCH] logger: add --socket-errors compatibility option Now when logger(1) has stopped using openlog() for Unix sockets, in commit mentioned in reference, the lack of /dev/log detected will report error accordingly. According to Gabriele Balducci this makes sysvinit style boot scripts to print a lot of errors. So make the logger to detect whether it should be in compatibility mode, and not report errors if logging device is missing. That imitates behavior of glibc openlog(). To allow full control to users the /dev/log error messages can be forced to on or off. The automatic error messaging is explained in manual page. Reference: 1d57503378bdcd838365d625f6d2d0a09da9c29d Reported-by: Gabriele Balducci <balducci@units.it> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
8 years ago
# include <systemd/sd-daemon.h>
# include <systemd/sd-journal.h>
#endif
#ifdef HAVE_SYS_TIMEX_H
# include <sys/timex.h>
#endif
enum {
TYPE_UDP = (1 << 1),
TYPE_TCP = (1 << 2),
ALL_TYPES = TYPE_UDP | TYPE_TCP
};
logger: add --socket-errors compatibility option Hello, Depending viewpoint this change is either regression fix, or re-regression in context of none-systemd init. I ack the change is sent very late to be part of v2.26, but then again the excess noise was found only because of -rc1 was tested in sysvinit environment. IMHO it would contradict purpose of having rc's if faults will not lead to fixes. I also want to point out the sysvinit scripts are broken, not the logger(1), but getting them corrected is practically impossible. Assuming sysvinit script are further developed by various teams and distributions who maintain them they should use --socket-error=on in future, and write scripts that pass without noise. Meanwhile trying to be clever when to silence errors seems like a reasonable thing to do. --->8---- From: Sami Kerola <kerolasa@iki.fi> Date: Sat, 14 Feb 2015 19:05:55 +0000 Subject: [PATCH] logger: add --socket-errors compatibility option Now when logger(1) has stopped using openlog() for Unix sockets, in commit mentioned in reference, the lack of /dev/log detected will report error accordingly. According to Gabriele Balducci this makes sysvinit style boot scripts to print a lot of errors. So make the logger to detect whether it should be in compatibility mode, and not report errors if logging device is missing. That imitates behavior of glibc openlog(). To allow full control to users the /dev/log error messages can be forced to on or off. The automatic error messaging is explained in manual page. Reference: 1d57503378bdcd838365d625f6d2d0a09da9c29d Reported-by: Gabriele Balducci <balducci@units.it> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
8 years ago
enum {
AF_UNIX_ERRORS_OFF = 0,
AF_UNIX_ERRORS_ON,
AF_UNIX_ERRORS_AUTO
};
enum {
OPT_PRIO_PREFIX = CHAR_MAX + 1,
OPT_JOURNALD,
OPT_RFC3164,
logger: add --socket-errors compatibility option Hello, Depending viewpoint this change is either regression fix, or re-regression in context of none-systemd init. I ack the change is sent very late to be part of v2.26, but then again the excess noise was found only because of -rc1 was tested in sysvinit environment. IMHO it would contradict purpose of having rc's if faults will not lead to fixes. I also want to point out the sysvinit scripts are broken, not the logger(1), but getting them corrected is practically impossible. Assuming sysvinit script are further developed by various teams and distributions who maintain them they should use --socket-error=on in future, and write scripts that pass without noise. Meanwhile trying to be clever when to silence errors seems like a reasonable thing to do. --->8---- From: Sami Kerola <kerolasa@iki.fi> Date: Sat, 14 Feb 2015 19:05:55 +0000 Subject: [PATCH] logger: add --socket-errors compatibility option Now when logger(1) has stopped using openlog() for Unix sockets, in commit mentioned in reference, the lack of /dev/log detected will report error accordingly. According to Gabriele Balducci this makes sysvinit style boot scripts to print a lot of errors. So make the logger to detect whether it should be in compatibility mode, and not report errors if logging device is missing. That imitates behavior of glibc openlog(). To allow full control to users the /dev/log error messages can be forced to on or off. The automatic error messaging is explained in manual page. Reference: 1d57503378bdcd838365d625f6d2d0a09da9c29d Reported-by: Gabriele Balducci <balducci@units.it> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
8 years ago
OPT_RFC5424,
OPT_SOCKET_ERRORS,
OPT_MSGID,
OPT_NOACT,
OPT_ID,
OPT_STRUCTURED_DATA_ID,
OPT_STRUCTURED_DATA_PARAM,
OPT_OCTET_COUNT
};
/* rfc5424 structured data */
struct structured_data {
char *id; /* SD-ID */
char **params; /* array with SD-PARAMs */
struct list_head sds;
};
struct logger_ctl {
int fd;
int pri;
pid_t pid; /* zero when unwanted */
char *hdr; /* the syslog header (based on protocol) */
char const *tag;
char *msgid;
char *unix_socket; /* -u <path> or default to _PATH_DEVLOG */
char *server;
char *port;
int socket_type;
size_t max_message_size;
struct list_head user_sds; /* user defined rfc5424 structured data */
struct list_head reserved_sds; /* standard rfc5424 structured data */
void (*syslogfp)(struct logger_ctl *ctl);
unsigned int
logger: add --socket-errors compatibility option Hello, Depending viewpoint this change is either regression fix, or re-regression in context of none-systemd init. I ack the change is sent very late to be part of v2.26, but then again the excess noise was found only because of -rc1 was tested in sysvinit environment. IMHO it would contradict purpose of having rc's if faults will not lead to fixes. I also want to point out the sysvinit scripts are broken, not the logger(1), but getting them corrected is practically impossible. Assuming sysvinit script are further developed by various teams and distributions who maintain them they should use --socket-error=on in future, and write scripts that pass without noise. Meanwhile trying to be clever when to silence errors seems like a reasonable thing to do. --->8---- From: Sami Kerola <kerolasa@iki.fi> Date: Sat, 14 Feb 2015 19:05:55 +0000 Subject: [PATCH] logger: add --socket-errors compatibility option Now when logger(1) has stopped using openlog() for Unix sockets, in commit mentioned in reference, the lack of /dev/log detected will report error accordingly. According to Gabriele Balducci this makes sysvinit style boot scripts to print a lot of errors. So make the logger to detect whether it should be in compatibility mode, and not report errors if logging device is missing. That imitates behavior of glibc openlog(). To allow full control to users the /dev/log error messages can be forced to on or off. The automatic error messaging is explained in manual page. Reference: 1d57503378bdcd838365d625f6d2d0a09da9c29d Reported-by: Gabriele Balducci <balducci@units.it> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
8 years ago
unix_socket_errors:1, /* whether to report or not errors */
noact:1, /* do not write to sockets */
prio_prefix:1, /* read priority from input */
logger: add --socket-errors compatibility option Hello, Depending viewpoint this change is either regression fix, or re-regression in context of none-systemd init. I ack the change is sent very late to be part of v2.26, but then again the excess noise was found only because of -rc1 was tested in sysvinit environment. IMHO it would contradict purpose of having rc's if faults will not lead to fixes. I also want to point out the sysvinit scripts are broken, not the logger(1), but getting them corrected is practically impossible. Assuming sysvinit script are further developed by various teams and distributions who maintain them they should use --socket-error=on in future, and write scripts that pass without noise. Meanwhile trying to be clever when to silence errors seems like a reasonable thing to do. --->8---- From: Sami Kerola <kerolasa@iki.fi> Date: Sat, 14 Feb 2015 19:05:55 +0000 Subject: [PATCH] logger: add --socket-errors compatibility option Now when logger(1) has stopped using openlog() for Unix sockets, in commit mentioned in reference, the lack of /dev/log detected will report error accordingly. According to Gabriele Balducci this makes sysvinit style boot scripts to print a lot of errors. So make the logger to detect whether it should be in compatibility mode, and not report errors if logging device is missing. That imitates behavior of glibc openlog(). To allow full control to users the /dev/log error messages can be forced to on or off. The automatic error messaging is explained in manual page. Reference: 1d57503378bdcd838365d625f6d2d0a09da9c29d Reported-by: Gabriele Balducci <balducci@units.it> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
8 years ago
stderr_printout:1, /* output message to stderr */
rfc5424_time:1, /* include time stamp */
rfc5424_tq:1, /* include time quality markup */
rfc5424_host:1, /* include hostname */
skip_empty_lines:1, /* do not send empty lines when processing files */
octet_count:1; /* use RFC6587 octet counting */
};
#define is_connected(_ctl) ((_ctl)->fd >= 0)
static void logger_reopen(struct logger_ctl *ctl);
/*
* For tests we want to be able to control datetime outputs
*/
#ifdef TEST_LOGGER
static inline int logger_gettimeofday(struct timeval *tv, struct timezone *tz)
{
char *str = getenv("LOGGER_TEST_TIMEOFDAY");
uintmax_t sec, usec;
if (str && sscanf(str, "%ju.%ju", &sec, &usec) == 2) {
tv->tv_sec = sec;
tv->tv_usec = usec;
return tv->tv_sec >= 0 && tv->tv_usec >= 0 ? 0 : -EINVAL;
}
return gettimeofday(tv, tz);
}
static inline char *logger_xgethostname(void)
{
char *str = getenv("LOGGER_TEST_HOSTNAME");
return str ? xstrdup(str) : xgethostname();
}
static inline pid_t logger_getpid(void)
{
char *str = getenv("LOGGER_TEST_GETPID");
unsigned int pid;
if (str && sscanf(str, "%u", &pid) == 1)
return pid;
return getpid();
}
#undef HAVE_NTP_GETTIME /* force to default non-NTP */
#else /* !TEST_LOGGER */
# define logger_gettimeofday(x, y) gettimeofday(x, y)
# define logger_xgethostname xgethostname
# define logger_getpid getpid
#endif
static int decode(const char *name, const CODE *codetab)
{
register const CODE *c;
if (name == NULL || *name == '\0')
return -1;
if (isdigit(*name)) {
int num;
char *end = NULL;
errno = 0;
num = strtol(name, &end, 10);
if (errno || name == end || (end && *end))
return -1;
for (c = codetab; c->c_name; c++)
if (num == c->c_val)
return num;
return -1;
}
for (c = codetab; c->c_name; c++)
if (!strcasecmp(name, c->c_name))
return (c->c_val);
return -1;
}
static int pencode(char *s)
{
int facility, level;
char *separator;
assert(s);
separator = strchr(s, '.');
if (separator) {
*separator = '\0';
facility = decode(s, facilitynames);
if (facility < 0)
errx(EXIT_FAILURE, _("unknown facility name: %s"), s);
s = ++separator;
} else
facility = LOG_USER;
level = decode(s, prioritynames);
if (level < 0)
errx(EXIT_FAILURE, _("unknown priority name: %s"), s);
if (facility == LOG_KERN)
facility = LOG_USER; /* kern is forbidden */
return ((level & LOG_PRIMASK) | (facility & LOG_FACMASK));
}
static int unix_socket(struct logger_ctl *ctl, const char *path, int *socket_type)
{
int fd = -1, i, type = -1;
static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
if (strlen(path) >= sizeof(s_addr.sun_path))
errx(EXIT_FAILURE, _("openlog %s: pathname too long"), path);
s_addr.sun_family = AF_UNIX;
strcpy(s_addr.sun_path, path);
for (i = 2; i; i--) {
int st = -1;
if (i == 2 && *socket_type & TYPE_UDP) {
st = SOCK_DGRAM;
type = TYPE_UDP;
}
if (i == 1 && *socket_type & TYPE_TCP) {
st = SOCK_STREAM;
type = TYPE_TCP;
}
if (st == -1 || (fd = socket(AF_UNIX, st, 0)) == -1)
continue;
if (connect(fd, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1) {
close(fd);
continue;
}
break;
}
logger: add --socket-errors compatibility option Hello, Depending viewpoint this change is either regression fix, or re-regression in context of none-systemd init. I ack the change is sent very late to be part of v2.26, but then again the excess noise was found only because of -rc1 was tested in sysvinit environment. IMHO it would contradict purpose of having rc's if faults will not lead to fixes. I also want to point out the sysvinit scripts are broken, not the logger(1), but getting them corrected is practically impossible. Assuming sysvinit script are further developed by various teams and distributions who maintain them they should use --socket-error=on in future, and write scripts that pass without noise. Meanwhile trying to be clever when to silence errors seems like a reasonable thing to do. --->8---- From: Sami Kerola <kerolasa@iki.fi> Date: Sat, 14 Feb 2015 19:05:55 +0000 Subject: [PATCH] logger: add --socket-errors compatibility option Now when logger(1) has stopped using openlog() for Unix sockets, in commit mentioned in reference, the lack of /dev/log detected will report error accordingly. According to Gabriele Balducci this makes sysvinit style boot scripts to print a lot of errors. So make the logger to detect whether it should be in compatibility mode, and not report errors if logging device is missing. That imitates behavior of glibc openlog(). To allow full control to users the /dev/log error messages can be forced to on or off. The automatic error messaging is explained in manual page. Reference: 1d57503378bdcd838365d625f6d2d0a09da9c29d Reported-by: Gabriele Balducci <balducci@units.it> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
8 years ago
if (i == 0) {
if (ctl->unix_socket_errors)
err(EXIT_FAILURE, _("socket %s"), path);
/* write_output() will try to reconnect */
return -1;
logger: add --socket-errors compatibility option Hello, Depending viewpoint this change is either regression fix, or re-regression in context of none-systemd init. I ack the change is sent very late to be part of v2.26, but then again the excess noise was found only because of -rc1 was tested in sysvinit environment. IMHO it would contradict purpose of having rc's if faults will not lead to fixes. I also want to point out the sysvinit scripts are broken, not the logger(1), but getting them corrected is practically impossible. Assuming sysvinit script are further developed by various teams and distributions who maintain them they should use --socket-error=on in future, and write scripts that pass without noise. Meanwhile trying to be clever when to silence errors seems like a reasonable thing to do. --->8---- From: Sami Kerola <kerolasa@iki.fi> Date: Sat, 14 Feb 2015 19:05:55 +0000 Subject: [PATCH] logger: add --socket-errors compatibility option Now when logger(1) has stopped using openlog() for Unix sockets, in commit mentioned in reference, the lack of /dev/log detected will report error accordingly. According to Gabriele Balducci this makes sysvinit style boot scripts to print a lot of errors. So make the logger to detect whether it should be in compatibility mode, and not report errors if logging device is missing. That imitates behavior of glibc openlog(). To allow full control to users the /dev/log error messages can be forced to on or off. The automatic error messaging is explained in manual page. Reference: 1d57503378bdcd838365d625f6d2d0a09da9c29d Reported-by: Gabriele Balducci <balducci@units.it> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
8 years ago
}
/* replace ALL_TYPES with the real TYPE_* */
if (type > 0 && type != *socket_type)
*socket_type = type;
return fd;
}
static int inet_socket(const char *servername, const char *port, int *socket_type)
{
int fd, errcode, i, type = -1;
struct addrinfo hints, *res;
const char *p = port;
for (i = 2; i; i--) {
memset(&hints, 0, sizeof(hints));
if (i == 2 && *socket_type & TYPE_UDP) {
hints.ai_socktype = SOCK_DGRAM;
type = TYPE_UDP;
if (port == NULL)
p = "syslog";
}
if (i == 1 && *socket_type & TYPE_TCP) {
hints.ai_socktype = SOCK_STREAM;
type = TYPE_TCP;
if (port == NULL)
p = "syslog-conn";
}
if (hints.ai_socktype == 0)
continue;
hints.ai_family = AF_UNSPEC;
errcode = getaddrinfo(servername, p, &hints, &res);
if (errcode != 0)
errx(EXIT_FAILURE, _("failed to resolve name %s port %s: %s"),
servername, p, gai_strerror(errcode));
if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
freeaddrinfo(res);
continue;
}
if (connect(fd, res->ai_addr, res->ai_addrlen) == -1) {
freeaddrinfo(res);
close(fd);
continue;
}
freeaddrinfo(res);
break;
}
if (i == 0)
errx(EXIT_FAILURE, _("failed to connect to %s port %s"), servername, p);
/* replace ALL_TYPES with the real TYPE_* */
if (type > 0 && type != *socket_type)
*socket_type = type;
return fd;
}
#ifdef HAVE_LIBSYSTEMD
static int journald_entry(struct logger_ctl *ctl, FILE *fp)
{
struct iovec *iovec;
char *buf = NULL;
ssize_t sz;
int n, lines = 0, vectors = 8, ret = 0, msgline = -1;
size_t dummy = 0;
iovec = xmalloc(vectors * sizeof(struct iovec));
while (1) {
buf = NULL;
sz = getline(&buf, &dummy, fp);
if (sz == -1 ||
(sz = rtrim_whitespace((unsigned char *) buf)) == 0) {
free(buf);
break;
}
if (strncmp(buf, "MESSAGE=", 8) == 0) {
if (msgline == -1)
msgline = lines; /* remember the first message */
else {
char *p = xrealloc(iovec[msgline].iov_base,
iovec[msgline].iov_len + sz - 8 + 2);
iovec[msgline].iov_base = p;
p += iovec[msgline].iov_len;
*p++ = '\n';
memcpy(p, buf + 8, sz - 8);
free(buf);
iovec[msgline].iov_len += sz - 8 + 1;
continue;
}
}
if (lines == vectors) {
vectors *= 2;
if (IOV_MAX < vectors)
errx(EXIT_FAILURE, _("maximum input lines (%d) exceeded"), IOV_MAX);
iovec = xrealloc(iovec, vectors * sizeof(struct iovec));
}
iovec[lines].iov_base = buf;
iovec[lines].iov_len = sz;
++lines;
}
if (!ctl->noact)
ret = sd_journal_sendv(iovec, lines);
if (ctl->stderr_printout) {
for (n = 0; n < lines; n++)
fprintf(stderr, "%s\n", (char *) iovec[n].iov_base);
}
for (n = 0; n < lines; n++)
free(iovec[n].iov_base);
free(iovec);
return ret;
}
#endif
static char const *xgetlogin(void)
{
char const *cp;
struct passwd *pw;
if (!(cp = getlogin()) || !*cp)
cp = (pw = getpwuid(geteuid()))? pw->pw_name : "<someone>";
return cp;
}
/* this creates a timestamp based on current time according to the
* fine rules of RFC3164, most importantly it ensures in a portable
* way that the month day is correctly written (with a SP instead
* of a leading 0). The function uses a static buffer which is
* overwritten on the next call (just like ctime() does).
*/
static char const *rfc3164_current_time(void)
{
static char time[32];
struct timeval tv;
struct tm tm;
static char const * const monthnames[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
};
logger_gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &tm);
snprintf(time, sizeof(time),"%s %2d %2.2d:%2.2d:%2.2d",
monthnames[tm.tm_mon], tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
return time;
}
#define next_iovec(ary, idx) __extension__ ({ \
misc: fix compiler warnungs (unsigned/signed) These ones should be fixed: libblkid/src/probe.c:393:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libblkid/src/probe.c:907:25: warning: signed and unsigned type in conditional expression [-Wsign-compare] libblkid/src/probe.c:1221:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libblkid/src/partitions/partitions.c:540:47: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libblkid/src/partitions/partitions.c:1043:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libblkid/src/partitions/partitions.c:1056:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libblkid/src/partitions/partitions.c:1057:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libblkid/src/partitions/partitions.c:1061:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libblkid/src/partitions/partitions.c:1199:27: warning: signed and unsigned type in conditional expression [-Wsign-compare] libblkid/src/partitions/partitions.c:1410:26: warning: signed and unsigned type in conditional expression [-Wsign-compare] libblkid/src/partitions/partitions.c:1431:25: warning: signed and unsigned type in conditional expression [-Wsign-compare] libblkid/src/superblocks/linux_raid.c:151:8: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits] libblkid/src/superblocks/linux_raid.c:155:2: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits] libblkid/src/superblocks/superblocks.c:375:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libblkid/src/superblocks/xfs.c:141:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libsmartcols/src/table.c:333:24: warning: signed and unsigned type in conditional expression [-Wsign-compare] libsmartcols/src/table.c:344:25: warning: signed and unsigned type in conditional expression [-Wsign-compare] libsmartcols/src/table_print.c:753:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libfdisk/src/ask.c:364:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libfdisk/src/utils.c:33:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libfdisk/src/context.c:435:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libfdisk/src/context.c:730:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libfdisk/src/script.c:557:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libfdisk/src/dos.c:1791:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] libfdisk/src/gpt.c:813:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] disk-utils/partx.c:140:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] disk-utils/partx.c:551:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] disk-utils/partx.c:640:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
7 years ago
assert(ARRAY_SIZE(ary) > (size_t)idx); \
assert(idx >= 0); \
&ary[idx++]; \
})
#define iovec_add_string(ary, idx, str, len) \
do { \
struct iovec *v = next_iovec(ary, idx); \
v->iov_base = (void *) str; \
v->iov_len = len ? len : strlen(str); \
} while (0)
#define iovec_memcmp(ary, idx, str, len) \
memcmp((ary)[(idx) - 1].iov_base, str, len)
/* writes generated buffer to desired destination. For TCP syslog,
* we use RFC6587 octet-stuffing (unless octet-counting is selected).
* This is not great, but doing full blown RFC5425 (TLS) looks like
* it is too much for the logger utility. If octet-counting is
* selected, we use that.
*/
static void write_output(struct logger_ctl *ctl, const char *const msg)
{
struct iovec iov[4];
int iovlen = 0;
char *octet = NULL;
/* initial connect failed? */
if (!ctl->noact && !is_connected(ctl))
logger_reopen(ctl);
/* 1) octen count */
if (ctl->octet_count) {
size_t len = xasprintf(&octet, "%zu ", strlen(ctl->hdr) + strlen(msg));
iovec_add_string(iov, iovlen, octet, len);
}
/* 2) header */
iovec_add_string(iov, iovlen, ctl->hdr, 0);
/* 3) message */
iovec_add_string(iov, iovlen, msg, 0);
if (!ctl->noact && is_connected(ctl)) {
struct msghdr message = { 0 };
#ifdef SCM_CREDENTIALS
struct cmsghdr *cmhp;
struct ucred *cred;
union {
struct cmsghdr cmh;
char control[CMSG_SPACE(sizeof(struct ucred))];
} cbuf;
#endif
/* 4) add extra \n to make sure message is terminated */
if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count)
iovec_add_string(iov, iovlen, "\n", 1);
message.msg_iov = iov;
message.msg_iovlen = iovlen;
#ifdef SCM_CREDENTIALS
/* syslog/journald may follow local socket credentials rather
* than in the message PID. If we use --id as root than we can
* force kernel to accept another valid PID than the real logger(1)
* PID.
*/
if (ctl->pid && !ctl->server && ctl->pid != getpid()
&& geteuid() == 0 && kill(ctl->pid, 0) == 0) {
message.msg_control = cbuf.control;
message.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
cmhp = CMSG_FIRSTHDR(&message);
cmhp->cmsg_len = CMSG_LEN(sizeof(struct ucred));
cmhp->cmsg_level = SOL_SOCKET;
cmhp->cmsg_type = SCM_CREDENTIALS;
cred = (struct ucred *) CMSG_DATA(cmhp);
cred->pid = ctl->pid;
}
#endif
/* Note that logger(1) maybe executed for long time (as pipe
* reader) and connection endpoint (syslogd) may be restarted.
*
* The libc syslog() function reconnects on failed send().
* Let's do the same to be robust. [kzak -- Oct 2017]
*
* MSG_NOSIGNAL is POSIX.1-2008 compatible, but it for example
* not supported by apple-darwin15.6.0.
*/
#ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
#endif
if (sendmsg(ctl->fd, &message, MSG_NOSIGNAL) < 0) {
logger_reopen(ctl);
if (sendmsg(ctl->fd, &message, MSG_NOSIGNAL) < 0)
warn(_("send message failed"));
}
}
if (ctl->stderr_printout) {
/* make sure it's terminated for stderr */
if (iovec_memcmp(iov, iovlen, "\n", 1) != 0)
iovec_add_string(iov, iovlen, "\n", 1);
ignore_result( writev(STDERR_FILENO, iov, iovlen) );
}
free(octet);
}
#define NILVALUE "-"
static void syslog_rfc3164_header(struct logger_ctl *const ctl)
{
char pid[30], *hostname;
*pid = '\0';
if (ctl->pid)
snprintf(pid, sizeof(pid), "[%d]", ctl->pid);
if ((hostname = logger_xgethostname())) {
char *dot = strchr(hostname, '.');
if (dot)
*dot = '\0';
} else
hostname = xstrdup(NILVALUE)<