s3-lanman: fix api_DosPrintQEnum.
[Samba/ekacnet.git] / source4 / smbd / process_thread.c
blobe17769d34ef4ff3decb09fe92e5fc3dde3c15ebe
1 /*
2 Unix SMB/CIFS implementation.
4 thread model: standard (1 thread per client connection)
6 Copyright (C) Andrew Tridgell 2003-2005
7 Copyright (C) James J Myers 2003 <myersjj@samba.org>
8 Copyright (C) Stefan (metze) Metzmacher 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "version.h"
26 #include <pthread.h>
27 #ifdef HAVE_BACKTRACE
28 #include <execinfo.h>
29 #endif
30 #include "system/wait.h"
31 #include "system/filesys.h"
32 #include "lib/events/events.h"
33 #include "lib/util/dlinklist.h"
34 #include "lib/util/mutex.h"
35 #include "smbd/process_model.h"
37 static pthread_key_t title_key;
39 struct new_conn_state {
40 struct tevent_context *ev;
41 struct socket_context *sock;
42 struct loadparm_context *lp_ctx;
43 void (*new_conn)(struct tevent_context *, struct loadparm_context *lp_ctx, struct socket_context *, uint32_t , void *);
44 void *private_data;
47 static void *thread_connection_fn(void *thread_parm)
49 struct new_conn_state *new_conn = talloc_get_type(thread_parm, struct new_conn_state);
51 new_conn->new_conn(new_conn->ev, new_conn->lp_ctx, new_conn->sock, pthread_self(), new_conn->private_data);
53 /* run this connection from here */
54 event_loop_wait(new_conn->ev);
56 talloc_free(new_conn);
58 return NULL;
62 called when a listening socket becomes readable
64 static void thread_accept_connection(struct tevent_context *ev,
65 struct loadparm_context *lp_ctx,
66 struct socket_context *sock,
67 void (*new_conn)(struct tevent_context *,
68 struct loadparm_context *,
69 struct socket_context *,
70 uint32_t , void *),
71 void *private_data)
73 NTSTATUS status;
74 int rc;
75 pthread_t thread_id;
76 pthread_attr_t thread_attr;
77 struct new_conn_state *state;
78 struct tevent_context *ev2;
80 ev2 = s4_event_context_init(ev);
81 if (ev2 == NULL) return;
83 state = talloc(ev2, struct new_conn_state);
84 if (state == NULL) {
85 talloc_free(ev2);
86 return;
89 state->new_conn = new_conn;
90 state->private_data = private_data;
91 state->lp_ctx = lp_ctx;
92 state->ev = ev2;
94 /* accept an incoming connection. */
95 status = socket_accept(sock, &state->sock);
96 if (!NT_STATUS_IS_OK(status)) {
97 talloc_free(ev2);
98 /* We need to throttle things until the system clears
99 enough resources to handle this new socket. If we
100 don't then we will spin filling the log and causing
101 more problems. We don't panic as this is probably a
102 temporary resource constraint */
103 sleep(1);
104 return;
107 talloc_steal(state, state->sock);
109 pthread_attr_init(&thread_attr);
110 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
111 rc = pthread_create(&thread_id, &thread_attr, thread_connection_fn, state);
112 pthread_attr_destroy(&thread_attr);
113 if (rc == 0) {
114 DEBUG(4,("accept_connection_thread: created thread_id=%lu for fd=%d\n",
115 (unsigned long int)thread_id, socket_get_fd(sock)));
116 } else {
117 DEBUG(0,("accept_connection_thread: thread create failed for fd=%d, rc=%d\n", socket_get_fd(sock), rc));
118 talloc_free(ev2);
123 struct new_task_state {
124 struct tevent_context *ev;
125 struct loadparm_context *lp_ctx;
126 void (*new_task)(struct tevent_context *, struct loadparm_context *,
127 uint32_t , void *);
128 void *private_data;
131 static void *thread_task_fn(void *thread_parm)
133 struct new_task_state *new_task = talloc_get_type(thread_parm, struct new_task_state);
135 new_task->new_task(new_task->ev, new_task->lp_ctx, pthread_self(),
136 new_task->private_data);
138 /* run this connection from here */
139 event_loop_wait(new_task->ev);
141 talloc_free(new_task);
143 return NULL;
147 called when a new task is needed
149 static void thread_new_task(struct tevent_context *ev,
150 struct loadparm_context *lp_ctx,
151 const char *service_name,
152 void (*new_task)(struct tevent_context *,
153 struct loadparm_context *,
154 uint32_t , void *),
155 void *private_data)
157 int rc;
158 pthread_t thread_id;
159 pthread_attr_t thread_attr;
160 struct new_task_state *state;
161 struct tevent_context *ev2;
163 ev2 = s4_event_context_init(ev);
164 if (ev2 == NULL) return;
166 state = talloc(ev2, struct new_task_state);
167 if (state == NULL) {
168 talloc_free(ev2);
169 return;
172 state->new_task = new_task;
173 state->lp_ctx = lp_ctx;
174 state->private_data = private_data;
175 state->ev = ev2;
177 pthread_attr_init(&thread_attr);
178 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
179 rc = pthread_create(&thread_id, &thread_attr, thread_task_fn, state);
180 pthread_attr_destroy(&thread_attr);
181 if (rc == 0) {
182 DEBUG(4,("thread_new_task: created %s thread_id=%lu\n",
183 service_name, (unsigned long int)thread_id));
184 } else {
185 DEBUG(0,("thread_new_task: thread create for %s failed rc=%d\n", service_name, rc));
186 talloc_free(ev2);
190 /* called when a task goes down */
191 static void thread_terminate(struct tevent_context *event_ctx, struct loadparm_context *lp_ctx, const char *reason)
193 DEBUG(10,("thread_terminate: reason[%s]\n",reason));
195 talloc_free(event_ctx);
197 /* terminate this thread */
198 pthread_exit(NULL); /* thread cleanup routine will do actual cleanup */
201 /* called to set a title of a task or connection */
202 static void thread_set_title(struct tevent_context *ev, const char *title)
204 char *old_title;
205 char *new_title;
207 old_title = pthread_getspecific(title_key);
208 talloc_free(old_title);
210 new_title = talloc_strdup(ev, title);
211 pthread_setspecific(title_key, new_title);
215 mutex init function for thread model
217 static int thread_mutex_init(smb_mutex_t *mutex, const char *name)
219 pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
220 mutex->mutex = memdup(&m, sizeof(m));
221 if (! mutex->mutex) {
222 errno = ENOMEM;
223 return -1;
225 return pthread_mutex_init((pthread_mutex_t *)mutex->mutex, NULL);
229 mutex destroy function for thread model
231 static int thread_mutex_destroy(smb_mutex_t *mutex, const char *name)
233 return pthread_mutex_destroy((pthread_mutex_t *)mutex->mutex);
236 static void mutex_start_timer(struct timeval *tp1)
238 gettimeofday(tp1,NULL);
241 static double mutex_end_timer(struct timeval tp1)
243 struct timeval tp2;
244 gettimeofday(&tp2,NULL);
245 return((tp2.tv_sec - tp1.tv_sec) +
246 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
250 mutex lock function for thread model
252 static int thread_mutex_lock(smb_mutex_t *mutexP, const char *name)
254 pthread_mutex_t *mutex = (pthread_mutex_t *)mutexP->mutex;
255 int rc;
256 double t;
257 struct timeval tp1;
258 /* Test below is ONLY for debugging */
259 if ((rc = pthread_mutex_trylock(mutex))) {
260 if (rc == EBUSY) {
261 mutex_start_timer(&tp1);
262 printf("mutex lock: thread %d, lock %s not available\n",
263 (uint32_t)pthread_self(), name);
264 print_suspicious_usage("mutex_lock", name);
265 pthread_mutex_lock(mutex);
266 t = mutex_end_timer(tp1);
267 printf("mutex lock: thread %d, lock %s now available, waited %g seconds\n",
268 (uint32_t)pthread_self(), name, t);
269 return 0;
271 printf("mutex lock: thread %d, lock %s failed rc=%d\n",
272 (uint32_t)pthread_self(), name, rc);
273 SMB_ASSERT(errno == 0); /* force error */
275 return 0;
279 mutex unlock for thread model
281 static int thread_mutex_unlock(smb_mutex_t *mutex, const char *name)
283 return pthread_mutex_unlock((pthread_mutex_t *)mutex->mutex);
286 /*****************************************************************
287 Read/write lock routines.
288 *****************************************************************/
290 rwlock init function for thread model
292 static int thread_rwlock_init(smb_rwlock_t *rwlock, const char *name)
294 pthread_rwlock_t m = PTHREAD_RWLOCK_INITIALIZER;
295 rwlock->rwlock = memdup(&m, sizeof(m));
296 if (! rwlock->rwlock) {
297 errno = ENOMEM;
298 return -1;
300 return pthread_rwlock_init((pthread_rwlock_t *)rwlock->rwlock, NULL);
304 rwlock destroy function for thread model
306 static int thread_rwlock_destroy(smb_rwlock_t *rwlock, const char *name)
308 return pthread_rwlock_destroy((pthread_rwlock_t *)rwlock->rwlock);
312 rwlock lock for read function for thread model
314 static int thread_rwlock_lock_read(smb_rwlock_t *rwlockP, const char *name)
316 pthread_rwlock_t *rwlock = (pthread_rwlock_t *)rwlockP->rwlock;
317 int rc;
318 double t;
319 struct timeval tp1;
320 /* Test below is ONLY for debugging */
321 if ((rc = pthread_rwlock_tryrdlock(rwlock))) {
322 if (rc == EBUSY) {
323 mutex_start_timer(&tp1);
324 printf("rwlock lock_read: thread %d, lock %s not available\n",
325 (uint32_t)pthread_self(), name);
326 print_suspicious_usage("rwlock_lock_read", name);
327 pthread_rwlock_rdlock(rwlock);
328 t = mutex_end_timer(tp1);
329 printf("rwlock lock_read: thread %d, lock %s now available, waited %g seconds\n",
330 (uint32_t)pthread_self(), name, t);
331 return 0;
333 printf("rwlock lock_read: thread %d, lock %s failed rc=%d\n",
334 (uint32_t)pthread_self(), name, rc);
335 SMB_ASSERT(errno == 0); /* force error */
337 return 0;
341 rwlock lock for write function for thread model
343 static int thread_rwlock_lock_write(smb_rwlock_t *rwlockP, const char *name)
345 pthread_rwlock_t *rwlock = (pthread_rwlock_t *)rwlockP->rwlock;
346 int rc;
347 double t;
348 struct timeval tp1;
349 /* Test below is ONLY for debugging */
350 if ((rc = pthread_rwlock_trywrlock(rwlock))) {
351 if (rc == EBUSY) {
352 mutex_start_timer(&tp1);
353 printf("rwlock lock_write: thread %d, lock %s not available\n",
354 (uint32_t)pthread_self(), name);
355 print_suspicious_usage("rwlock_lock_write", name);
356 pthread_rwlock_wrlock(rwlock);
357 t = mutex_end_timer(tp1);
358 printf("rwlock lock_write: thread %d, lock %s now available, waited %g seconds\n",
359 (uint32_t)pthread_self(), name, t);
360 return 0;
362 printf("rwlock lock_write: thread %d, lock %s failed rc=%d\n",
363 (uint32_t)pthread_self(), name, rc);
364 SMB_ASSERT(errno == 0); /* force error */
366 return 0;
371 rwlock unlock for thread model
373 static int thread_rwlock_unlock(smb_rwlock_t *rwlock, const char *name)
375 return pthread_rwlock_unlock((pthread_rwlock_t *)rwlock->rwlock);
378 /*****************************************************************
379 Log suspicious usage (primarily for possible thread-unsafe behavior).
380 *****************************************************************/
381 static void thread_log_suspicious_usage(const char* from, const char* info)
383 DEBUG(1,("log_suspicious_usage: from %s info='%s'\n", from, info));
384 #ifdef HAVE_BACKTRACE
386 void *addresses[10];
387 int num_addresses = backtrace(addresses, 8);
388 char **bt_symbols = backtrace_symbols(addresses, num_addresses);
389 int i;
391 if (bt_symbols) {
392 for (i=0; i<num_addresses; i++) {
393 DEBUG(1,("log_suspicious_usage: %s%s\n", DEBUGTAB(1), bt_symbols[i]));
395 free(bt_symbols);
398 #endif
401 /*****************************************************************
402 Log suspicious usage to stdout (primarily for possible thread-unsafe behavior.
403 Used in mutex code where DEBUG calls would cause recursion.
404 *****************************************************************/
405 static void thread_print_suspicious_usage(const char* from, const char* info)
407 printf("log_suspicious_usage: from %s info='%s'\n", from, info);
408 #ifdef HAVE_BACKTRACE
410 void *addresses[10];
411 int num_addresses = backtrace(addresses, 8);
412 char **bt_symbols = backtrace_symbols(addresses, num_addresses);
413 int i;
415 if (bt_symbols) {
416 for (i=0; i<num_addresses; i++) {
417 printf("log_suspicious_usage: %s%s\n", DEBUGTAB(1), bt_symbols[i]);
419 free(bt_symbols);
422 #endif
425 static uint32_t thread_get_task_id(void)
427 return (uint32_t)pthread_self();
430 static void thread_log_task_id(int fd)
432 char *s= NULL;
434 asprintf(&s, "thread[%u][%s]:\n",
435 (uint32_t)pthread_self(),
436 (const char *)pthread_getspecific(title_key));
437 if (!s) return;
438 write(fd, s, strlen(s));
439 free(s);
442 /****************************************************************************
443 catch serious errors
444 ****************************************************************************/
445 static void thread_sig_fault(int sig)
447 DEBUG(0,("===============================================================\n"));
448 DEBUG(0,("TERMINAL ERROR: Recursive signal %d in thread [%u][%s] (%s)\n",
449 sig,(uint32_t)pthread_self(),
450 (const char *)pthread_getspecific(title_key),
451 SAMBA_VERSION_STRING));
452 DEBUG(0,("===============================================================\n"));
453 exit(1); /* kill the whole server for now */
456 /*******************************************************************
457 setup our recursive fault handlers
458 ********************************************************************/
459 static void thread_fault_setup(void)
461 #ifdef SIGSEGV
462 CatchSignal(SIGSEGV, thread_sig_fault);
463 #endif
464 #ifdef SIGBUS
465 CatchSignal(SIGBUS, thread_sig_fault);
466 #endif
467 #ifdef SIGABRT
468 CatchSignal(SIGABRT, thread_sig_fault);
469 #endif
472 /*******************************************************************
473 report a fault in a thread
474 ********************************************************************/
475 static void thread_fault_handler(int sig)
477 static int counter;
479 /* try to catch recursive faults */
480 thread_fault_setup();
482 counter++; /* count number of faults that have occurred */
484 DEBUG(0,("===============================================================\n"));
485 DEBUG(0,("INTERNAL ERROR: Signal %d in thread [%u] [%s] (%s)\n",
486 sig,(uint32_t)pthread_self(),
487 (const char *)pthread_getspecific(title_key),
488 SAMBA_VERSION_STRING));
489 DEBUG(0,("Please read the file BUGS.txt in the distribution\n"));
490 DEBUG(0,("===============================================================\n"));
491 #ifdef HAVE_BACKTRACE
493 void *addresses[10];
494 int num_addresses = backtrace(addresses, 8);
495 char **bt_symbols = backtrace_symbols(addresses, num_addresses);
496 int i;
498 if (bt_symbols) {
499 for (i=0; i<num_addresses; i++) {
500 DEBUG(1,("fault_report: %s%s\n", DEBUGTAB(1), bt_symbols[i]));
502 free(bt_symbols);
505 #endif
506 pthread_exit(NULL); /* terminate failing thread only */
510 called when the process model is selected
512 static void thread_model_init(struct tevent_context *event_context)
514 struct mutex_ops m_ops;
515 struct debug_ops d_ops;
517 ZERO_STRUCT(m_ops);
518 ZERO_STRUCT(d_ops);
520 pthread_key_create(&title_key, NULL);
521 pthread_setspecific(title_key, talloc_strdup(event_context, ""));
523 /* register mutex/rwlock handlers */
524 m_ops.mutex_init = thread_mutex_init;
525 m_ops.mutex_lock = thread_mutex_lock;
526 m_ops.mutex_unlock = thread_mutex_unlock;
527 m_ops.mutex_destroy = thread_mutex_destroy;
529 m_ops.rwlock_init = thread_rwlock_init;
530 m_ops.rwlock_lock_write = thread_rwlock_lock_write;
531 m_ops.rwlock_lock_read = thread_rwlock_lock_read;
532 m_ops.rwlock_unlock = thread_rwlock_unlock;
533 m_ops.rwlock_destroy = thread_rwlock_destroy;
535 register_mutex_handlers("thread", &m_ops);
537 register_fault_handler("thread", thread_fault_handler);
539 d_ops.log_suspicious_usage = thread_log_suspicious_usage;
540 d_ops.print_suspicious_usage = thread_print_suspicious_usage;
541 d_ops.get_task_id = thread_get_task_id;
542 d_ops.log_task_id = thread_log_task_id;
544 register_debug_handlers("thread", &d_ops);
548 static const struct model_ops thread_ops = {
549 .name = "thread",
550 .model_init = thread_model_init,
551 .accept_connection = thread_accept_connection,
552 .new_task = thread_new_task,
553 .terminate = thread_terminate,
554 .set_title = thread_set_title,
558 initialise the thread process model, registering ourselves with the model subsystem
560 NTSTATUS process_model_thread_init(void)
562 NTSTATUS ret;
564 /* register ourselves with the PROCESS_MODEL subsystem. */
565 ret = register_process_model(&thread_ops);
566 if (!NT_STATUS_IS_OK(ret)) {
567 DEBUG(0,("Failed to register process_model 'thread'!\n"));
568 return ret;
571 return ret;