aboutsummaryrefslogtreecommitdiff
path: root/mysql/mysys/my_syslog.c
blob: e45410b9336d009374cab2f65d5f01aa35c6c09d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
   Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "mysys_priv.h"
#include "my_sys.h"
#include <m_string.h>
#include <stdarg.h>

#ifndef _WIN32
#include <syslog.h>

/*
  Some C libraries offer a variant of this, but we roll our own so we
  won't have to worry about portability.
*/
SYSLOG_FACILITY syslog_facility[] = {
  { LOG_DAEMON, "daemon" }, /* default for mysqld */
  { LOG_USER,   "user"   }, /* default for mysql command-line client */

  { LOG_LOCAL0, "local0" }, { LOG_LOCAL1, "local1" }, { LOG_LOCAL2, "local2" },
  { LOG_LOCAL3, "local3" }, { LOG_LOCAL4, "local4" }, { LOG_LOCAL5, "local5" },
  { LOG_LOCAL6, "local6" }, { LOG_LOCAL7, "local7" },

  /* "just in case" */
  { LOG_AUTH,   "auth" },   { LOG_CRON,   "cron" },   { LOG_KERN,   "kern" },
  { LOG_LPR,    "lpr" },    { LOG_MAIL,   "mail" },   { LOG_NEWS,   "news" },
  { LOG_SYSLOG, "syslog" }, { LOG_UUCP,   "uucp" },

#if defined(LOG_FTP)
  { LOG_FTP,    "ftp" },
#endif
#if defined(LOG_AUTHPRIV)
  { LOG_AUTHPRIV, "authpriv" },
#endif

  { -1, NULL }};
#endif


#ifdef _WIN32
#define MSG_DEFAULT       0xC0000064L
static  HANDLE hEventLog= NULL;                  // global
#endif

/**
  Sends message to the system logger. On Windows, the specified message is
  internally converted to UCS-2 encoding, while on other platforms, no
  conversion takes place and the string is passed to the syslog API as it is.

  @param cs    [in]                Character set info of the message string
  @param level [in]                Log level
  @param msg   [in]                Message to be logged

  @return
     0 Success
    -1 Error
*/
int my_syslog(const CHARSET_INFO *cs MY_ATTRIBUTE((unused)),
              enum loglevel level,
              const char *msg)
{
  int _level;
#ifdef _WIN32
  wchar_t buff[MAX_SYSLOG_MESSAGE_SIZE];
  wchar_t *u16buf= NULL;
  size_t nchars;
  uint dummy_errors;

  DBUG_ENTER("my_syslog");

  _level= (level == INFORMATION_LEVEL) ? EVENTLOG_INFORMATION_TYPE :
    (level == WARNING_LEVEL) ? EVENTLOG_WARNING_TYPE : EVENTLOG_ERROR_TYPE;

  if (hEventLog)
  {
    nchars= my_convert((char *) buff, sizeof(buff) - sizeof(buff[0]),
                       &my_charset_utf16le_bin, msg,
                       MAX_SYSLOG_MESSAGE_SIZE, cs, &dummy_errors);

    // terminate it with NULL
    buff[nchars / sizeof(wchar_t)]= L'\0';
    u16buf= buff;

    if (!ReportEventW(hEventLog, _level, 0, MSG_DEFAULT, NULL, 1, 0,
                      (LPCWSTR*) &u16buf, NULL))
      goto err;
  }

  // Message successfully written to the event log.
  DBUG_RETURN(0);

err:
  // map error appropriately
  my_osmaperr(GetLastError());
  DBUG_RETURN(-1);

#else
  DBUG_ENTER("my_syslog");

  _level= (level == INFORMATION_LEVEL) ? LOG_INFO :
    (level == WARNING_LEVEL) ? LOG_WARNING : LOG_ERR;

  syslog(_level, "%s", msg);
  DBUG_RETURN(0);

#endif                                          /* _WIN32 */
}


#ifdef _WIN32

/**
   Create a key in the Windows registry.
   We'll setup a "MySQL" key in the EventLog branch (RegCreateKey),
   set our executable name (GetModuleFileName) as file-name
   ("EventMessageFile"), then set the message types we expect to
   be logging ("TypesSupported").
   If the key does not exist, sufficient privileges will be required
   to create and configure it.  If the key does exist, opening it
   should be unprivileged; modifying will fail on insufficient
   privileges, but that is non-fatal.

  @param key [in]         Name of the event generator.
                          (Only last part of the key, e.g. "MySQL")

  @return
     0 Success
    -1 Error
*/

const char registry_prefix[]=
         "SYSTEM\\CurrentControlSet\\services\\eventlog\\Application\\";

static int windows_eventlog_create_registry_entry(const char *key)
{
  HKEY    hRegKey= NULL;
  DWORD   dwError= 0;
  TCHAR   szPath[MAX_PATH];
  DWORD   dwTypes;

  size_t  l= sizeof(registry_prefix) + strlen(key) + 1;
  char   *buff;

  int     ret= 0;

  DBUG_ENTER("my_syslog");

  if ((buff= (char *) my_malloc(PSI_NOT_INSTRUMENTED, l, MYF(0))) == NULL)
    DBUG_RETURN(-1);

  my_snprintf(buff, l, "%s%s", registry_prefix, key);

  // Opens the event source registry key; creates it first if required.
  dwError= RegCreateKey(HKEY_LOCAL_MACHINE, buff, &hRegKey);

  my_free(buff);

  if (dwError != ERROR_SUCCESS)
  {
    if (dwError == ERROR_ACCESS_DENIED)
    {
      my_message_stderr(0, "Could not create or access the registry key needed for the MySQL application\n"
                           "to log to the Windows EventLog. Run the application with sufficient\n"
                           "privileges once to create the key, add the key manually, or turn off\n"
                           "logging for that application.", MYF(0));
    }
    DBUG_RETURN(-1);
  }

  /* Name of the PE module that contains the message resource */
  GetModuleFileName(NULL, szPath, MAX_PATH);

  /* Register EventMessageFile (DLL/exec containing event identifiers) */
  dwError = RegSetValueEx(hRegKey, "EventMessageFile", 0, REG_EXPAND_SZ,
                          (PBYTE) szPath, (DWORD) (strlen(szPath) + 1));
  if ((dwError != ERROR_SUCCESS) && (dwError != ERROR_ACCESS_DENIED))
    ret = -1;

  /* Register supported event types */
  dwTypes= (EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
            EVENTLOG_INFORMATION_TYPE);
  dwError= RegSetValueEx(hRegKey, "TypesSupported", 0, REG_DWORD,
                         (LPBYTE) &dwTypes, sizeof dwTypes);
  if ((dwError != ERROR_SUCCESS) && (dwError != ERROR_ACCESS_DENIED))
    ret= -1;

  RegCloseKey(hRegKey);

  DBUG_RETURN(ret);
}
#endif


/**
  Opens/Registers a new handle for system logging.
  Note: It's a thread-unsafe function. It should either
  be invoked from the main thread or some extra thread
  safety measures need to be taken.

  @param name [in]   Name of the event source / syslog ident

  @return
     0 Success
    -1 Error, log not opened
    -2 Error, not updated, using previous values
*/
int my_openlog(const char *name, int option, int facility)
{
#ifndef _WIN32
  int opts= (option & MY_SYSLOG_PIDS) ? LOG_PID : 0;

  DBUG_ENTER("my_openlog");
  openlog(name, opts | LOG_NDELAY, facility);

#else

  HANDLE hEL_new;

  DBUG_ENTER("my_openlog");

  // OOM failsafe.  Not needed for syslog.
  if (name == NULL)
    DBUG_RETURN(-1);

  if ((windows_eventlog_create_registry_entry(name) != 0) ||
      !(hEL_new= RegisterEventSource(NULL, name)))
  {
    // map error appropriately
    my_osmaperr(GetLastError());
    DBUG_RETURN((hEventLog == NULL) ? -1 : -2);
  }
  else
  {
    if (hEventLog != NULL)
      DeregisterEventSource(hEventLog);
    hEventLog= hEL_new;
  }
#endif

  DBUG_RETURN(0);
}


/**
  Closes/de-registers the system logging handle.
  Note: Its a thread-unsafe function. It should
  either be invoked from the main thread or some
  extra thread safety measures need to be taken.

  @return
     0 Success
    -1 Error
*/
int my_closelog(void)
{
  DBUG_ENTER("my_closelog");
#ifndef _WIN32
  closelog();
  DBUG_RETURN(0);
#else
  if ((hEventLog != NULL) && (! DeregisterEventSource(hEventLog)))
    goto err;

  hEventLog= NULL;
  DBUG_RETURN(0);

err:
  hEventLog= NULL;
  // map error appropriately
  my_osmaperr(GetLastError());
  DBUG_RETURN(-1);
#endif
}