2 * (c) Copyright 1992 by Panagiotis Tsirigotis
3 * (c) Sections Copyright 1998-2001 by Rob Braun
4 * All rights reserved. The file named COPYRIGHT specifies the terms
5 * and conditions for redistribution.
10 #include <sys/types.h>
11 #include <sys/socket.h>
28 #define LOGBUF_SIZE 1024
30 static char ipv6_ret[NI_MAXHOST];
32 const char *xaddrname(const union xsockaddr *inaddr)
35 if( inaddr->sa.sa_family == AF_INET ) len = sizeof(struct sockaddr_in);
36 if( inaddr->sa.sa_family == AF_INET6 ) len = sizeof(struct sockaddr_in6);
37 memset(ipv6_ret, 0, sizeof(ipv6_ret));
38 if( getnameinfo(&inaddr->sa, len, ipv6_ret, sizeof(ipv6_ret), NULL,
40 strncpy(ipv6_ret, "<unknown>", NI_MAXHOST);
44 uint16_t xaddrport(const union xsockaddr *inaddr)
46 if( inaddr->sa.sa_family == AF_INET ) return inaddr->sa_in.sin_port;
47 if( inaddr->sa.sa_family == AF_INET6 ) return inaddr->sa_in6.sin6_port;
51 static int log_common(mask_t *, char *, int, const connection_s *) ;
54 * This function writes log records of the form:
56 * START: service [pid] [from_address]
58 void svc_log_success( struct service *sp, const connection_s *cp, pid_t pid )
60 char buf[ LOGBUF_SIZE ] ;
62 struct service_config *scp = SVC_CONF( sp ) ;
66 if ( ! SVC_LOGS_ON_SUCCESS( sp ) )
69 bufsize = sizeof( buf ) ;
72 cc = strx_nprint( buf, bufsize, "%s: %s", START_ENTRY, SC_ID( scp ) ) ;
76 if ( SC_LOGS_PID( scp ) )
78 cc = strx_nprint( &buf[ len ], bufsize, " pid=%d", pid ) ;
83 cc = log_common( &SC_LOG_ON_SUCCESS( scp ), &buf[len], bufsize, cp ) ;
87 xlog_write( SVC_LOG(sp), buf, len, XLOG_NO_ERRNO ) ;
92 * This function writes log records of the form:
94 * FAIL: service failure-type [from_address]
97 void svc_log_failure( struct service *sp,
98 const connection_s *cp,
99 access_e access_failure )
101 char buf[ LOGBUF_SIZE ] ;
103 struct service_config *scp = SVC_CONF( sp ) ;
107 if ( ! SVC_LOGS_ON_FAILURE( sp ) )
110 bufsize = sizeof( buf ) ;
111 cc = strx_nprint( buf, bufsize, "%s: %s", FAIL_ENTRY, SC_ID( scp ) ) ;
115 cc = strx_nprint( &buf[ len ], bufsize,
116 " %s", ACCESS_EXPLAIN( access_failure ) ) ;
120 cc = log_common( &SC_LOG_ON_FAILURE( scp ), &buf[ len ], bufsize, cp ) ;
124 xlog_write( SVC_LOG(sp), buf, len, XLOG_NO_ERRNO ) ;
129 static int log_common( mask_t *logmask,
132 const connection_s *cp )
136 if ( M_IS_SET( *logmask, LO_HOST ) )
137 len = strx_nprint( buf, bufsize, " from=%s", conn_addrstr( cp ) ) ;
142 void svc_log_exit( struct service *sp, const struct server *serp )
144 char buf[ LOGBUF_SIZE ] ;
148 int exit_status = SERVER_EXITSTATUS( serp ) ;
149 struct service_config *scp = SVC_CONF( sp ) ;
150 const char *func = "log_exit" ;
152 if ( ! SVC_LOGS_ON_EXIT( sp ) )
155 bufsize = sizeof( buf ) ;
158 cc = strx_nprint( buf, bufsize, "%s: %s", EXIT_ENTRY, SC_ID( scp ) ) ;
163 * If the EXIT flag was used, log the exit status or the signal that
164 * killed the process. We assume that these are the only reasons
165 * for process termination.
167 if ( SC_LOGS_EXITS( scp ) )
172 if ( PROC_EXITED( exit_status ) )
175 num = PROC_EXITSTATUS( exit_status ) ;
177 else if ( PROC_SIGNALED( exit_status ) )
180 num = PROC_TERMSIG( exit_status ) ;
184 msg( LOG_ERR, func, "Bad exit status" ) ;
190 cc = strx_nprint( &buf[ len ], bufsize, " %s=%d", s, num ) ;
196 if ( SC_LOGS_PID( scp ) )
198 cc = strx_nprint( &buf[ len ], bufsize, " pid=%d", SERVER_PID( serp ) ) ;
203 if ( SC_LOGS_DURATION( scp ) )
205 time_t current_time ;
207 (void) time( ¤t_time ) ;
208 cc = strx_nprint( &buf[ len ], bufsize, " duration=%ld(sec)",
209 (long)(current_time - SERVER_STARTTIME( serp )) ) ;
213 xlog_write( SVC_LOG(sp), buf, len, XLOG_NO_ERRNO ) ;
219 * Used by other parts of xinetd that want to log something without
220 * going through the proper channels (i.e. log_{success,failure} and log_exit)
223 void svc_logprint( struct service *sp, const char *line_id,
224 const char *fmt, ...)
226 char buf[ LOGBUF_SIZE ] ;
227 int bufsize = sizeof( buf ) ;
232 if ( ! SVC_IS_LOGGING( sp ) )
235 len = strx_nprint( buf, bufsize, "%s: %s ", line_id, SVC_ID( sp ) ) ;
236 va_start( ap, fmt ) ;
237 cc = strx_nprintv( &buf[ len ], bufsize-len, fmt, ap ) ;
239 xlog_write( SVC_LOG(sp), buf, len+cc, XLOG_NO_ERRNO | XLOG_NO_SIZECHECK ) ;