]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/mysql.c
Massive SD calling sequence reorganization
[bacula/bacula] / bacula / src / cats / mysql.c
1 /*
2  * Bacula Catalog Database routines specific to MySQL
3  *   These are MySQL specific routines -- hopefully all
4  *    other files are generic.
5  *
6  *    Kern Sibbald, March 2000
7  *
8  *    Version $Id$
9  */
10
11 /*
12    Copyright (C) 2000-2004 Kern Sibbald and John Walker
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of
17    the License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public
25    License along with this program; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27    MA 02111-1307, USA.
28
29  */
30
31
32 /* The following is necessary so that we do not include
33  * the dummy external definition of DB.
34  */
35 #define __SQL_C                       /* indicate that this is sql.c */
36
37 #include "bacula.h"
38 #include "cats.h"
39
40 #ifdef HAVE_MYSQL
41
42 /* -----------------------------------------------------------------------
43  *
44  *   MySQL dependent defines and subroutines
45  *
46  * -----------------------------------------------------------------------
47  */
48
49 /* List of open databases */
50 static BQUEUE db_list = {&db_list, &db_list};
51
52 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
53
54 /*
55  * Initialize database data structure. In principal this should
56  * never have errors, or it is really fatal.
57  */
58 B_DB *
59 db_init_database(JCR *jcr, const char *db_name, const char *db_user, const char *db_password, 
60                  const char *db_address, int db_port, const char *db_socket,
61                  int mult_db_connections)
62 {
63    B_DB *mdb;
64
65    if (!db_user) {              
66       Jmsg(jcr, M_FATAL, 0, _("A user name for MySQL must be supplied.\n"));
67       return NULL;
68    }
69    P(mutex);                          /* lock DB queue */
70    /* Look to see if DB already open */
71    if (!mult_db_connections) {
72       for (mdb=NULL; (mdb=(B_DB *)qnext(&db_list, &mdb->bq)); ) {
73          if (strcmp(mdb->db_name, db_name) == 0) {
74             Dmsg2(100, "DB REopen %d %s\n", mdb->ref_count, db_name);
75             mdb->ref_count++;
76             V(mutex);
77             return mdb;                  /* already open */
78          }
79       }
80    }
81    Dmsg0(100, "db_open first time\n");
82    mdb = (B_DB *) malloc(sizeof(B_DB));
83    memset(mdb, 0, sizeof(B_DB));
84    mdb->db_name = bstrdup(db_name);
85    mdb->db_user = bstrdup(db_user);
86    if (db_password) {
87       mdb->db_password = bstrdup(db_password);
88    }
89    if (db_address) {
90       mdb->db_address = bstrdup(db_address);
91    }
92    if (db_socket) {
93       mdb->db_socket = bstrdup(db_socket);
94    }
95    mdb->db_port = db_port;
96    mdb->have_insert_id = TRUE;
97    mdb->errmsg = get_pool_memory(PM_EMSG); /* get error message buffer */
98    *mdb->errmsg = 0;
99    mdb->cmd = get_pool_memory(PM_EMSG);    /* get command buffer */
100    mdb->cached_path = get_pool_memory(PM_FNAME);
101    mdb->cached_path_id = 0;
102    mdb->ref_count = 1;
103    mdb->fname = get_pool_memory(PM_FNAME);
104    mdb->path = get_pool_memory(PM_FNAME);
105    mdb->esc_name = get_pool_memory(PM_FNAME);
106    qinsert(&db_list, &mdb->bq);            /* put db in list */
107    V(mutex);
108    return mdb;
109 }
110
111 /*
112  * Now actually open the database.  This can generate errors,
113  *  which are returned in the errmsg
114  *
115  * DO NOT close the database or free(mdb) here !!!!
116  */
117 int
118 db_open_database(JCR *jcr, B_DB *mdb)
119 {
120    int errstat;
121
122    P(mutex);
123    if (mdb->connected) {
124       V(mutex);
125       return 1;
126    }
127    mdb->connected = FALSE;
128
129    if ((errstat=rwl_init(&mdb->lock)) != 0) {
130       Mmsg1(&mdb->errmsg, _("Unable to initialize DB lock. ERR=%s\n"), 
131             strerror(errstat));
132       V(mutex);
133       return 0;
134    }
135
136    /* connect to the database */
137 #ifdef HAVE_EMBEDDED_MYSQL
138    mysql_server_init(0, NULL, NULL);
139 #endif
140    mysql_init(&(mdb->mysql));
141    Dmsg0(50, "mysql_init done\n");
142    /* If connection fails, try at 5 sec intervals for 30 seconds. */
143    for (int retry=0; retry < 6; retry++) {
144       mdb->db = mysql_real_connect(
145            &(mdb->mysql),                /* db */
146            mdb->db_address,              /* default = localhost */
147            mdb->db_user,                 /*  login name */
148            mdb->db_password,             /*  password */
149            mdb->db_name,                 /* database name */
150            mdb->db_port,                 /* default port */
151            mdb->db_socket,               /* default = socket */
152            CLIENT_FOUND_ROWS);           /* flags */
153
154       /* If no connect, try once more in case it is a timing problem */
155       if (mdb->db != NULL) {
156          break;
157       }
158       bmicrosleep(5,0);
159    }
160     
161    Dmsg0(50, "mysql_real_connect done\n");
162    Dmsg3(50, "db_user=%s db_name=%s db_password=%s\n", mdb->db_user, mdb->db_name, 
163             mdb->db_password==NULL?"(NULL)":mdb->db_password);
164   
165    if (mdb->db == NULL) {
166       Mmsg2(&mdb->errmsg, _("Unable to connect to MySQL server. \n\
167 Database=%s User=%s\n\
168 It is probably not running or your password is incorrect.\n"), 
169          mdb->db_name, mdb->db_user);
170       V(mutex);
171       return 0;
172    }
173
174    if (!check_tables_version(jcr, mdb)) {
175       V(mutex);
176       return 0;
177    }
178
179 #ifdef HAVE_TREAD_SAFE_MYSQL
180    my_thread_init();
181 #endif
182
183    mdb->connected = TRUE;
184    V(mutex);
185    return 1;
186 }
187
188 void
189 db_close_database(JCR *jcr, B_DB *mdb)
190 {
191    if (!mdb) {
192       return;
193    }
194    P(mutex);
195    mdb->ref_count--;
196 #ifdef HAVE_TREAD_SAFE_MYSQL
197    my_thread_end();
198 #endif
199    if (mdb->ref_count == 0) {
200       qdchain(&mdb->bq);
201       if (mdb->connected && mdb->db) {
202          sql_close(mdb);
203 #ifdef HAVE_EMBEDDED_MYSQL
204          mysql_server_end();
205 #endif
206       }
207       rwl_destroy(&mdb->lock);       
208       free_pool_memory(mdb->errmsg);
209       free_pool_memory(mdb->cmd);
210       free_pool_memory(mdb->cached_path);
211       free_pool_memory(mdb->fname);
212       free_pool_memory(mdb->path);
213       free_pool_memory(mdb->esc_name);
214       if (mdb->db_name) {
215          free(mdb->db_name);
216       }
217       if (mdb->db_user) {
218          free(mdb->db_user);
219       }
220       if (mdb->db_password) {
221          free(mdb->db_password);
222       }
223       if (mdb->db_address) {
224          free(mdb->db_address);
225       }
226       if (mdb->db_socket) {
227          free(mdb->db_socket);
228       }
229       free(mdb);
230    }
231    V(mutex);
232 }
233
234 /*
235  * Return the next unique index (auto-increment) for
236  * the given table.  Return NULL on error.
237  *  
238  * For MySQL, NULL causes the auto-increment value
239  *  to be updated.
240  */
241 int db_next_index(JCR *jcr, B_DB *mdb, char *table, char *index)
242 {
243    strcpy(index, "NULL");
244    return 1;
245 }   
246
247
248 /*
249  * Escape strings so that MySQL is happy
250  *
251  *   NOTE! len is the length of the old string. Your new
252  *         string must be long enough (max 2*old+1) to hold
253  *         the escaped output.
254  */
255 void
256 db_escape_string(char *snew, char *old, int len)
257 {
258    mysql_escape_string(snew, old, len);
259
260 #ifdef DO_IT_MYSELF
261
262 /* Should use mysql_real_escape_string ! */
263 unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length);
264
265    char *n, *o;
266
267    n = snew;
268    o = old;
269    while (len--) {
270       switch (*o) {
271       case 0:
272          *n++= '\\';
273          *n++= '0';
274          o++;
275          break;
276       case '\n':
277          *n++= '\\';
278          *n++= 'n';
279          o++;
280          break;
281       case '\r':
282          *n++= '\\';
283          *n++= 'r';
284          o++;
285          break;
286       case '\\':
287          *n++= '\\';
288          *n++= '\\';
289          o++;
290          break;
291       case '\'':
292          *n++= '\\';
293          *n++= '\'';
294          o++;
295          break;
296       case '"':
297          *n++= '\\';
298          *n++= '"';
299          o++;
300          break;
301       case '\032':
302          *n++= '\\';
303          *n++= 'Z';
304          o++;
305          break;
306       default:
307          *n++= *o++;
308       }
309    }
310    *n = 0;
311 #endif
312 }
313
314 /*
315  * Submit a general SQL command (cmd), and for each row returned,
316  *  the sqlite_handler is called with the ctx.
317  */
318 int db_sql_query(B_DB *mdb, const char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
319 {
320    SQL_ROW row;
321   
322    db_lock(mdb);
323    if (sql_query(mdb, query) != 0) {
324       Mmsg(mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror(mdb));
325       db_unlock(mdb);
326       return 0;
327    }
328    if (result_handler != NULL) {
329       if ((mdb->result = sql_store_result(mdb)) != NULL) {
330          int num_fields = sql_num_fields(mdb);
331
332          while ((row = sql_fetch_row(mdb)) != NULL) {
333             if (result_handler(ctx, num_fields, row))
334                break;
335          }
336
337          sql_free_result(mdb);
338       }
339    }
340    db_unlock(mdb);
341    return 1;
342
343 }
344
345 #endif /* HAVE_MYSQL */