|
Packit |
8480eb |
/* ----------------------------------------------------------------------- *
|
|
Packit |
8480eb |
*
|
|
Packit |
8480eb |
* log.c - applcation logging routines.
|
|
Packit |
8480eb |
*
|
|
Packit |
8480eb |
* Copyright 2004 Denis Vlasenko <vda@port.imtp.ilyichevsk.odessa.ua>
|
|
Packit |
8480eb |
* - All Rights Reserved
|
|
Packit |
8480eb |
* Copyright 2005 Ian Kent <raven@themaw.net> - All Rights Reserved
|
|
Packit |
8480eb |
*
|
|
Packit |
8480eb |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
8480eb |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
8480eb |
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
|
|
Packit |
8480eb |
* USA; either version 2 of the License, or (at your option) any later
|
|
Packit |
8480eb |
* version; incorporated herein by reference.
|
|
Packit |
8480eb |
*
|
|
Packit |
8480eb |
* This module has been adapted from patches submitted by:
|
|
Packit |
8480eb |
* Denis Vlasenko <vda@port.imtp.ilyichevsk.odessa.ua>
|
|
Packit |
8480eb |
* Thanks Denis.
|
|
Packit |
8480eb |
*
|
|
Packit |
8480eb |
* ----------------------------------------------------------------------- */
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
#include <stdarg.h>
|
|
Packit |
8480eb |
#include <stdio.h>
|
|
Packit |
8480eb |
#include <stdlib.h>
|
|
Packit |
8480eb |
#include <string.h>
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
#include "automount.h"
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
static unsigned int syslog_open = 0;
|
|
Packit |
8480eb |
static unsigned int logging_to_syslog = 0;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
/* log notification level */
|
|
Packit |
8480eb |
static unsigned int do_verbose = 0; /* Verbose feedback option */
|
|
Packit |
8480eb |
static unsigned int do_debug = 0; /* Full debug output */
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
static char *prepare_attempt_prefix(const char *msg)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
unsigned long *attempt_id;
|
|
Packit |
8480eb |
char buffer[ATTEMPT_ID_SIZE + 1];
|
|
Packit |
8480eb |
char *prefixed_msg = NULL;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
attempt_id = pthread_getspecific(key_thread_attempt_id);
|
|
Packit |
8480eb |
if (attempt_id) {
|
|
Packit |
8480eb |
int len = sizeof(buffer) + 1 + strlen(msg) + 1;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
snprintf(buffer, ATTEMPT_ID_SIZE, "%02lx", *attempt_id);
|
|
Packit |
8480eb |
prefixed_msg = (char *) calloc(len, sizeof(char));
|
|
Packit Service |
9f646e |
if (!prefixed_msg)
|
|
Packit Service |
9f646e |
return NULL;
|
|
Packit |
8480eb |
strcpy(prefixed_msg, buffer);
|
|
Packit |
8480eb |
strcat(prefixed_msg, "|");
|
|
Packit |
8480eb |
strcat(prefixed_msg, msg);
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
return prefixed_msg;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void set_log_norm(void)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
do_verbose = 0;
|
|
Packit |
8480eb |
do_debug = 0;
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void set_log_verbose(void)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
do_verbose = 1;
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void set_log_debug(void)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
do_debug = 1;
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void set_log_norm_ap(struct autofs_point *ap)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
ap->logopt = LOGOPT_ERROR;
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void set_log_verbose_ap(struct autofs_point *ap)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
ap->logopt = LOGOPT_VERBOSE;
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void set_log_debug_ap(struct autofs_point *ap)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
ap->logopt = LOGOPT_DEBUG;
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void log_info(unsigned int logopt, const char *msg, ...)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
unsigned int opt_log = logopt & (LOGOPT_DEBUG | LOGOPT_VERBOSE);
|
|
Packit |
8480eb |
char *prefixed_msg;
|
|
Packit |
8480eb |
va_list ap;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (!do_debug && !do_verbose && !opt_log)
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
prefixed_msg = prepare_attempt_prefix(msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
va_start(ap, msg);
|
|
Packit |
8480eb |
if (logging_to_syslog) {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vsyslog(LOG_INFO, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vsyslog(LOG_INFO, msg, ap);
|
|
Packit |
8480eb |
} else {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vfprintf(stderr, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vfprintf(stderr, msg, ap);
|
|
Packit |
8480eb |
fputc('\n', stderr);
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
va_end(ap);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
free(prefixed_msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void log_notice(unsigned int logopt, const char *msg, ...)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
unsigned int opt_log = logopt & (LOGOPT_DEBUG | LOGOPT_VERBOSE);
|
|
Packit |
8480eb |
char *prefixed_msg;
|
|
Packit |
8480eb |
va_list ap;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (!do_debug && !do_verbose && !opt_log)
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
prefixed_msg = prepare_attempt_prefix(msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
va_start(ap, msg);
|
|
Packit |
8480eb |
if (logging_to_syslog) {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vsyslog(LOG_NOTICE, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vsyslog(LOG_INFO, msg, ap);
|
|
Packit |
8480eb |
} else {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vfprintf(stderr, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vfprintf(stderr, msg, ap);
|
|
Packit |
8480eb |
fputc('\n', stderr);
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
va_end(ap);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
free(prefixed_msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void log_warn(unsigned int logopt, const char *msg, ...)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
unsigned int opt_log = logopt & (LOGOPT_DEBUG | LOGOPT_VERBOSE);
|
|
Packit |
8480eb |
char *prefixed_msg;
|
|
Packit |
8480eb |
va_list ap;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (!do_debug && !do_verbose && !opt_log)
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
prefixed_msg = prepare_attempt_prefix(msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
va_start(ap, msg);
|
|
Packit |
8480eb |
if (logging_to_syslog) {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vsyslog(LOG_WARNING, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vsyslog(LOG_INFO, msg, ap);
|
|
Packit |
8480eb |
} else {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vfprintf(stderr, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vfprintf(stderr, msg, ap);
|
|
Packit |
8480eb |
fputc('\n', stderr);
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
va_end(ap);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
free(prefixed_msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void log_error(unsigned logopt, const char *msg, ...)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
char *prefixed_msg;
|
|
Packit |
8480eb |
va_list ap;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
prefixed_msg = prepare_attempt_prefix(msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
va_start(ap, msg);
|
|
Packit |
8480eb |
if (logging_to_syslog) {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vsyslog(LOG_ERR, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vsyslog(LOG_INFO, msg, ap);
|
|
Packit |
8480eb |
} else {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vfprintf(stderr, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vfprintf(stderr, msg, ap);
|
|
Packit |
8480eb |
fputc('\n', stderr);
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
va_end(ap);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
free(prefixed_msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void log_crit(unsigned logopt, const char *msg, ...)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
char *prefixed_msg;
|
|
Packit |
8480eb |
va_list ap;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
prefixed_msg = prepare_attempt_prefix(msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
va_start(ap, msg);
|
|
Packit |
8480eb |
if (logging_to_syslog) {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vsyslog(LOG_CRIT, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vsyslog(LOG_INFO, msg, ap);
|
|
Packit |
8480eb |
} else {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vfprintf(stderr, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vfprintf(stderr, msg, ap);
|
|
Packit |
8480eb |
fputc('\n', stderr);
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
va_end(ap);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
free(prefixed_msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void log_debug(unsigned int logopt, const char *msg, ...)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
unsigned int opt_log = logopt & LOGOPT_DEBUG;
|
|
Packit |
8480eb |
char *prefixed_msg;
|
|
Packit |
8480eb |
va_list ap;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (!do_debug && !opt_log)
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
prefixed_msg = prepare_attempt_prefix(msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
va_start(ap, msg);
|
|
Packit |
8480eb |
if (logging_to_syslog) {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vsyslog(LOG_WARNING, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vsyslog(LOG_INFO, msg, ap);
|
|
Packit |
8480eb |
} else {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vfprintf(stderr, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vfprintf(stderr, msg, ap);
|
|
Packit |
8480eb |
fputc('\n', stderr);
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
va_end(ap);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
free(prefixed_msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void logmsg(const char *msg, ...)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
char *prefixed_msg;
|
|
Packit |
8480eb |
va_list ap;
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
prefixed_msg = prepare_attempt_prefix(msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
va_start(ap, msg);
|
|
Packit |
8480eb |
if (logging_to_syslog) {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vsyslog(LOG_CRIT, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vsyslog(LOG_INFO, msg, ap);
|
|
Packit |
8480eb |
} else {
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
vfprintf(stderr, prefixed_msg, ap);
|
|
Packit |
8480eb |
else
|
|
Packit |
8480eb |
vfprintf(stderr, msg, ap);
|
|
Packit |
8480eb |
fputc('\n', stderr);
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
va_end(ap);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
if (prefixed_msg)
|
|
Packit |
8480eb |
free(prefixed_msg);
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
void open_log(void)
|
|
Packit |
8480eb |
{
|
|
Packit |
8480eb |
if (!syslog_open) {
|
|
Packit |
8480eb |
syslog_open = 1;
|
|
Packit |
8480eb |
openlog("automount", LOG_PID, LOG_DAEMON);
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit |
8480eb |
logging_to_syslog = 1;
|
|
Packit |
8480eb |
return;
|
|
Packit |
8480eb |
}
|
|
Packit |
8480eb |
|
|
Packit Service |
71cd40 |
void log_to_stderr(void)
|
|
Packit Service |
71cd40 |
{
|
|
Packit Service |
71cd40 |
if (syslog_open) {
|
|
Packit Service |
71cd40 |
syslog_open = 0;
|
|
Packit Service |
71cd40 |
closelog();
|
|
Packit |
50799d |
}
|
|
Packit |
50799d |
|
|
Packit Service |
71cd40 |
logging_to_syslog = 0;
|
|
Packit |
50799d |
|
|
Packit Service |
71cd40 |
return;
|
|
Packit |
50799d |
}
|
|
Packit Service |
888ac8 |
|
|
Packit Service |
888ac8 |
pid_t log_pidinfo(struct autofs_point *ap, pid_t pid, char *label) {
|
|
Packit Service |
888ac8 |
char buf[PATH_MAX + 1] = "";
|
|
Packit Service |
888ac8 |
FILE *statfile;
|
|
Packit Service |
888ac8 |
|
|
Packit Service |
888ac8 |
pid_t tgid, ppid;
|
|
Packit Service |
888ac8 |
int uid, euid, gid, egid;
|
|
Packit Service |
888ac8 |
char comm[64] = "";
|
|
Packit Service |
888ac8 |
|
|
Packit Service |
888ac8 |
sprintf(buf, "/proc/%d/status", pid);
|
|
Packit Service |
888ac8 |
statfile = fopen(buf, "r");
|
|
Packit Service |
888ac8 |
if (statfile == NULL) {
|
|
Packit Service |
888ac8 |
info(ap->logopt, "pidinfo %s: failed to open %s", label, buf);
|
|
Packit Service |
888ac8 |
return -1;
|
|
Packit Service |
888ac8 |
}
|
|
Packit Service |
888ac8 |
|
|
Packit Service |
888ac8 |
while (fgets(buf, sizeof(buf), statfile) != NULL) {
|
|
Packit Service |
888ac8 |
if (strncmp(buf, "Name:", 5) == 0) {
|
|
Packit Service |
888ac8 |
sscanf(buf, "Name:\t%s", (char *) &comm);
|
|
Packit Service |
888ac8 |
} else if (strncmp(buf, "Tgid:", 5) == 0) {
|
|
Packit Service |
888ac8 |
sscanf(buf, "Tgid:\t%d", (int *) &tgid);
|
|
Packit Service |
888ac8 |
} else if (strncmp(buf, "PPid:", 5) == 0) {
|
|
Packit Service |
888ac8 |
sscanf(buf, "PPid:\t%d", (int *) &ppid);
|
|
Packit Service |
888ac8 |
} else if (strncmp(buf, "Uid:", 4) == 0) {
|
|
Packit Service |
888ac8 |
sscanf(buf,
|
|
Packit Service |
888ac8 |
"Uid:\t%d\t%d", (int *) &uid, (int *) &euid);
|
|
Packit Service |
888ac8 |
} else if (strncmp(buf, "Gid:", 4) == 0) {
|
|
Packit Service |
888ac8 |
sscanf(buf,
|
|
Packit Service |
888ac8 |
"Gid:\t%d\t%d", (int *) &gid, (int *) &egid);
|
|
Packit Service |
888ac8 |
}
|
|
Packit Service |
888ac8 |
}
|
|
Packit Service |
888ac8 |
fclose(statfile);
|
|
Packit Service |
888ac8 |
|
|
Packit Service |
888ac8 |
info(ap->logopt,
|
|
Packit Service |
888ac8 |
"pidinfo %s: pid:%d comm:%s tgid:%d uid:%d euid:%d gid:%d egid:%d",
|
|
Packit Service |
888ac8 |
label, pid, comm, tgid, uid, euid, gid, egid);
|
|
Packit Service |
888ac8 |
|
|
Packit Service |
888ac8 |
return ppid;
|
|
Packit Service |
888ac8 |
}
|