]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - pool_error.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / pool_error.c
1 /* -*-pgsql-c-*- */
2 /*
3  * $Header: /cvsroot/pgpool/pgpool-II/pool_error.c,v 1.3.2.1 2009/08/22 04:19:49 t-ishii Exp $
4  *
5  * pgpool: a language independent connection pool server for PostgreSQL
6  * written by Tatsuo Ishii
7  *
8  * Copyright (c) 2003-2008      PgPool Global Development Group
9  *
10  * Permission to use, copy, modify, and distribute this software and
11  * its documentation for any purpose and without fee is hereby
12  * granted, provided that the above copyright notice appear in all
13  * copies and that both that copyright notice and this permission
14  * notice appear in supporting documentation, and that the name of the
15  * author not be used in advertising or publicity pertaining to
16  * distribution of the software without specific, written prior
17  * permission. The author makes no representations about the
18  * suitability of this software for any purpose.  It is provided "as
19  * is" without express or implied warranty.
20  *
21  * pool_error.c: error and debug messages
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30
31 #include "pool.h"
32
33 #define MAXSTRFTIME 128
34
35 extern int debug;
36
37 static char *nowsec(void);
38
39 void pool_error(const char *fmt,...)
40 {
41         va_list         ap;
42 #ifdef HAVE_ASPRINTF
43         char            *fmt2;
44 #endif
45
46 #ifdef HAVE_SIGPROCMASK
47         sigset_t oldmask;
48 #else
49         int     oldmask;
50 #endif
51
52         POOL_SETMASK2(&BlockSig, &oldmask);
53
54         if (pool_config->print_timestamp)
55 #ifdef HAVE_ASPRINTF
56           asprintf(&fmt2, "%s ERROR: pid %d: %s\n", nowsec(), (int)getpid(), fmt);
57         else
58           asprintf(&fmt2, "ERROR: pid %d: %s\n", (int)getpid(), fmt);
59
60    if (fmt2)
61    {
62      va_start(ap, fmt);
63      vfprintf(stderr, fmt2, ap);
64      va_end(ap);
65      fflush(stderr);
66          free(fmt2);
67    }
68 #else
69           fprintf(stderr, "%s ERROR: pid %d: ", nowsec(), (int)getpid());
70         else
71           fprintf(stderr, "ERROR: pid %d: ", (int)getpid());
72
73         va_start(ap, fmt);
74         vfprintf(stderr, fmt, ap);
75         va_end(ap);
76         fprintf(stderr, "\n");
77 #endif
78
79         POOL_SETMASK(&oldmask);
80 }
81
82 void pool_debug(const char *fmt,...)
83 {
84         va_list         ap;
85 #ifdef HAVE_ASPRINTF
86         char            *fmt2;
87 #endif
88
89 #ifdef HAVE_SIGPROCMASK
90         sigset_t oldmask;
91 #else
92         int     oldmask;
93 #endif
94
95         if (!debug)
96                 return;
97
98         POOL_SETMASK2(&BlockSig, &oldmask);
99
100         if (pool_config->print_timestamp)
101 #ifdef HAVE_ASPRINTF
102           asprintf(&fmt2, "%s DEBUG: pid %d: %s\n", nowsec(), (int)getpid(), fmt);
103         else
104           asprintf(&fmt2, "DEBUG: pid %d: %s\n", (int)getpid(), fmt);
105
106    if (fmt2)
107    {
108      va_start(ap, fmt);
109      vfprintf(stderr, fmt2, ap);
110      va_end(ap);
111      fflush(stderr);
112          free(fmt2);
113    }
114 #else
115           fprintf(stderr, "%s DEBUG: pid %d: ", nowsec(), (int)getpid());
116         else
117           fprintf(stderr, "DEBUG: pid %d: ", (int)getpid());
118
119         va_start(ap, fmt);
120         vfprintf(stderr, fmt, ap);
121         va_end(ap);
122         fprintf(stderr, "\n");
123 #endif
124
125         POOL_SETMASK(&oldmask);
126 }
127
128 void pool_log(const char *fmt,...)
129 {
130         va_list         ap;
131 #ifdef HAVE_ASPRINTF
132         char            *fmt2;
133 #endif
134
135 #ifdef HAVE_SIGPROCMASK
136         sigset_t oldmask;
137 #else
138         int     oldmask;
139 #endif
140
141         POOL_SETMASK2(&BlockSig, &oldmask);
142
143         if (pool_config->print_timestamp)
144 #ifdef HAVE_ASPRINTF
145           asprintf(&fmt2, "%s LOG:   pid %d: %s\n", nowsec(), (int)getpid(), fmt);
146         else
147           asprintf(&fmt2, "LOG:   pid %d: %s\n", (int)getpid(), fmt);
148
149    if (fmt2)
150    {
151      va_start(ap, fmt);
152      vfprintf(stderr, fmt2, ap);
153      va_end(ap);
154      fflush(stderr);
155          free(fmt2);
156    }
157 #else
158           fprintf(stderr, "%s LOG:   pid %d: ", nowsec(), (int)getpid());
159         else
160           fprintf(stderr, "LOG:   pid %d: ", (int)getpid());
161
162         va_start(ap, fmt);
163         vfprintf(stderr, fmt, ap);
164         va_end(ap);
165         fprintf(stderr, "\n");
166 #endif
167
168         POOL_SETMASK(&oldmask);
169 }
170
171 static char *nowsec(void)
172 {
173         static char strbuf[MAXSTRFTIME];
174         time_t now = time(NULL);
175
176         strftime(strbuf, MAXSTRFTIME, "%Y-%m-%d %H:%M:%S", localtime(&now));
177         return strbuf;
178 }