]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/mysql.c
c71418e44913fabc2865ebf5bc99310a0819f128
[bacula/bacula] / bacula / src / cats / mysql.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Bacula Catalog Database routines specific to MySQL
30  *   These are MySQL specific routines -- hopefully all
31  *    other files are generic.
32  *
33  *    Kern Sibbald, March 2000
34  *
35  *    Version $Id$
36  */
37
38
39 /* The following is necessary so that we do not include
40  * the dummy external definition of DB.
41  */
42 #define __SQL_C                       /* indicate that this is sql.c */
43
44 #include "bacula.h"
45 #include "cats.h"
46
47 #ifdef HAVE_MYSQL
48
49 /* -----------------------------------------------------------------------
50  *
51  *   MySQL dependent defines and subroutines
52  *
53  * -----------------------------------------------------------------------
54  */
55
56 /* List of open databases */
57 static dlist *db_list = NULL;
58
59 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
60
61 /*
62  * Retrieve database type
63  */
64 const char *
65 db_get_type(void)
66 {
67    return "MySQL";
68 }
69
70 /*
71  * Initialize database data structure. In principal this should
72  * never have errors, or it is really fatal.
73  */
74 B_DB *
75 db_init_database(JCR *jcr, const char *db_name, const char *db_user, const char *db_password,
76                  const char *db_address, int db_port, const char *db_socket,
77                  int mult_db_connections)
78 {
79    B_DB *mdb = NULL;
80
81    if (!db_user) {
82       Jmsg(jcr, M_FATAL, 0, _("A user name for MySQL must be supplied.\n"));
83       return NULL;
84    }
85    P(mutex);                          /* lock DB queue */
86    if (db_list == NULL) {
87       db_list = New(dlist(mdb, &mdb->link));
88    }
89    /* Look to see if DB already open */
90    if (!mult_db_connections) {
91       foreach_dlist(mdb, db_list) {
92          if (bstrcmp(mdb->db_name, db_name) &&
93              bstrcmp(mdb->db_address, db_address) &&
94              mdb->db_port == db_port) {
95             Dmsg2(100, "DB REopen %d %s\n", mdb->ref_count, db_name);
96             mdb->ref_count++;
97             V(mutex);
98             Dmsg3(100, "initdb ref=%d connected=%d db=%p\n", mdb->ref_count,
99                   mdb->connected, mdb->db);
100             return mdb;                  /* already open */
101          }
102       }
103    }
104    db_check_backend_thread_safe();
105    Dmsg0(100, "db_open first time\n");
106    mdb = (B_DB *)malloc(sizeof(B_DB));
107    memset(mdb, 0, sizeof(B_DB));
108    mdb->db_name = bstrdup(db_name);
109    mdb->db_user = bstrdup(db_user);
110    if (db_password) {
111       mdb->db_password = bstrdup(db_password);
112    }
113    if (db_address) {
114       mdb->db_address = bstrdup(db_address);
115    }
116    if (db_socket) {
117       mdb->db_socket = bstrdup(db_socket);
118    }
119    mdb->db_port = db_port;
120    mdb->have_insert_id = true;
121    mdb->errmsg = get_pool_memory(PM_EMSG); /* get error message buffer */
122    *mdb->errmsg = 0;
123    mdb->cmd = get_pool_memory(PM_EMSG);    /* get command buffer */
124    mdb->cached_path = get_pool_memory(PM_FNAME);
125    mdb->cached_path_id = 0;
126    mdb->ref_count = 1;
127    mdb->fname = get_pool_memory(PM_FNAME);
128    mdb->path = get_pool_memory(PM_FNAME);
129    mdb->esc_name = get_pool_memory(PM_FNAME);
130    mdb->esc_path = get_pool_memory(PM_FNAME);
131    mdb->allow_transactions = mult_db_connections;
132    db_list->append(mdb);                   /* put db in list */
133    Dmsg3(100, "initdb ref=%d connected=%d db=%p\n", mdb->ref_count,
134          mdb->connected, mdb->db);
135    V(mutex);
136    return mdb;
137 }
138
139 /*
140  * Now actually open the database.  This can generate errors,
141  *  which are returned in the errmsg
142  *
143  * DO NOT close the database or free(mdb) here !!!!
144  */
145 int
146 db_open_database(JCR *jcr, B_DB *mdb)
147 {
148    int errstat;
149
150    P(mutex);
151    if (mdb->connected) {
152       V(mutex);
153       return 1;
154    }
155
156    if ((errstat=rwl_init(&mdb->lock)) != 0) {
157       berrno be;
158       Mmsg1(&mdb->errmsg, _("Unable to initialize DB lock. ERR=%s\n"),
159             be.bstrerror(errstat));
160       V(mutex);
161       return 0;
162    }
163
164    /* connect to the database */
165 #ifdef xHAVE_EMBEDDED_MYSQL
166 // mysql_server_init(0, NULL, NULL);
167 #endif
168    mysql_init(&mdb->mysql);
169
170    Dmsg0(50, "mysql_init done\n");
171    /* If connection fails, try at 5 sec intervals for 30 seconds. */
172    for (int retry=0; retry < 6; retry++) {
173       mdb->db = mysql_real_connect(
174            &(mdb->mysql),                /* db */
175            mdb->db_address,              /* default = localhost */
176            mdb->db_user,                 /*  login name */
177            mdb->db_password,             /*  password */
178            mdb->db_name,                 /* database name */
179            mdb->db_port,                 /* default port */
180            mdb->db_socket,               /* default = socket */
181            CLIENT_FOUND_ROWS);           /* flags */
182
183       /* If no connect, try once more in case it is a timing problem */
184       if (mdb->db != NULL) {
185          break;
186       }
187       bmicrosleep(5,0);
188    }
189
190    mdb->mysql.reconnect = 1;             /* so connection does not timeout */
191    Dmsg0(50, "mysql_real_connect done\n");
192    Dmsg3(50, "db_user=%s db_name=%s db_password=%s\n", mdb->db_user, mdb->db_name,
193             mdb->db_password==NULL?"(NULL)":mdb->db_password);
194
195    if (mdb->db == NULL) {
196       Mmsg2(&mdb->errmsg, _("Unable to connect to MySQL server.\n"
197 "Database=%s User=%s\n"
198 "MySQL connect failed either server not running or your authorization is incorrect.\n"),
199          mdb->db_name, mdb->db_user);
200 #if MYSQL_VERSION_ID >= 40101
201       Dmsg3(50, "Error %u (%s): %s\n",
202             mysql_errno(&(mdb->mysql)), mysql_sqlstate(&(mdb->mysql)),
203             mysql_error(&(mdb->mysql)));
204 #else
205       Dmsg2(50, "Error %u: %s\n",
206             mysql_errno(&(mdb->mysql)), mysql_error(&(mdb->mysql)));
207 #endif
208       V(mutex);
209       return 0;
210    }
211
212    mdb->connected = true;
213    if (!check_tables_version(jcr, mdb)) {
214       V(mutex);
215       return 0;
216    }
217
218    Dmsg3(100, "opendb ref=%d connected=%d db=%p\n", mdb->ref_count,
219          mdb->connected, mdb->db);
220
221    /* Set connection timeout to 8 days specialy for batch mode */
222    sql_query(mdb, "SET wait_timeout=691200");
223    sql_query(mdb, "SET interactive_timeout=691200");
224
225    V(mutex);
226    return 1;
227 }
228
229 void
230 db_close_database(JCR *jcr, B_DB *mdb)
231 {
232    if (!mdb) {
233       return;
234    }
235    db_end_transaction(jcr, mdb);
236    P(mutex);
237    sql_free_result(mdb);
238    mdb->ref_count--;
239    Dmsg3(100, "closedb ref=%d connected=%d db=%p\n", mdb->ref_count,
240          mdb->connected, mdb->db);
241    if (mdb->ref_count == 0) {
242       db_list->remove(mdb);
243       if (mdb->connected) {
244          Dmsg1(100, "close db=%p\n", mdb->db);
245          mysql_close(&mdb->mysql);
246
247 #ifdef xHAVE_EMBEDDED_MYSQL
248 //       mysql_server_end();
249 #endif
250       }
251       rwl_destroy(&mdb->lock);
252       free_pool_memory(mdb->errmsg);
253       free_pool_memory(mdb->cmd);
254       free_pool_memory(mdb->cached_path);
255       free_pool_memory(mdb->fname);
256       free_pool_memory(mdb->path);
257       free_pool_memory(mdb->esc_name);
258       free_pool_memory(mdb->esc_path);
259       if (mdb->db_name) {
260          free(mdb->db_name);
261       }
262       if (mdb->db_user) {
263          free(mdb->db_user);
264       }
265       if (mdb->db_password) {
266          free(mdb->db_password);
267       }
268       if (mdb->db_address) {
269          free(mdb->db_address);
270       }
271       if (mdb->db_socket) {
272          free(mdb->db_socket);
273       }
274       free(mdb);
275    }
276    V(mutex);
277 }
278
279 void db_check_backend_thread_safe()
280 {
281 #ifdef HAVE_BATCH_FILE_INSERT
282    if (!mysql_thread_safe()) {
283       Emsg0(M_ABORT, 0, _("MySQL client library must be thread-safe "
284                           "when using BatchMode.\n"));
285    }
286 #endif
287 }
288
289 /*
290  * This call is needed because the message channel thread
291  *  opens a database on behalf of a jcr that was created in
292  *  a different thread. MySQL then allocates thread specific
293  *  data, which is NOT freed when the original jcr thread
294  *  closes the database.  Thus the msgchan must call here
295  *  to cleanup any thread specific data that it created.
296  */
297 void db_thread_cleanup()
298
299 #ifndef HAVE_WIN32
300    my_thread_end();
301 #endif
302 }
303
304 /*
305  * Return the next unique index (auto-increment) for
306  * the given table.  Return NULL on error.
307  *
308  * For MySQL, NULL causes the auto-increment value
309  *  to be updated.
310  */
311 int db_next_index(JCR *jcr, B_DB *mdb, char *table, char *index)
312 {
313    strcpy(index, "NULL");
314    return 1;
315 }
316
317
318 /*
319  * Escape strings so that MySQL is happy
320  *
321  *   NOTE! len is the length of the old string. Your new
322  *         string must be long enough (max 2*old+1) to hold
323  *         the escaped output.
324  */
325 void
326 db_escape_string(JCR *jcr, B_DB *mdb, char *snew, char *old, int len)
327 {
328    mysql_real_escape_string(mdb->db, snew, old, len);
329 }
330
331 /*
332  * Submit a general SQL command (cmd), and for each row returned,
333  *  the sqlite_handler is called with the ctx.
334  */
335 bool db_sql_query(B_DB *mdb, const char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
336 {
337    SQL_ROW row;
338    bool send = true;
339
340    db_lock(mdb);
341    if (sql_query(mdb, query) != 0) {
342       Mmsg(mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror(mdb));
343       db_unlock(mdb);
344       return false;
345    }
346    if (result_handler != NULL) {
347       if ((mdb->result = sql_use_result(mdb)) != NULL) {
348          int num_fields = sql_num_fields(mdb);
349
350          /* We *must* fetch all rows */
351          while ((row = sql_fetch_row(mdb)) != NULL) {
352             if (send) {
353                /* the result handler returns 1 when it has
354                 *  seen all the data it wants.  However, we
355                 *  loop to the end of the data.
356                 */
357                if (result_handler(ctx, num_fields, row)) {
358                   send = false;
359                }
360             }
361          }
362
363          sql_free_result(mdb);
364       }
365    }
366    db_unlock(mdb);
367    return true;
368
369 }
370
371 void my_mysql_free_result(B_DB *mdb)
372 {
373    db_lock(mdb);
374    if (mdb->result) {
375       mysql_free_result(mdb->result);
376       mdb->result = NULL;
377    }
378    db_unlock(mdb);
379 }
380
381 #ifdef HAVE_BATCH_FILE_INSERT
382 const char *my_mysql_batch_lock_path_query = 
383    "LOCK TABLES Path write, batch write, Path as p write";
384
385
386 const char *my_mysql_batch_lock_filename_query = 
387    "LOCK TABLES Filename write, batch write, Filename as f write";
388
389 const char *my_mysql_batch_unlock_tables_query = "UNLOCK TABLES";
390
391 const char *my_mysql_batch_fill_path_query = 
392    "INSERT INTO Path (Path) "
393     "SELECT a.Path FROM " 
394      "(SELECT DISTINCT Path FROM batch) AS a WHERE NOT EXISTS "
395      "(SELECT Path FROM Path AS p WHERE p.Path = a.Path)";     
396
397 const char *my_mysql_batch_fill_filename_query = 
398    "INSERT INTO Filename (Name) "
399     "SELECT a.Name FROM " 
400      "(SELECT DISTINCT Name FROM batch) AS a WHERE NOT EXISTS "
401      "(SELECT Name FROM Filename AS f WHERE f.Name = a.Name)";
402 #endif /* HAVE_BATCH_FILE_INSERT */
403
404 #endif /* HAVE_MYSQL */