From 1438a5288e1e3392c654bbe55f0cf4b65ef603b4 Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Wed, 12 Dec 2012 13:40:02 +0100 Subject: Ready for use. If you run Linux. --- debug_syslog.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 110 insertions(+), 3 deletions(-) diff --git a/debug_syslog.c b/debug_syslog.c index fb7ab2f..c5d9dbc 100644 --- a/debug_syslog.c +++ b/debug_syslog.c @@ -46,9 +46,12 @@ #define SYSLOG_CONTENTLEN SYSLOG_MSGLEN - SYSLOG_PRILEN - SYSLOG_TIMELEN - SYSLOG_TAGLEN //#define SYSLOG_CONTENTLEN SYSLOG_MSGLEN - SYSLOG_PRILEN - SYSLOG_TAGLEN - SYSLOG_HEADERLEN -1 +#define SYSLOG_LENOFEXECNAME 256 static int dbg_syslog_sock; static struct sockaddr_in dbg_syslog_sockaddr; +static pid_t pid; +static char execname[SYSLOG_LENOFEXECNAME]; void dbg_syslog_init(const char* host, int port) { @@ -61,8 +64,32 @@ void dbg_syslog_init(const char* host, int port) dbg_syslog_sockaddr.sin_family = AF_INET; dbg_syslog_sockaddr.sin_addr.s_addr = inet_addr(host); dbg_syslog_sockaddr.sin_port = htons(port); + + // This implementation has all kind of possible errors: + // * It is Linux only + // * Max length of execname (including path, avoid this by cmdline file in reverse order) + // is set to SYSLOG_LENOFEXECNAME + // * If not found execname is "" + pid = getpid(); + char buf[SYSLOG_LENOFEXECNAME]; + FILE* f = fopen("/proc/self/cmdline", "r"); + if(f) { + fgets(buf, SYSLOG_LENOFEXECNAME, f); + fclose(f); + } + + char* bufptr = strrchr(buf, '/'); + if(bufptr) { + strncpy(execname, bufptr+1, SYSLOG_LENOFEXECNAME-1); + } + else { + strcpy(execname, "\0"); + } + + printf("\tRunning as %s with pid %d\n", execname, pid); } +/* void dbg_syslog_createheader() { const time_t rawtime = time(NULL); struct tm time; @@ -73,6 +100,83 @@ void dbg_syslog_createheader() { char bufpri[SYSLOG_PRILEN] = "<20>"; char buftag[SYSLOG_TAGLEN] = "PROGRAM[PID]: "; +} +*/ + +/* + * Syslog message specification (based on rfc3164). + * Modified after reading syslog v2. documentation + * + * MSG: "<%d%d>%s %s %s%c%s", FACILITY, SEVERITY, TIME, CLIENT, TAG, DELIM, CONTENT + * + * Example: + * <34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8 + * + * + * NOTE! Length of MSG must not exceed 1024 characters! + * + * FACILITY: + * 0 kernel messages + * 1 user-level messages + * 2 mail system + * 3 system daemons + * 4 security/authorization messages (note 1) + * 5 messages generated internally by syslogd + * 6 line printer subsystem + * 7 network news subsystem + * 8 UUCP subsystem + * 9 clock daemon (note 2) + * 10 security/authorization messages (note 1) + * 11 FTP daemon + * 12 NTP subsystem + * 13 log audit (note 1) + * 14 log alert (note 1) + * 15 clock daemon (note 2) + * 16 local use 0 (local0) + * 17 local use 1 (local1) + * 18 local use 2 (local2) + * 19 local use 3 (local3) + * 20 local use 4 (local4) + * 21 local use 5 (local5) + * 22 local use 6 (local6) + * 23 local use 7 (local7) + * + * SEVERITY: + * 0 Emergency: system is unusable + * 1 Alert: action must be taken immediately + * 2 Critical: critical conditions + * 3 Error: error conditions + * 4 Warning: warning conditions + * 5 Notice: normal but significant condition + * 6 Informational: informational messages + * 7 Debug: debug-level messages + * + * TIME: + * Time of message formatted like: Mmm dd hh:mm:ss + * + * CLIENT: + * Hostname or + * IPv4 address or + * IPv6 address + * + * TAG: format: PROGRAM[PID] + * Program or process information + * PROGRAM is Alphanumeric + * PID is numeric + * Must not exceed 32 characters + * + * DELIM: + * A nonn-alphanumeric character (eg. ':', '[' ...) + * In implementation DELIM is the first character of CONTENT + * + * CONTENT: + * The message to log. + * String of printable characters + * + * */ + +static void _calculate_pri() { + } void dbg_syslog_output(char* msg) @@ -85,8 +189,11 @@ void dbg_syslog_output(char* msg) char buftime[SYSLOG_TIMELEN]; strftime(buftime, SYSLOG_TIMELEN, "%b %e %H:%M:%S ", &time); - char bufpri[SYSLOG_PRILEN] = "<20>"; - char buftag[SYSLOG_TAGLEN] = "PROGRAM[PID]: "; + // Currently everything is mapped to local facility 4 as debug + char bufpri[SYSLOG_PRILEN] = "<167>"; + + char buftag[SYSLOG_TAGLEN]; + snprintf(buftag, SYSLOG_TAGLEN, "%s[%d]: ", execname, pid); char buf[SYSLOG_MSGLEN]; memset(buf, 0, sizeof(buf)); @@ -97,7 +204,7 @@ void dbg_syslog_output(char* msg) strcat(buf, "\n"); - printf("Sending to syslog: %s\n", buf); +// printf("Sending to syslog: %s\n", buf); int buf_len = strlen(buf); if((sendto(dbg_syslog_sock, buf, buf_len, 0, (struct sockaddr *) &dbg_syslog_sockaddr, sizeof(dbg_syslog_sockaddr))) != buf_len) { -- cgit v1.2.3