2 Unix SMB/CIFS implementation.
3 thread model: standard (1 thread per client connection)
4 Copyright (C) Andrew Tridgell 2003
5 Copyright (C) James J Myers 2003 <myersjj@samba.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 static void *connection_thread(void *thread_parm)
30 struct event_context *ev = thread_parm;
35 pthread_cleanup_pop(1); /* will invoke terminate_mt_connection() */
40 static int get_id(struct request_context *req)
42 return (int)pthread_self();
46 called when a listening socket becomes readable
48 static void accept_connection(struct event_context *ev, struct fd_event *fde,
49 time_t t, uint16_t flags)
53 socklen_t in_addrlen = sizeof(addr);
55 pthread_attr_t thread_attr;
56 struct model_ops *model_ops = fde->private;
58 /* accept an incoming connection */
59 accepted_fd = accept(fde->fd,&addr,&in_addrlen);
61 if (accepted_fd == -1) {
62 DEBUG(0,("accept_connection_thread: accept: %s\n",
67 /* create new detached thread for this connection. The new
68 thread gets a new event_context with a single fd_event for
69 receiving from the new socket. We set that thread running
70 with the main event loop, then return. When we return the
71 main event_context is continued.
73 ev = event_context_init();
74 MUTEX_LOCK_BY_ID(MUTEX_SMBD);
75 init_smbsession(ev, model_ops, accepted_fd, smbd_read_handler);
76 MUTEX_UNLOCK_BY_ID(MUTEX_SMBD);
78 pthread_attr_init(&thread_attr);
79 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
80 rc = pthread_create(&thread_id, &thread_attr, &connection_thread, ev);
81 pthread_attr_destroy(&thread_attr);
83 DEBUG(4,("accept_connection_thread: created thread_id=%lu for fd=%d\n",
84 (unsigned long int)thread_id, accepted_fd));
86 DEBUG(0,("accept_connection_thread: thread create failed for fd=%d, rc=%d\n", accepted_fd, rc));
92 called when a rpc listening socket becomes readable
94 static void accept_rpc_connection(struct event_context *ev, struct fd_event *fde, time_t t, uint16_t flags)
98 socklen_t in_addrlen = sizeof(addr);
100 pthread_attr_t thread_attr;
102 /* accept an incoming connection */
103 accepted_fd = accept(fde->fd,&addr,&in_addrlen);
105 if (accepted_fd == -1) {
106 DEBUG(0,("accept_connection_thread: accept: %s\n",
111 ev = event_context_init();
112 MUTEX_LOCK_BY_ID(MUTEX_SMBD);
113 init_rpc_session(ev, fde->private, accepted_fd);
114 MUTEX_UNLOCK_BY_ID(MUTEX_SMBD);
116 pthread_attr_init(&thread_attr);
117 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
118 rc = pthread_create(&thread_id, &thread_attr, &connection_thread, ev);
119 pthread_attr_destroy(&thread_attr);
121 DEBUG(4,("accept_connection_thread: created thread_id=%lu for fd=%d\n",
122 (unsigned long int)thread_id, accepted_fd));
124 DEBUG(0,("accept_connection_thread: thread create failed for fd=%d, rc=%d\n", accepted_fd, rc));
128 /* called when a SMB connection goes down */
129 static void terminate_connection(struct server_context *server, const char *reason)
131 server_terminate(server);
133 /* terminate this thread */
134 pthread_exit(NULL); /* thread cleanup routine will do actual cleanup */
137 /* called when a rpc connection goes down */
138 static void terminate_rpc_connection(void *r, const char *reason)
140 rpc_server_terminate(r);
142 /* terminate this thread */
143 pthread_exit(NULL); /* thread cleanup routine will do actual cleanup */
147 mutex init function for thread model
149 static int thread_mutex_init(smb_mutex_t *mutex, const char *name)
151 pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
152 mutex->mutex = memdup(&m, sizeof(m));
153 if (! mutex->mutex) {
157 return pthread_mutex_init((pthread_mutex_t *)mutex->mutex, NULL);
161 mutex destroy function for thread model
163 static int thread_mutex_destroy(smb_mutex_t *mutex, const char *name)
165 return pthread_mutex_destroy((pthread_mutex_t *)mutex->mutex);
168 static void mutex_start_timer(struct timeval *tp1)
170 gettimeofday(tp1,NULL);
173 static double mutex_end_timer(struct timeval tp1)
176 gettimeofday(&tp2,NULL);
177 return((tp2.tv_sec - tp1.tv_sec) +
178 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
182 mutex lock function for thread model
184 static int thread_mutex_lock(smb_mutex_t *mutexP, const char *name)
186 pthread_mutex_t *mutex = (pthread_mutex_t *)mutexP->mutex;
190 /* Test below is ONLY for debugging */
191 if ((rc = pthread_mutex_trylock(mutex))) {
193 mutex_start_timer(&tp1);
194 printf("mutex lock: thread %d, lock %s not available\n",
195 (uint32_t)pthread_self(), name);
196 print_suspicious_usage("mutex_lock", name);
197 pthread_mutex_lock(mutex);
198 t = mutex_end_timer(tp1);
199 printf("mutex lock: thread %d, lock %s now available, waited %g seconds\n",
200 (uint32_t)pthread_self(), name, t);
203 printf("mutex lock: thread %d, lock %s failed rc=%d\n",
204 (uint32_t)pthread_self(), name, rc);
205 SMB_ASSERT(errno == 0); /* force error */
211 mutex unlock for thread model
213 static int thread_mutex_unlock(smb_mutex_t *mutex, const char *name)
215 return pthread_mutex_unlock((pthread_mutex_t *)mutex->mutex);
218 /*****************************************************************
219 Read/write lock routines.
220 *****************************************************************/
222 rwlock init function for thread model
224 static int thread_rwlock_init(smb_rwlock_t *rwlock, const char *name)
226 pthread_rwlock_t m = PTHREAD_RWLOCK_INITIALIZER;
227 rwlock->rwlock = memdup(&m, sizeof(m));
228 if (! rwlock->rwlock) {
232 return pthread_rwlock_init((pthread_rwlock_t *)rwlock->rwlock, NULL);
236 rwlock destroy function for thread model
238 static int thread_rwlock_destroy(smb_rwlock_t *rwlock, const char *name)
240 return pthread_rwlock_destroy((pthread_rwlock_t *)rwlock->rwlock);
244 rwlock lock for read function for thread model
246 static int thread_rwlock_lock_read(smb_rwlock_t *rwlockP, const char *name)
248 pthread_rwlock_t *rwlock = (pthread_rwlock_t *)rwlockP->rwlock;
252 /* Test below is ONLY for debugging */
253 if ((rc = pthread_rwlock_tryrdlock(rwlock))) {
255 mutex_start_timer(&tp1);
256 printf("rwlock lock_read: thread %d, lock %s not available\n",
257 (uint32_t)pthread_self(), name);
258 print_suspicious_usage("rwlock_lock_read", name);
259 pthread_rwlock_rdlock(rwlock);
260 t = mutex_end_timer(tp1);
261 printf("rwlock lock_read: thread %d, lock %s now available, waited %g seconds\n",
262 (uint32_t)pthread_self(), name, t);
265 printf("rwlock lock_read: thread %d, lock %s failed rc=%d\n",
266 (uint32_t)pthread_self(), name, rc);
267 SMB_ASSERT(errno == 0); /* force error */
273 rwlock lock for write function for thread model
275 static int thread_rwlock_lock_write(smb_rwlock_t *rwlockP, const char *name)
277 pthread_rwlock_t *rwlock = (pthread_rwlock_t *)rwlockP->rwlock;
281 /* Test below is ONLY for debugging */
282 if ((rc = pthread_rwlock_trywrlock(rwlock))) {
284 mutex_start_timer(&tp1);
285 printf("rwlock lock_write: thread %d, lock %s not available\n",
286 (uint32_t)pthread_self(), name);
287 print_suspicious_usage("rwlock_lock_write", name);
288 pthread_rwlock_wrlock(rwlock);
289 t = mutex_end_timer(tp1);
290 printf("rwlock lock_write: thread %d, lock %s now available, waited %g seconds\n",
291 (uint32_t)pthread_self(), name, t);
294 printf("rwlock lock_write: thread %d, lock %s failed rc=%d\n",
295 (uint32_t)pthread_self(), name, rc);
296 SMB_ASSERT(errno == 0); /* force error */
303 rwlock unlock for thread model
305 static int thread_rwlock_unlock(smb_rwlock_t *rwlock, const char *name)
307 return pthread_rwlock_unlock((pthread_rwlock_t *)rwlock->rwlock);
310 /*****************************************************************
311 Log suspicious usage (primarily for possible thread-unsafe behavior.
312 *****************************************************************/
313 static void thread_log_suspicious_usage(const char* from, const char* info)
315 DEBUG(1,("log_suspicious_usage: from %s info='%s'\n", from, info));
316 #ifdef HAVE_BACKTRACE
319 int num_addresses = backtrace(addresses, 8);
320 char **bt_symbols = backtrace_symbols(addresses, num_addresses);
324 for (i=0; i<num_addresses; i++) {
325 DEBUG(1,("log_suspicious_usage: %s%s\n", DEBUGTAB(1), bt_symbols[i]));
333 /*****************************************************************
334 Log suspicious usage to stdout (primarily for possible thread-unsafe behavior.
335 Used in mutex code where DEBUG calls would cause recursion.
336 *****************************************************************/
337 static void thread_print_suspicious_usage(const char* from, const char* info)
339 printf("log_suspicious_usage: from %s info='%s'\n", from, info);
340 #ifdef HAVE_BACKTRACE
343 int num_addresses = backtrace(addresses, 8);
344 char **bt_symbols = backtrace_symbols(addresses, num_addresses);
348 for (i=0; i<num_addresses; i++) {
349 printf("log_suspicious_usage: %s%s\n", DEBUGTAB(1), bt_symbols[i]);
357 static uint32_t thread_get_task_id(void)
359 return (uint32_t)pthread_self();
362 static void thread_log_task_id(int fd)
366 asprintf(&s, "thread %u: ", (uint32_t)pthread_self());
367 write(fd, s, strlen(s));
370 /****************************************************************************
372 ****************************************************************************/
373 static void thread_sig_fault(int sig)
375 DEBUG(0,("===============================================================\n"));
376 DEBUG(0,("TERMINAL ERROR: Recursive signal %d in thread %lu (%s)\n",sig,(unsigned long int)pthread_self(),SAMBA_VERSION_STRING));
377 DEBUG(0,("===============================================================\n"));
378 exit(1); /* kill the whole server for now */
381 /*******************************************************************
382 setup our recursive fault handlers
383 ********************************************************************/
384 static void thread_fault_setup(void)
387 CatchSignal(SIGSEGV,SIGNAL_CAST thread_sig_fault);
390 CatchSignal(SIGBUS,SIGNAL_CAST thread_sig_fault);
393 CatchSignal(SIGABRT,SIGNAL_CAST thread_sig_fault);
397 /*******************************************************************
398 report a fault in a thread
399 ********************************************************************/
400 static void thread_fault_handler(int sig)
404 /* try to catch recursive faults */
405 thread_fault_setup();
407 counter++; /* count number of faults that have occurred */
409 DEBUG(0,("===============================================================\n"));
410 DEBUG(0,("INTERNAL ERROR: Signal %d in thread %lu (%s)\n",sig,(unsigned long int)pthread_self(),SAMBA_VERSION_STRING));
411 DEBUG(0,("Please read the file BUGS.txt in the distribution\n"));
412 DEBUG(0,("===============================================================\n"));
413 #ifdef HAVE_BACKTRACE
416 int num_addresses = backtrace(addresses, 8);
417 char **bt_symbols = backtrace_symbols(addresses, num_addresses);
421 for (i=0; i<num_addresses; i++) {
422 DEBUG(1,("fault_report: %s%s\n", DEBUGTAB(1), bt_symbols[i]));
428 pthread_exit(NULL); /* terminate failing thread only */
432 called when the process model is selected
434 static void model_startup(void)
436 struct mutex_ops m_ops;
437 struct debug_ops d_ops;
444 /* register mutex/rwlock handlers */
445 m_ops.mutex_init = thread_mutex_init;
446 m_ops.mutex_lock = thread_mutex_lock;
447 m_ops.mutex_unlock = thread_mutex_unlock;
448 m_ops.mutex_destroy = thread_mutex_destroy;
450 m_ops.rwlock_init = thread_rwlock_init;
451 m_ops.rwlock_lock_write = thread_rwlock_lock_write;
452 m_ops.rwlock_lock_read = thread_rwlock_lock_read;
453 m_ops.rwlock_unlock = thread_rwlock_unlock;
454 m_ops.rwlock_destroy = thread_rwlock_destroy;
456 register_mutex_handlers("thread", &m_ops);
458 register_fault_handler("thread", thread_fault_handler);
460 d_ops.log_suspicious_usage = thread_log_suspicious_usage;
461 d_ops.print_suspicious_usage = thread_print_suspicious_usage;
462 d_ops.get_task_id = thread_get_task_id;
463 d_ops.log_task_id = thread_log_task_id;
465 register_debug_handlers("thread", &d_ops);
468 static void thread_exit_server(struct server_context *smb, const char *reason)
470 DEBUG(1,("thread_exit_server: reason[%s]\n",reason));
474 initialise the thread process model, registering ourselves with the model subsystem
476 NTSTATUS process_model_thread_init(void)
479 struct model_ops ops;
483 /* fill in our name */
486 /* fill in all the operations */
487 ops.model_startup = model_startup;
488 ops.accept_connection = accept_connection;
489 ops.accept_rpc_connection = accept_rpc_connection;
490 ops.terminate_connection = terminate_connection;
491 ops.terminate_rpc_connection = terminate_rpc_connection;
492 ops.exit_server = thread_exit_server;
495 /* register ourselves with the PROCESS_MODEL subsystem. */
496 ret = register_backend("process_model", &ops);
497 if (!NT_STATUS_IS_OK(ret)) {
498 DEBUG(0,("Failed to register process_model 'thread'!\n"));