Changeset 988 for vendor/current/source3/smbd/server.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/source3/smbd/server.c
r860 r988 30 30 #include "libcli/auth/schannel.h" 31 31 #include "secrets.h" 32 #include " memcache.h"32 #include "../lib/util/memcache.h" 33 33 #include "ctdbd_conn.h" 34 #include " printing/printer_list.h"35 #include " rpc_server/rpc_ep_setup.h"36 #include " printing/pcap.h"37 #include " printing.h"34 #include "util_cluster.h" 35 #include "printing/queue_process.h" 36 #include "rpc_server/rpc_service_setup.h" 37 #include "rpc_server/rpc_config.h" 38 38 #include "serverid.h" 39 39 #include "passdb.h" … … 41 41 #include "messages.h" 42 42 #include "smbprofile.h" 43 #include "lib/id_cache.h" 44 #include "lib/param/param.h" 45 #include "lib/background.h" 46 #include "lib/conn_tdb.h" 47 #include "../lib/util/pidfile.h" 48 #include "lib/smbd_shim.h" 49 #include "scavenger.h" 50 #include "locking/leases_db.h" 51 #include "smbd/notifyd/notifyd.h" 52 #include "smbd/smbd_cleanupd.h" 53 #include "lib/util/sys_rw.h" 54 55 #ifdef CLUSTER_SUPPORT 56 #include "ctdb_protocol.h" 57 #endif 58 59 struct smbd_open_socket; 60 struct smbd_child_pid; 61 62 struct smbd_parent_context { 63 bool interactive; 64 65 struct tevent_context *ev_ctx; 66 struct messaging_context *msg_ctx; 67 68 /* the list of listening sockets */ 69 struct smbd_open_socket *sockets; 70 71 /* the list of current child processes */ 72 struct smbd_child_pid *children; 73 size_t num_children; 74 75 struct server_id cleanupd; 76 77 struct tevent_timer *cleanup_te; 78 }; 79 80 struct smbd_open_socket { 81 struct smbd_open_socket *prev, *next; 82 struct smbd_parent_context *parent; 83 int fd; 84 struct tevent_fd *fde; 85 }; 86 87 struct smbd_child_pid { 88 struct smbd_child_pid *prev, *next; 89 pid_t pid; 90 }; 43 91 44 92 extern void start_epmd(struct tevent_context *ev_ctx, 45 93 struct messaging_context *msg_ctx); 46 94 47 extern void start_spoolssd(struct event_context *ev_ctx, 48 struct messaging_context *msg_ctx); 49 50 #ifdef WITH_DFS 51 extern int dcelogin_atmost_once; 52 #endif /* WITH_DFS */ 53 54 static void smbd_set_server_fd(int fd) 55 { 56 struct smbd_server_connection *sconn = smbd_server_conn; 57 char addr[INET6_ADDRSTRLEN]; 58 const char *name; 59 60 sconn->sock = fd; 61 62 /* 63 * Initialize sconn->client_id: If we can't find the client's 64 * name, default to its address. 65 */ 66 67 if (sconn->client_id.name != NULL && 68 sconn->client_id.name != sconn->client_id.addr) { 69 talloc_free(discard_const_p(char, sconn->client_id.name)); 70 sconn->client_id.name = NULL; 71 } 72 73 client_addr(fd, sconn->client_id.addr, sizeof(sconn->client_id.addr)); 74 75 name = client_name(sconn->sock); 76 if (strcmp(name, "UNKNOWN") != 0) { 77 name = talloc_strdup(sconn, name); 78 } else { 79 name = NULL; 80 } 81 sconn->client_id.name = 82 (name != NULL) ? name : sconn->client_id.addr; 83 84 sub_set_socket_ids(sconn->client_id.addr, sconn->client_id.name, 85 client_socket_addr(sconn->sock, addr, 86 sizeof(addr))); 87 } 88 89 struct event_context *smbd_event_context(void) 90 { 91 return server_event_context(); 92 } 95 extern void start_lsasd(struct tevent_context *ev_ctx, 96 struct messaging_context *msg_ctx); 97 98 extern void start_fssd(struct tevent_context *ev_ctx, 99 struct messaging_context *msg_ctx); 100 101 extern void start_mdssd(struct tevent_context *ev_ctx, 102 struct messaging_context *msg_ctx); 93 103 94 104 /******************************************************************* … … 96 106 ********************************************************************/ 97 107 98 static void smb _conf_updated(struct messaging_context *msg,99 void *private_data,100 uint32_t msg_type,101 struct server_id server_id,102 DATA_BLOB *data)108 static void smbd_parent_conf_updated(struct messaging_context *msg, 109 void *private_data, 110 uint32_t msg_type, 111 struct server_id server_id, 112 DATA_BLOB *data) 103 113 { 104 114 struct tevent_context *ev_ctx = 105 115 talloc_get_type_abort(private_data, struct tevent_context); 106 116 107 DEBUG(10,("smb _conf_updated: Got message saying smb.conf was "117 DEBUG(10,("smbd_parent_conf_updated: Got message saying smb.conf was " 108 118 "updated. Reloading.\n")); 109 119 change_to_root_user(); 110 reload_services(msg, smbd_server_conn->sock, False); 111 /* printer reload triggered by background printing process */ 112 } 113 114 /******************************************************************* 115 What to do when printcap is updated. 116 ********************************************************************/ 117 118 static void smb_pcap_updated(struct messaging_context *msg, 119 void *private_data, 120 uint32_t msg_type, 121 struct server_id server_id, 122 DATA_BLOB *data) 123 { 124 struct tevent_context *ev_ctx = 125 talloc_get_type_abort(private_data, struct tevent_context); 126 127 DEBUG(10,("Got message saying pcap was updated. Reloading.\n")); 128 change_to_root_user(); 129 reload_printers(ev_ctx, msg); 130 } 131 132 static void smbd_sig_term_handler(struct tevent_context *ev, 133 struct tevent_signal *se, 134 int signum, 135 int count, 136 void *siginfo, 137 void *private_data) 138 { 139 exit_server_cleanly("termination signal"); 140 } 141 142 static void smbd_setup_sig_term_handler(void) 143 { 144 struct tevent_signal *se; 145 146 se = tevent_add_signal(smbd_event_context(), 147 smbd_event_context(), 148 SIGTERM, 0, 149 smbd_sig_term_handler, 150 NULL); 151 if (!se) { 152 exit_server("failed to setup SIGTERM handler"); 153 } 154 } 155 156 static void smbd_sig_hup_handler(struct tevent_context *ev, 157 struct tevent_signal *se, 158 int signum, 159 int count, 160 void *siginfo, 161 void *private_data) 162 { 163 struct messaging_context *msg_ctx = talloc_get_type_abort( 164 private_data, struct messaging_context); 165 change_to_root_user(); 166 DEBUG(1,("Reloading services after SIGHUP\n")); 167 reload_services(msg_ctx, smbd_server_conn->sock, false); 168 } 169 170 static void smbd_setup_sig_hup_handler(struct tevent_context *ev, 171 struct messaging_context *msg_ctx) 172 { 173 struct tevent_signal *se; 174 175 se = tevent_add_signal(ev, ev, SIGHUP, 0, smbd_sig_hup_handler, 176 msg_ctx); 177 if (!se) { 178 exit_server("failed to setup SIGHUP handler"); 179 } 180 } 181 120 reload_services(NULL, NULL, false); 121 printing_subsystem_update(ev_ctx, msg, false); 122 } 182 123 183 124 /******************************************************************* … … 203 144 { 204 145 if(am_parent) kill(0,SIGTERM); 205 }206 207 /****************************************************************************208 Process a sam sync message - not sure whether to do this here or209 somewhere else.210 ****************************************************************************/211 212 static void msg_sam_sync(struct messaging_context *msg,213 void *private_data,214 uint32_t msg_type,215 struct server_id server_id,216 DATA_BLOB *data)217 {218 DEBUG(10, ("** sam sync message received, ignoring\n"));219 146 } 220 147 … … 237 164 { 238 165 int sig; 166 struct server_id_buf tmp; 239 167 240 168 if (data->length != sizeof(sig)) { 241 169 DEBUG(0, ("Process %s sent bogus signal injection request\n", 242 procid_str_static(&src)));170 server_id_str_buf(src, &tmp))); 243 171 return; 244 172 } … … 252 180 #if HAVE_STRSIGNAL 253 181 DEBUG(0, ("Process %s requested injection of signal %d (%s)\n", 254 procid_str_static(&src), sig, strsignal(sig)));182 server_id_str_buf(src, &tmp), sig, strsignal(sig))); 255 183 #else 256 184 DEBUG(0, ("Process %s requested injection of signal %d\n", 257 procid_str_static(&src), sig));185 server_id_str_buf(src, &tmp), sig)); 258 186 #endif 259 187 260 kill( sys_getpid(), sig);188 kill(getpid(), sig); 261 189 } 262 190 #endif /* DEVELOPER */ 191 192 static NTSTATUS messaging_send_to_children(struct messaging_context *msg_ctx, 193 uint32_t msg_type, DATA_BLOB* data) 194 { 195 NTSTATUS status; 196 struct smbd_parent_context *parent = am_parent; 197 struct smbd_child_pid *child; 198 199 if (parent == NULL) { 200 return NT_STATUS_INTERNAL_ERROR; 201 } 202 203 for (child = parent->children; child != NULL; child = child->next) { 204 status = messaging_send(parent->msg_ctx, 205 pid_to_procid(child->pid), 206 msg_type, data); 207 if (!NT_STATUS_IS_OK(status)) { 208 return status; 209 } 210 } 211 return NT_STATUS_OK; 212 } 213 214 static void smb_parent_send_to_children(struct messaging_context *ctx, 215 void* data, 216 uint32_t msg_type, 217 struct server_id srv_id, 218 DATA_BLOB* msg_data) 219 { 220 messaging_send_to_children(ctx, msg_type, msg_data); 221 } 263 222 264 223 /* … … 274 233 DATA_BLOB *data) 275 234 { 276 struct child_pid *child;277 278 235 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data); 279 236 280 for (child = children; child != NULL; child = child->next) { 281 messaging_send_buf(msg_ctx, pid_to_procid(child->pid), 282 MSG_DEBUG, 283 data->data, 284 strlen((char *) data->data) + 1); 285 } 286 } 287 288 static void add_child_pid(pid_t pid) 289 { 290 struct child_pid *child; 291 292 child = SMB_MALLOC_P(struct child_pid); 237 messaging_send_to_children(msg_ctx, MSG_DEBUG, data); 238 } 239 240 static void smbd_parent_id_cache_kill(struct messaging_context *msg_ctx, 241 void *private_data, 242 uint32_t msg_type, 243 struct server_id server_id, 244 DATA_BLOB* data) 245 { 246 const char *msg = (data && data->data) 247 ? (const char *)data->data : "<NULL>"; 248 struct id_cache_ref id; 249 250 if (!id_cache_ref_parse(msg, &id)) { 251 DEBUG(0, ("Invalid ?ID: %s\n", msg)); 252 return; 253 } 254 255 id_cache_delete_from_cache(&id); 256 257 messaging_send_to_children(msg_ctx, msg_type, data); 258 } 259 260 static void smbd_parent_id_cache_delete(struct messaging_context *ctx, 261 void* data, 262 uint32_t msg_type, 263 struct server_id srv_id, 264 DATA_BLOB* msg_data) 265 { 266 id_cache_delete_message(ctx, data, msg_type, srv_id, msg_data); 267 268 messaging_send_to_children(ctx, msg_type, msg_data); 269 } 270 271 #ifdef CLUSTER_SUPPORT 272 static int smbd_parent_ctdb_reconfigured( 273 uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid, 274 const uint8_t *msg, size_t msglen, void *private_data) 275 { 276 struct messaging_context *msg_ctx = talloc_get_type_abort( 277 private_data, struct messaging_context); 278 279 DEBUG(10, ("Got %s message\n", (dst_srvid == CTDB_SRVID_RECONFIGURE) 280 ? "cluster reconfigure" : "SAMBA_NOTIFY")); 281 282 /* 283 * Someone from the family died, validate our locks 284 */ 285 286 if (am_parent) { 287 messaging_send_buf(msg_ctx, am_parent->cleanupd, 288 MSG_SMB_BRL_VALIDATE, NULL, 0); 289 } 290 291 return 0; 292 } 293 #endif 294 295 static void add_child_pid(struct smbd_parent_context *parent, 296 pid_t pid) 297 { 298 struct smbd_child_pid *child; 299 300 child = talloc_zero(parent, struct smbd_child_pid); 293 301 if (child == NULL) { 294 302 DEBUG(0, ("Could not add child struct -- malloc failed\n")); … … 296 304 } 297 305 child->pid = pid; 298 DLIST_ADD(children, child); 299 num_children += 1; 306 DLIST_ADD(parent->children, child); 307 parent->num_children += 1; 308 } 309 310 static void smb_tell_num_children(struct messaging_context *ctx, void *data, 311 uint32_t msg_type, struct server_id srv_id, 312 DATA_BLOB *msg_data) 313 { 314 uint8_t buf[sizeof(uint32_t)]; 315 316 if (am_parent) { 317 SIVAL(buf, 0, am_parent->num_children); 318 messaging_send_buf(ctx, srv_id, MSG_SMB_NUM_CHILDREN, 319 buf, sizeof(buf)); 320 } 321 } 322 323 static void notifyd_stopped(struct tevent_req *req); 324 325 static struct tevent_req *notifyd_req(struct messaging_context *msg_ctx, 326 struct tevent_context *ev) 327 { 328 struct tevent_req *req; 329 sys_notify_watch_fn sys_notify_watch = NULL; 330 struct sys_notify_context *sys_notify_ctx = NULL; 331 332 if (lp_kernel_change_notify()) { 333 334 #ifdef HAVE_INOTIFY 335 if (lp_parm_bool(-1, "notify", "inotify", true)) { 336 sys_notify_watch = inotify_watch; 337 } 338 #endif 339 340 #ifdef HAVE_FAM 341 if (lp_parm_bool(-1, "notify", "fam", 342 (sys_notify_watch == NULL))) { 343 sys_notify_watch = fam_watch; 344 } 345 #endif 346 } 347 348 if (sys_notify_watch != NULL) { 349 sys_notify_ctx = sys_notify_context_create(msg_ctx, ev); 350 if (sys_notify_ctx == NULL) { 351 return NULL; 352 } 353 } 354 355 req = notifyd_send(msg_ctx, ev, msg_ctx, 356 messaging_ctdbd_connection(), 357 sys_notify_watch, sys_notify_ctx); 358 if (req == NULL) { 359 TALLOC_FREE(sys_notify_ctx); 360 return NULL; 361 } 362 tevent_req_set_callback(req, notifyd_stopped, msg_ctx); 363 364 return req; 365 } 366 367 static void notifyd_stopped(struct tevent_req *req) 368 { 369 int ret; 370 371 ret = notifyd_recv(req); 372 TALLOC_FREE(req); 373 DEBUG(1, ("notifyd stopped: %s\n", strerror(ret))); 374 } 375 376 static bool smbd_notifyd_init(struct messaging_context *msg, bool interactive) 377 { 378 struct tevent_context *ev = messaging_tevent_context(msg); 379 struct tevent_req *req; 380 pid_t pid; 381 NTSTATUS status; 382 383 if (interactive) { 384 req = notifyd_req(msg, ev); 385 return (req != NULL); 386 } 387 388 pid = fork(); 389 if (pid == -1) { 390 DEBUG(1, ("%s: fork failed: %s\n", __func__, 391 strerror(errno))); 392 return false; 393 } 394 395 if (pid != 0) { 396 return true; 397 } 398 399 status = reinit_after_fork(msg, ev, true, "smbd-notifyd"); 400 if (!NT_STATUS_IS_OK(status)) { 401 DEBUG(1, ("%s: reinit_after_fork failed: %s\n", 402 __func__, nt_errstr(status))); 403 exit(1); 404 } 405 406 req = notifyd_req(msg, ev); 407 if (req == NULL) { 408 exit(1); 409 } 410 tevent_req_set_callback(req, notifyd_stopped, msg); 411 return tevent_req_poll(req, ev); 412 } 413 414 static void cleanupd_stopped(struct tevent_req *req); 415 416 static bool cleanupd_init(struct messaging_context *msg, bool interactive, 417 struct server_id *ppid) 418 { 419 struct tevent_context *ev = messaging_tevent_context(msg); 420 struct server_id parent_id = messaging_server_id(msg); 421 struct tevent_req *req; 422 pid_t pid; 423 NTSTATUS status; 424 ssize_t rwret; 425 int ret; 426 bool ok; 427 char c; 428 int up_pipe[2]; 429 430 if (interactive) { 431 req = smbd_cleanupd_send(msg, ev, msg, parent_id.pid); 432 *ppid = messaging_server_id(msg); 433 return (req != NULL); 434 } 435 436 ret = pipe(up_pipe); 437 if (ret == -1) { 438 DBG_WARNING("pipe failed: %s\n", strerror(errno)); 439 return false; 440 } 441 442 pid = fork(); 443 if (pid == -1) { 444 DBG_WARNING("fork failed: %s\n", strerror(errno)); 445 close(up_pipe[0]); 446 close(up_pipe[1]); 447 return false; 448 } 449 450 if (pid != 0) { 451 452 close(up_pipe[1]); 453 rwret = sys_read(up_pipe[0], &c, 1); 454 close(up_pipe[0]); 455 456 if (rwret == -1) { 457 DBG_WARNING("sys_read failed: %s\n", strerror(errno)); 458 return false; 459 } 460 if (rwret == 0) { 461 DBG_WARNING("cleanupd could not start\n"); 462 return false; 463 } 464 if (c != 0) { 465 DBG_WARNING("cleanupd returned %d\n", (int)c); 466 return false; 467 } 468 469 DBG_DEBUG("Started cleanupd pid=%d\n", (int)pid); 470 471 if (am_parent != NULL) { 472 add_child_pid(am_parent, pid); 473 } 474 475 *ppid = pid_to_procid(pid); 476 return true; 477 } 478 479 close(up_pipe[0]); 480 481 status = reinit_after_fork(msg, ev, true, "cleanupd"); 482 if (!NT_STATUS_IS_OK(status)) { 483 DBG_WARNING("reinit_after_fork failed: %s\n", 484 nt_errstr(status)); 485 c = 1; 486 sys_write(up_pipe[1], &c, 1); 487 488 exit(1); 489 } 490 491 req = smbd_cleanupd_send(msg, ev, msg, parent_id.pid); 492 if (req == NULL) { 493 DBG_WARNING("smbd_cleanupd_send failed\n"); 494 c = 2; 495 sys_write(up_pipe[1], &c, 1); 496 497 exit(1); 498 } 499 500 tevent_req_set_callback(req, cleanupd_stopped, msg); 501 502 c = 0; 503 rwret = sys_write(up_pipe[1], &c, 1); 504 close(up_pipe[1]); 505 506 if (rwret == -1) { 507 DBG_WARNING("sys_write failed: %s\n", strerror(errno)); 508 exit(1); 509 } 510 if (rwret != 1) { 511 DBG_WARNING("sys_write could not write result\n"); 512 exit(1); 513 } 514 515 ok = tevent_req_poll(req, ev); 516 if (!ok) { 517 DBG_WARNING("tevent_req_poll returned %s\n", strerror(errno)); 518 } 519 exit(0); 520 } 521 522 static void cleanupd_stopped(struct tevent_req *req) 523 { 524 NTSTATUS status; 525 526 status = smbd_cleanupd_recv(req); 527 DBG_WARNING("cleanupd stopped: %s\n", nt_errstr(status)); 300 528 } 301 529 … … 311 539 */ 312 540 313 static void cleanup_timeout_fn(struct event_context *event_ctx,314 struct t imed_event*te,541 static void cleanup_timeout_fn(struct tevent_context *event_ctx, 542 struct tevent_timer *te, 315 543 struct timeval now, 316 544 void *private_data) 317 545 { 318 struct timed_event **cleanup_te = (struct timed_event **)private_data; 319 320 DEBUG(1,("Cleaning up brl and lock database after unclean shutdown\n")); 321 message_send_all(smbd_messaging_context(), MSG_SMB_UNLOCK, NULL, 0, NULL); 322 messaging_send_buf(smbd_messaging_context(), procid_self(), 323 MSG_SMB_BRL_VALIDATE, NULL, 0); 324 /* mark the cleanup as having been done */ 325 (*cleanup_te) = NULL; 326 } 327 328 static void remove_child_pid(pid_t pid, bool unclean_shutdown) 329 { 330 struct child_pid *child; 331 static struct timed_event *cleanup_te; 332 struct server_id child_id; 333 334 child_id = procid_self(); /* Just initialize pid and potentially vnn */ 335 child_id.pid = pid; 336 337 for (child = children; child != NULL; child = child->next) { 546 struct smbd_parent_context *parent = 547 talloc_get_type_abort(private_data, 548 struct smbd_parent_context); 549 550 parent->cleanup_te = NULL; 551 552 messaging_send_buf(parent->msg_ctx, parent->cleanupd, 553 MSG_SMB_UNLOCK, NULL, 0); 554 } 555 556 static void remove_child_pid(struct smbd_parent_context *parent, 557 pid_t pid, 558 bool unclean_shutdown) 559 { 560 struct smbd_child_pid *child; 561 struct iovec iov[2]; 562 NTSTATUS status; 563 564 for (child = parent->children; child != NULL; child = child->next) { 338 565 if (child->pid == pid) { 339 struct child_pid *tmp = child;340 DLIST_REMOVE( children, child);341 SAFE_FREE(tmp);342 num_children -= 1;566 struct smbd_child_pid *tmp = child; 567 DLIST_REMOVE(parent->children, child); 568 TALLOC_FREE(tmp); 569 parent->num_children -= 1; 343 570 break; 344 571 } … … 350 577 return; 351 578 } 579 580 if (child->pid == procid_to_pid(&parent->cleanupd)) { 581 bool ok; 582 583 DBG_WARNING("Restarting cleanupd\n"); 584 ok = cleanupd_init(parent->msg_ctx, false, &parent->cleanupd); 585 if (!ok) { 586 DBG_ERR("Failed to restart cleanupd\n"); 587 } 588 return; 589 } 590 591 iov[0] = (struct iovec) { .iov_base = (uint8_t *)&pid, 592 .iov_len = sizeof(pid) }; 593 iov[1] = (struct iovec) { .iov_base = (uint8_t *)&unclean_shutdown, 594 .iov_len = sizeof(bool) }; 595 596 status = messaging_send_iov(parent->msg_ctx, parent->cleanupd, 597 MSG_SMB_NOTIFY_CLEANUP, 598 iov, ARRAY_SIZE(iov), NULL, 0); 599 DEBUG(10, ("messaging_send_iov returned %s\n", nt_errstr(status))); 352 600 353 601 if (unclean_shutdown) { … … 358 606 DEBUG(3,(__location__ " Unclean shutdown of pid %u\n", 359 607 (unsigned int)pid)); 360 if ( !cleanup_te) {608 if (parent->cleanup_te == NULL) { 361 609 /* call the cleanup timer, but not too often */ 362 610 int cleanup_time = lp_parm_int(-1, "smbd", "cleanuptime", 20); 363 cleanup_te = event_add_timed(smbd_event_context(), NULL, 611 parent->cleanup_te = tevent_add_timer(parent->ev_ctx, 612 parent, 364 613 timeval_current_ofs(cleanup_time, 0), 365 614 cleanup_timeout_fn, 366 &cleanup_te);615 parent); 367 616 DEBUG(1,("Scheduled cleanup of brl and lock database after unclean shutdown\n")); 368 617 } 369 }370 371 if (!serverid_deregister(child_id)) {372 DEBUG(1, ("Could not remove pid %d from serverid.tdb\n",373 (int)pid));374 618 } 375 619 } … … 379 623 ****************************************************************************/ 380 624 381 static bool allowable_number_of_smbd_processes( void)625 static bool allowable_number_of_smbd_processes(struct smbd_parent_context *parent) 382 626 { 383 627 int max_processes = lp_max_smbd_processes(); … … 386 630 return True; 387 631 388 return num_children < max_processes;632 return parent->num_children < max_processes; 389 633 } 390 634 … … 398 642 pid_t pid; 399 643 int status; 644 struct smbd_parent_context *parent = 645 talloc_get_type_abort(private_data, 646 struct smbd_parent_context); 400 647 401 648 while ((pid = sys_waitpid(-1, &status, WNOHANG)) > 0) { … … 415 662 unclean_shutdown = True; 416 663 } 417 remove_child_pid(p id, unclean_shutdown);418 } 419 } 420 421 static void smbd_setup_sig_chld_handler( void)664 remove_child_pid(parent, pid, unclean_shutdown); 665 } 666 } 667 668 static void smbd_setup_sig_chld_handler(struct smbd_parent_context *parent) 422 669 { 423 670 struct tevent_signal *se; 424 671 425 se = tevent_add_signal( smbd_event_context(),426 smbd_event_context(),672 se = tevent_add_signal(parent->ev_ctx, 673 parent, /* mem_ctx */ 427 674 SIGCHLD, 0, 428 675 smbd_sig_chld_handler, 429 NULL);676 parent); 430 677 if (!se) { 431 678 exit_server("failed to setup SIGCHLD handler"); 432 679 } 433 680 } 434 435 struct smbd_open_socket;436 437 struct smbd_parent_context {438 bool interactive;439 440 /* the list of listening sockets */441 struct smbd_open_socket *sockets;442 };443 444 struct smbd_open_socket {445 struct smbd_open_socket *prev, *next;446 struct smbd_parent_context *parent;447 int fd;448 struct tevent_fd *fde;449 };450 681 451 682 static void smbd_open_socket_close_fn(struct tevent_context *ev, … … 465 696 struct smbd_open_socket *s = talloc_get_type_abort(private_data, 466 697 struct smbd_open_socket); 698 struct messaging_context *msg_ctx = s->parent->msg_ctx; 467 699 struct sockaddr_storage addr; 468 700 socklen_t in_addrlen = sizeof(addr); 469 701 int fd; 470 702 pid_t pid = 0; 471 uint64_t unique_id;472 703 473 704 fd = accept(s->fd, (struct sockaddr *)(void *)&addr,&in_addrlen); 474 smbd_set_server_fd(fd);475 476 705 if (fd == -1 && errno == EINTR) 477 706 return; 478 707 479 708 if (fd == -1) { 480 DEBUG(0,(" open_sockets_smbd:accept: %s\n",709 DEBUG(0,("accept: %s\n", 481 710 strerror(errno))); 482 711 return; … … 484 713 485 714 if (s->parent->interactive) { 486 smbd_process(smbd_server_conn); 715 reinit_after_fork(msg_ctx, ev, true, NULL); 716 smbd_process(ev, msg_ctx, fd, true); 487 717 exit_server_cleanly("end of interactive mode"); 488 718 return; 489 719 } 490 720 491 if (!allowable_number_of_smbd_processes( )) {721 if (!allowable_number_of_smbd_processes(s->parent)) { 492 722 close(fd); 493 smbd_set_server_fd(-1);494 723 return; 495 724 } 496 725 497 /* 498 * Generate a unique id in the parent process so that we use 499 * the global random state in the parent. 500 */ 501 unique_id = serverid_get_random_unique_id(); 502 503 pid = sys_fork(); 726 pid = fork(); 504 727 if (pid == 0) { 505 728 NTSTATUS status = NT_STATUS_OK; 506 507 /* Child code ... */508 am_parent = 0;509 510 set_my_unique_id(unique_id);511 512 /* Stop zombies, the parent explicitly handles513 * them, counting worker smbds. */514 CatchChild();515 516 /* close our standard file517 descriptors */518 close_low_fds(False);519 729 520 730 /* … … 525 735 s = NULL; 526 736 527 status = reinit_after_fork(smbd_messaging_context(), 528 smbd_event_context(), procid_self(), 529 true); 737 /* Stop zombies, the parent explicitly handles 738 * them, counting worker smbds. */ 739 CatchChild(); 740 741 status = smbd_reinit_after_fork(msg_ctx, ev, true, NULL); 530 742 if (!NT_STATUS_IS_OK(status)) { 531 743 if (NT_STATUS_EQUAL(status, … … 548 760 } 549 761 550 smbd_setup_sig_term_handler(); 551 smbd_setup_sig_hup_handler(server_event_context(), 552 server_messaging_context()); 553 554 if (!serverid_register(procid_self(), 555 FLAG_MSG_GENERAL|FLAG_MSG_SMBD 556 |FLAG_MSG_DBWRAP 557 |FLAG_MSG_PRINT_GENERAL)) { 558 exit_server_cleanly("Could not register myself in " 559 "serverid.tdb"); 560 } 561 562 smbd_process(smbd_server_conn); 762 smbd_process(ev, msg_ctx, fd, false); 563 763 exit: 564 764 exit_server_cleanly("end of child"); … … 567 767 568 768 if (pid < 0) { 569 DEBUG(0,("smbd_accept_connection: sys_fork() failed: %s\n",769 DEBUG(0,("smbd_accept_connection: fork() failed: %s\n", 570 770 strerror(errno))); 571 771 } … … 582 782 */ 583 783 584 smbd_set_server_fd(-1);585 586 784 if (pid != 0) { 587 add_child_pid( pid);785 add_child_pid(s->parent, pid); 588 786 } 589 787 … … 606 804 607 805 static bool smbd_open_one_socket(struct smbd_parent_context *parent, 806 struct tevent_context *ev_ctx, 608 807 const struct sockaddr_storage *ifss, 609 808 uint16_t port) … … 623 822 true); 624 823 if (s->fd == -1) { 625 DEBUG(0,("smbd_open_on ce_socket: open_socket_in: "824 DEBUG(0,("smbd_open_one_socket: open_socket_in: " 626 825 "%s\n", strerror(errno))); 627 826 TALLOC_FREE(s); … … 641 840 642 841 if (listen(s->fd, SMBD_LISTEN_BACKLOG) == -1) { 643 DEBUG(0,(" open_sockets_smbd: listen: "842 DEBUG(0,("smbd_open_one_socket: listen: " 644 843 "%s\n", strerror(errno))); 645 844 close(s->fd); … … 648 847 } 649 848 650 s->fde = tevent_add_fd( smbd_event_context(),849 s->fde = tevent_add_fd(ev_ctx, 651 850 s, 652 851 s->fd, TEVENT_FD_READ, … … 654 853 s); 655 854 if (!s->fde) { 656 DEBUG(0,(" open_sockets_smbd: "855 DEBUG(0,("smbd_open_one_socket: " 657 856 "tevent_add_fd: %s\n", 658 857 strerror(errno))); … … 663 862 tevent_fd_set_close_fn(s->fde, smbd_open_socket_close_fn); 664 863 665 DLIST_ADD_END(parent->sockets, s , struct smbd_open_socket *);864 DLIST_ADD_END(parent->sockets, s); 666 865 667 866 return true; … … 673 872 674 873 static bool open_sockets_smbd(struct smbd_parent_context *parent, 874 struct tevent_context *ev_ctx, 675 875 struct messaging_context *msg_ctx, 676 876 const char *smb_ports) 677 877 { 678 878 int num_interfaces = iface_count(); 679 int i; 680 char *ports; 681 char *tok; 682 const char *ptr; 879 int i,j; 880 const char **ports; 683 881 unsigned dns_port = 0; 684 882 … … 688 886 689 887 /* Stop zombies */ 690 smbd_setup_sig_chld_handler(); 888 smbd_setup_sig_chld_handler(parent); 889 890 ports = lp_smb_ports(); 691 891 692 892 /* use a reasonable default set of ports - listing on 445 and 139 */ 693 if (!smb_ports) { 694 ports = lp_smb_ports(); 695 if (!ports || !*ports) { 696 ports = talloc_strdup(talloc_tos(), SMB_PORTS); 697 } else { 698 ports = talloc_strdup(talloc_tos(), ports); 699 } 700 } else { 701 ports = talloc_strdup(talloc_tos(), smb_ports); 702 } 703 704 for (ptr = ports; 705 next_token_talloc(talloc_tos(),&ptr, &tok, " \t,");) { 706 unsigned port = atoi(tok); 893 if (smb_ports) { 894 char **l; 895 l = str_list_make_v3(talloc_tos(), smb_ports, NULL); 896 ports = discard_const_p(const char *, l); 897 } 898 899 for (j = 0; ports && ports[j]; j++) { 900 unsigned port = atoi(ports[j]); 707 901 708 902 if (port == 0 || port > 0xffff) { … … 723 917 const struct sockaddr_storage *ifss = 724 918 iface_n_sockaddr_storage(i); 725 726 919 if (ifss == NULL) { 727 920 DEBUG(0,("open_sockets_smbd: " … … 731 924 } 732 925 733 for (ptr=ports; 734 next_token_talloc(talloc_tos(),&ptr, &tok, " \t,");) { 735 unsigned port = atoi(tok); 926 for (j = 0; ports && ports[j]; j++) { 927 unsigned port = atoi(ports[j]); 736 928 737 929 /* Keep the first port for mDNS service … … 742 934 } 743 935 744 if (!smbd_open_one_socket(parent, ifss, port)) { 936 if (!smbd_open_one_socket(parent, 937 ev_ctx, 938 ifss, 939 port)) { 745 940 return false; 746 941 } … … 751 946 from anywhere. */ 752 947 753 const char *sock_addr = lp_socket_address();948 const char *sock_addr; 754 949 char *sock_tok; 755 950 const char *sock_ptr; 756 951 757 if (strequal(sock_addr, "0.0.0.0") ||758 strequal(sock_addr, "::")) {759 952 #if HAVE_IPV6 760 953 sock_addr = "::,0.0.0.0"; 761 954 #else 762 955 sock_addr = "0.0.0.0"; 763 956 #endif 764 }765 957 766 958 for (sock_ptr=sock_addr; 767 959 next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,"); ) { 768 for ( ptr=ports; next_token_talloc(talloc_tos(), &ptr, &tok, " \t,");) {960 for (j = 0; ports && ports[j]; j++) { 769 961 struct sockaddr_storage ss; 770 unsigned port = atoi( tok);962 unsigned port = atoi(ports[j]); 771 963 772 964 /* Keep the first port for mDNS service … … 783 975 } 784 976 785 if (!smbd_open_one_socket(parent, &ss, port)) { 786 return false; 787 } 977 /* 978 * If we fail to open any sockets 979 * in this loop the parent-sockets == NULL 980 * case below will prevent us from starting. 981 */ 982 983 (void)smbd_open_one_socket(parent, 984 ev_ctx, 985 &ss, 986 port); 788 987 } 789 988 } … … 802 1001 includes checking to see that smbd is listening. */ 803 1002 804 if (!serverid_register( procid_self(),1003 if (!serverid_register(messaging_server_id(msg_ctx), 805 1004 FLAG_MSG_GENERAL|FLAG_MSG_SMBD 1005 |FLAG_MSG_PRINT_GENERAL 806 1006 |FLAG_MSG_DBWRAP)) { 807 1007 DEBUG(0, ("open_sockets_smbd: Failed to register " … … 812 1012 /* Listen to messages */ 813 1013 814 messaging_register(msg_ctx, NULL, MSG_SMB_SAM_SYNC, msg_sam_sync);815 1014 messaging_register(msg_ctx, NULL, MSG_SHUTDOWN, msg_exit_server); 816 messaging_register(msg_ctx, NULL, MSG_SMB_FILE_RENAME, 817 msg_file_was_renamed); 818 messaging_register(msg_ctx, server_event_context(), MSG_SMB_CONF_UPDATED, 819 smb_conf_updated); 1015 messaging_register(msg_ctx, ev_ctx, MSG_SMB_CONF_UPDATED, 1016 smbd_parent_conf_updated); 820 1017 messaging_register(msg_ctx, NULL, MSG_SMB_STAT_CACHE_DELETE, 821 1018 smb_stat_cache_delete); 822 1019 messaging_register(msg_ctx, NULL, MSG_DEBUG, smbd_msg_debug); 823 brl_register_msgs(msg_ctx); 824 825 msg_idmap_register_msgs(msg_ctx); 1020 messaging_register(msg_ctx, NULL, MSG_SMB_FORCE_TDIS, 1021 smb_parent_send_to_children); 1022 messaging_register(msg_ctx, NULL, MSG_SMB_KILL_CLIENT_IP, 1023 smb_parent_send_to_children); 1024 messaging_register(msg_ctx, NULL, MSG_SMB_TELL_NUM_CHILDREN, 1025 smb_tell_num_children); 1026 1027 messaging_register(msg_ctx, NULL, 1028 ID_CACHE_DELETE, smbd_parent_id_cache_delete); 1029 messaging_register(msg_ctx, NULL, 1030 ID_CACHE_KILL, smbd_parent_id_cache_kill); 826 1031 827 1032 #ifdef CLUSTER_SUPPORT 828 1033 if (lp_clustering()) { 829 ctdbd_register_reconfigure(messaging_ctdbd_connection()); 1034 struct ctdbd_connection *conn = messaging_ctdbd_connection(); 1035 1036 register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE, 1037 smbd_parent_ctdb_reconfigured, msg_ctx); 1038 register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY, 1039 smbd_parent_ctdb_reconfigured, msg_ctx); 830 1040 } 831 1041 #endif … … 838 1048 if (lp_multicast_dns_register() && (dns_port != 0)) { 839 1049 #ifdef WITH_DNSSD_SUPPORT 840 smbd_setup_mdns_registration( smbd_event_context(),1050 smbd_setup_mdns_registration(ev_ctx, 841 1051 parent, dns_port); 842 1052 #endif … … 844 1054 void *avahi_conn; 845 1055 846 avahi_conn = avahi_start_register( 847 smbd_event_context(), smbd_event_context(), dns_port); 1056 avahi_conn = avahi_start_register(ev_ctx, 1057 ev_ctx, 1058 dns_port); 848 1059 if (avahi_conn == NULL) { 849 1060 DEBUG(10, ("avahi_start_register failed\n")); … … 855 1066 } 856 1067 857 static void smbd_parent_loop(struct smbd_parent_context *parent) 858 { 1068 1069 /* 1070 handle stdin becoming readable when we are in --foreground mode 1071 */ 1072 static void smbd_stdin_handler(struct tevent_context *ev, 1073 struct tevent_fd *fde, 1074 uint16_t flags, 1075 void *private_data) 1076 { 1077 char c; 1078 if (read(0, &c, 1) != 1) { 1079 /* we have reached EOF on stdin, which means the 1080 parent has exited. Shutdown the server */ 1081 exit_server_cleanly("EOF on stdin"); 1082 } 1083 } 1084 1085 struct smbd_parent_tevent_trace_state { 1086 TALLOC_CTX *frame; 1087 }; 1088 1089 static void smbd_parent_tevent_trace_callback(enum tevent_trace_point point, 1090 void *private_data) 1091 { 1092 struct smbd_parent_tevent_trace_state *state = 1093 (struct smbd_parent_tevent_trace_state *)private_data; 1094 1095 switch (point) { 1096 case TEVENT_TRACE_BEFORE_WAIT: 1097 break; 1098 case TEVENT_TRACE_AFTER_WAIT: 1099 break; 1100 case TEVENT_TRACE_BEFORE_LOOP_ONCE: 1101 TALLOC_FREE(state->frame); 1102 state->frame = talloc_stackframe(); 1103 break; 1104 case TEVENT_TRACE_AFTER_LOOP_ONCE: 1105 TALLOC_FREE(state->frame); 1106 break; 1107 } 1108 1109 errno = 0; 1110 } 1111 1112 static void smbd_parent_loop(struct tevent_context *ev_ctx, 1113 struct smbd_parent_context *parent) 1114 { 1115 struct smbd_parent_tevent_trace_state trace_state = { 1116 .frame = NULL, 1117 }; 1118 int ret = 0; 1119 1120 tevent_set_trace_callback(ev_ctx, smbd_parent_tevent_trace_callback, 1121 &trace_state); 1122 859 1123 /* now accept incoming connections - forking a new process 860 1124 for each incoming connection */ 861 1125 DEBUG(2,("waiting for connections\n")); 862 while (1) { 863 int ret; 864 TALLOC_CTX *frame = talloc_stackframe(); 865 866 ret = tevent_loop_once(smbd_event_context()); 867 if (ret != 0) { 868 exit_server_cleanly("tevent_loop_once() error"); 869 } 870 871 TALLOC_FREE(frame); 872 } /* end while 1 */ 1126 1127 ret = tevent_loop_wait(ev_ctx); 1128 if (ret != 0) { 1129 DEBUG(0, ("tevent_loop_wait failed: %d, %s, exiting\n", 1130 ret, strerror(errno))); 1131 } 1132 1133 TALLOC_FREE(trace_state.frame); 873 1134 874 1135 /* NOTREACHED return True; */ … … 894 1155 895 1156 return True; 1157 } 1158 1159 static void smbd_parent_sig_term_handler(struct tevent_context *ev, 1160 struct tevent_signal *se, 1161 int signum, 1162 int count, 1163 void *siginfo, 1164 void *private_data) 1165 { 1166 exit_server_cleanly("termination signal"); 1167 } 1168 1169 static void smbd_parent_sig_hup_handler(struct tevent_context *ev, 1170 struct tevent_signal *se, 1171 int signum, 1172 int count, 1173 void *siginfo, 1174 void *private_data) 1175 { 1176 struct smbd_parent_context *parent = 1177 talloc_get_type_abort(private_data, 1178 struct smbd_parent_context); 1179 1180 change_to_root_user(); 1181 DEBUG(1,("parent: Reloading services after SIGHUP\n")); 1182 reload_services(NULL, NULL, false); 1183 1184 printing_subsystem_update(parent->ev_ctx, parent->msg_ctx, true); 896 1185 } 897 1186 … … 937 1226 {"profiling-level", 'P', POPT_ARG_STRING, &profile_level, 0, "Set profiling level","PROFILE_LEVEL"}, 938 1227 POPT_COMMON_SAMBA 939 POPT_COMMON_DYNCONFIG940 1228 POPT_TABLEEND 941 1229 }; … … 943 1231 TALLOC_CTX *frame; 944 1232 NTSTATUS status; 1233 struct tevent_context *ev_ctx; 1234 struct messaging_context *msg_ctx; 1235 struct server_id server_id; 1236 struct tevent_signal *se; 1237 int profiling_level; 1238 char *np_dir = NULL; 1239 static const struct smbd_shim smbd_shim_fns = 1240 { 1241 .cancel_pending_lock_requests_by_fid = smbd_cancel_pending_lock_requests_by_fid, 1242 .send_stat_cache_delete_message = smbd_send_stat_cache_delete_message, 1243 .change_to_root_user = smbd_change_to_root_user, 1244 .become_authenticated_pipe_user = smbd_become_authenticated_pipe_user, 1245 .unbecome_authenticated_pipe_user = smbd_unbecome_authenticated_pipe_user, 1246 1247 .contend_level2_oplocks_begin = smbd_contend_level2_oplocks_begin, 1248 .contend_level2_oplocks_end = smbd_contend_level2_oplocks_end, 1249 1250 .become_root = smbd_become_root, 1251 .unbecome_root = smbd_unbecome_root, 1252 1253 .exit_server = smbd_exit_server, 1254 .exit_server_cleanly = smbd_exit_server_cleanly, 1255 }; 945 1256 946 1257 /* … … 950 1261 frame = talloc_stackframe(); 951 1262 952 load_case_tables(); 953 954 /* Initialize the event context, it will panic on error */ 955 smbd_event_context(); 1263 setup_logging(argv[0], DEBUG_DEFAULT_STDOUT); 1264 1265 smb_init_locale(); 1266 1267 set_smbd_shim(&smbd_shim_fns); 956 1268 957 1269 smbd_init_globals(); … … 1025 1337 } 1026 1338 1027 /* we want to re-seed early to prevent time delays causing 1028 client problems at a later date. (tridge) */ 1339 /* 1340 * We want to die early if we can't open /dev/urandom 1341 */ 1029 1342 generate_random_buffer(NULL, 0); 1030 1343 … … 1037 1350 gain_root_group_privilege(); 1038 1351 1039 fault_setup( (void (*)(void *))exit_server_fault);1040 dump_core_setup("smbd" );1352 fault_setup(); 1353 dump_core_setup("smbd", lp_logfile(talloc_tos())); 1041 1354 1042 1355 /* we are never interested in SIGPIPE */ … … 1079 1392 build_options(False); 1080 1393 1081 if (sizeof(uint16 ) < 2 || sizeof(uint32) < 4) {1394 if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4) { 1082 1395 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n")); 1083 1396 exit(1); … … 1085 1398 1086 1399 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) { 1087 DEBUG(0, ("error opening config file\n")); 1400 DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE())); 1401 exit(1); 1402 } 1403 1404 if (!cluster_probe_ok()) { 1088 1405 exit(1); 1089 1406 } … … 1092 1409 init_sec_ctx(); 1093 1410 1094 if (smbd_messaging_context() == NULL) 1411 /* 1412 * Initialize the event context. The event context needs to be 1413 * initialized before the messaging context, cause the messaging 1414 * context holds an event context. 1415 * FIXME: This should be s3_tevent_context_init() 1416 */ 1417 ev_ctx = server_event_context(); 1418 if (ev_ctx == NULL) { 1095 1419 exit(1); 1420 } 1421 1422 /* 1423 * Init the messaging context 1424 * FIXME: This should only call messaging_init() 1425 */ 1426 msg_ctx = server_messaging_context(); 1427 if (msg_ctx == NULL) { 1428 exit(1); 1429 } 1096 1430 1097 1431 /* … … 1099 1433 * server info and rpc services set up. It will be called later. 1100 1434 */ 1101 if (!reload_services( smbd_messaging_context(), -1, False)) {1435 if (!reload_services(NULL, NULL, false)) { 1102 1436 exit(1); 1103 1437 } 1104 1438 1439 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC 1440 && !lp_parm_bool(-1, "server role check", "inhibit", false)) { 1441 DEBUG(0, ("server role = 'active directory domain controller' not compatible with running smbd standalone. \n")); 1442 DEBUGADD(0, ("You should start 'samba' instead, and it will control starting smbd if required\n")); 1443 exit(1); 1444 } 1445 1105 1446 /* ...NOTE... Log files are working from this point! */ 1106 1447 … … 1109 1450 init_structs(); 1110 1451 1111 #ifdef WITH_PROFILE 1112 if (!profile_setup(smbd_messaging_context(), False)) { 1452 if (!profile_setup(msg_ctx, False)) { 1113 1453 DEBUG(0,("ERROR: failed to setup profiling\n")); 1114 1454 return -1; 1115 1455 } 1456 1116 1457 if (profile_level != NULL) { 1117 int pl = atoi(profile_level); 1118 struct server_id src; 1119 1120 DEBUG(1, ("setting profiling level: %s\n",profile_level)); 1121 src.pid = getpid(); 1122 set_profile_level(pl, src); 1123 } 1124 #endif 1458 profiling_level = atoi(profile_level); 1459 } else { 1460 profiling_level = lp_smbd_profiling_level(); 1461 } 1462 set_profile_level(profiling_level, messaging_server_id(msg_ctx)); 1125 1463 1126 1464 if (!is_daemon && !is_a_socket(0)) { 1127 if (!interactive) 1128 DEBUG(0,("standard input is not a socket, assuming -D option\n")); 1465 if (!interactive) { 1466 DEBUG(3, ("Standard input is not a socket, " 1467 "assuming -D option\n")); 1468 } 1129 1469 1130 1470 /* … … 1137 1477 1138 1478 if (is_daemon && !interactive) { 1139 DEBUG( 3, ( "Becoming a daemon.\n" ));1479 DEBUG(3, ("Becoming a daemon.\n")); 1140 1480 become_daemon(Fork, no_process_group, log_stdout); 1141 1481 } 1142 1143 set_my_unique_id(serverid_get_random_unique_id());1144 1482 1145 1483 #if HAVE_SETPGID … … 1152 1490 #endif 1153 1491 1154 if (!directory_exist(lp_lockdir())) 1155 mkdir(lp_lockdir(), 0755); 1492 if (!directory_exist(lp_lock_directory())) 1493 mkdir(lp_lock_directory(), 0755); 1494 1495 if (!directory_exist(lp_pid_directory())) 1496 mkdir(lp_pid_directory(), 0755); 1156 1497 1157 1498 if (is_daemon) 1158 pidfile_create("smbd"); 1159 1160 status = reinit_after_fork(smbd_messaging_context(), 1161 smbd_event_context(), 1162 procid_self(), false); 1499 pidfile_create(lp_pid_directory(), "smbd"); 1500 1501 status = reinit_after_fork(msg_ctx, ev_ctx, false, NULL); 1163 1502 if (!NT_STATUS_IS_OK(status)) { 1164 DEBUG(0,("reinit_after_fork() failed\n")); 1165 exit(1); 1166 } 1167 1168 smbd_server_conn->msg_ctx = smbd_messaging_context(); 1169 1170 smbd_setup_sig_term_handler(); 1171 smbd_setup_sig_hup_handler(smbd_event_context(), 1172 smbd_server_conn->msg_ctx); 1503 exit_daemon("reinit_after_fork() failed", map_errno_from_nt_status(status)); 1504 } 1505 1506 if (!interactive) { 1507 /* 1508 * Do not initialize the parent-child-pipe before becoming a 1509 * daemon: this is used to detect a died parent in the child 1510 * process. 1511 */ 1512 status = init_before_fork(); 1513 if (!NT_STATUS_IS_OK(status)) { 1514 exit_daemon(nt_errstr(status), map_errno_from_nt_status(status)); 1515 } 1516 } 1517 1518 parent = talloc_zero(ev_ctx, struct smbd_parent_context); 1519 if (!parent) { 1520 exit_server("talloc(struct smbd_parent_context) failed"); 1521 } 1522 parent->interactive = interactive; 1523 parent->ev_ctx = ev_ctx; 1524 parent->msg_ctx = msg_ctx; 1525 am_parent = parent; 1526 1527 se = tevent_add_signal(parent->ev_ctx, 1528 parent, 1529 SIGTERM, 0, 1530 smbd_parent_sig_term_handler, 1531 parent); 1532 if (!se) { 1533 exit_server("failed to setup SIGTERM handler"); 1534 } 1535 se = tevent_add_signal(parent->ev_ctx, 1536 parent, 1537 SIGHUP, 0, 1538 smbd_parent_sig_hup_handler, 1539 parent); 1540 if (!se) { 1541 exit_server("failed to setup SIGHUP handler"); 1542 } 1173 1543 1174 1544 /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */ 1175 1545 1176 1546 if (smbd_memcache() == NULL) { 1177 exit (1);1547 exit_daemon("no memcache available", EACCES); 1178 1548 } 1179 1549 … … 1183 1553 to ensure that we fetch from ldap before we make a domain sid up */ 1184 1554 1185 if(!initialize_password_db( False, smbd_event_context()))1555 if(!initialize_password_db(false, ev_ctx)) 1186 1556 exit(1); 1187 1557 1188 1558 if (!secrets_init()) { 1189 DEBUG(0, ("ERROR: smbd can not open secrets.tdb\n")); 1559 exit_daemon("smbd can not open secrets.tdb", EACCES); 1560 } 1561 1562 if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) { 1563 struct loadparm_context *lp_ctx = loadparm_init_s3(NULL, loadparm_s3_helpers()); 1564 if (!open_schannel_session_store(NULL, lp_ctx)) { 1565 exit_daemon("ERROR: Samba cannot open schannel store for secured NETLOGON operations.", EACCES); 1566 } 1567 TALLOC_FREE(lp_ctx); 1568 } 1569 1570 if(!get_global_sam_sid()) { 1571 exit_daemon("Samba cannot create a SAM SID", EACCES); 1572 } 1573 1574 server_id = messaging_server_id(msg_ctx); 1575 status = smbXsrv_version_global_init(&server_id); 1576 if (!NT_STATUS_IS_OK(status)) { 1577 exit_daemon("Samba cannot init server context", EACCES); 1578 } 1579 1580 status = smbXsrv_session_global_init(); 1581 if (!NT_STATUS_IS_OK(status)) { 1582 exit_daemon("Samba cannot init session context", EACCES); 1583 } 1584 1585 status = smbXsrv_tcon_global_init(); 1586 if (!NT_STATUS_IS_OK(status)) { 1587 exit_daemon("Samba cannot init tcon context", EACCES); 1588 } 1589 1590 if (!locking_init()) 1591 exit_daemon("Samba cannot init locking", EACCES); 1592 1593 if (!leases_db_init(false)) { 1594 exit_daemon("Samba cannot init leases", EACCES); 1595 } 1596 1597 if (!smbd_notifyd_init(msg_ctx, interactive)) { 1598 exit_daemon("Samba cannot init notification", EACCES); 1599 } 1600 1601 if (!cleanupd_init(msg_ctx, interactive, &parent->cleanupd)) { 1602 exit_daemon("Samba cannot init the cleanupd", EACCES); 1603 } 1604 1605 if (!messaging_parent_dgm_cleanup_init(msg_ctx)) { 1190 1606 exit(1); 1191 1607 } 1192 1608 1193 if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) { 1194 if (!open_schannel_session_store(NULL, lp_private_dir())) { 1195 DEBUG(0,("ERROR: Samba cannot open schannel store for secured NETLOGON operations.\n")); 1196 exit(1); 1197 } 1198 } 1199 1200 if(!get_global_sam_sid()) { 1201 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n")); 1202 exit(1); 1203 } 1204 1205 if (!sessionid_init()) { 1206 exit(1); 1207 } 1208 1209 if (!connections_init(True)) 1210 exit(1); 1211 1212 if (!locking_init()) 1213 exit(1); 1214 1215 if (!messaging_tdb_parent_init(smbd_event_context())) { 1216 exit(1); 1217 } 1218 1219 if (!notify_internal_parent_init(smbd_event_context())) { 1220 exit(1); 1221 } 1222 1223 if (!serverid_parent_init(smbd_event_context())) { 1224 exit(1); 1225 } 1226 1227 if (!printer_list_parent_init()) { 1228 exit(1); 1609 if (!smbd_scavenger_init(NULL, msg_ctx, ev_ctx)) { 1610 exit_daemon("Samba cannot init scavenging", EACCES); 1611 } 1612 1613 if (!serverid_parent_init(ev_ctx)) { 1614 exit_daemon("Samba cannot init server id", EACCES); 1229 1615 } 1230 1616 1231 1617 if (!W_ERROR_IS_OK(registry_init_full())) 1232 exit (1);1618 exit_daemon("Samba cannot init registry", EACCES); 1233 1619 1234 1620 /* Open the share_info.tdb here, so we don't have to open … … 1237 1623 fds used. */ 1238 1624 if (!share_info_db_init()) { 1239 DEBUG(0,("ERROR: failed to load share info db.\n")); 1240 exit(1); 1241 } 1242 1243 status = init_system_info(); 1625 exit_daemon("ERROR: failed to load share info db.", EACCES); 1626 } 1627 1628 status = init_system_session_info(); 1244 1629 if (!NT_STATUS_IS_OK(status)) { 1245 1630 DEBUG(1, ("ERROR: failed to setup system user info: %s.\n", … … 1253 1638 } 1254 1639 1255 if (!file_init (smbd_server_conn)) {1256 DEBUG(0, ("ERROR: file_init failed\n"));1640 if (!file_init_global()) { 1641 DEBUG(0, ("ERROR: file_init_global() failed\n")); 1257 1642 return -1; 1258 1643 } 1644 status = smbXsrv_open_global_init(); 1645 if (!NT_STATUS_IS_OK(status)) { 1646 exit_daemon("Samba cannot init global open", map_errno_from_nt_status(status)); 1647 } 1648 1649 /* This MUST be done before start_epmd() because otherwise 1650 * start_epmd() forks and races against dcesrv_ep_setup() to 1651 * call directory_create_or_exist() */ 1652 if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) { 1653 DEBUG(0, ("Failed to create pipe directory %s - %s\n", 1654 lp_ncalrpc_dir(), strerror(errno))); 1655 return -1; 1656 } 1657 1658 np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir()); 1659 if (!np_dir) { 1660 DEBUG(0, ("%s: Out of memory\n", __location__)); 1661 return -1; 1662 } 1663 1664 if (!directory_create_or_exist_strict(np_dir, geteuid(), 0700)) { 1665 DEBUG(0, ("Failed to create pipe directory %s - %s\n", 1666 np_dir, strerror(errno))); 1667 return -1; 1668 } 1259 1669 1260 1670 if (is_daemon && !interactive) { 1261 const char *rpcsrv_type; 1262 1263 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM, 1264 "rpc_server", "epmapper", 1265 "none"); 1266 if (StrCaseCmp(rpcsrv_type, "daemon") == 0) { 1267 start_epmd(smbd_event_context(), 1268 smbd_server_conn->msg_ctx); 1269 } 1270 } 1271 1272 if (!dcesrv_ep_setup(smbd_event_context(), smbd_server_conn->msg_ctx)) { 1273 exit(1); 1274 } 1275 1276 /* 1277 * The print backend init also migrates the printing tdb's, 1278 * this requires a winreg pipe. 1279 */ 1280 if (!print_backend_init(smbd_messaging_context())) 1281 exit(1); 1282 1283 /* only start the background queue daemon if we are 1284 running as a daemon -- bad things will happen if 1285 smbd is launched via inetd and we fork a copy of 1286 ourselves here */ 1287 1288 if (is_daemon && !interactive 1289 && lp_parm_bool(-1, "smbd", "backgroundqueue", true)) { 1290 /* background queue is responsible for printcap cache updates */ 1291 messaging_register(smbd_server_conn->msg_ctx, 1292 smbd_event_context(), 1293 MSG_PRINTER_PCAP, smb_pcap_updated); 1294 start_background_queue(server_event_context(), 1295 smbd_server_conn->msg_ctx); 1296 } else { 1297 DEBUG(3, ("running without background printer process, dynamic " 1298 "printer updates disabled\n")); 1299 /* Publish nt printers, this requires a working winreg pipe */ 1300 pcap_cache_reload(server_event_context(), 1301 smbd_messaging_context(), 1302 &reload_printers_full); 1303 } 1304 1305 if (is_daemon && !_lp_disable_spoolss()) { 1306 const char *rpcsrv_type; 1307 1308 /* start spoolss daemon */ 1309 /* start as a separate daemon only if enabled */ 1310 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM, 1311 "rpc_server", "spoolss", 1312 "embedded"); 1313 if (StrCaseCmp(rpcsrv_type, "daemon") == 0) { 1314 start_spoolssd(smbd_event_context(), 1315 smbd_messaging_context()); 1671 if (rpc_epmapper_daemon() == RPC_DAEMON_FORK) { 1672 start_epmd(ev_ctx, msg_ctx); 1673 } 1674 } 1675 1676 if (!dcesrv_ep_setup(ev_ctx, msg_ctx)) { 1677 exit_daemon("Samba cannot setup ep pipe", EACCES); 1678 } 1679 1680 if (is_daemon && !interactive) { 1681 daemon_ready("smbd"); 1682 } 1683 1684 /* only start other daemons if we are running as a daemon 1685 * -- bad things will happen if smbd is launched via inetd 1686 * and we fork a copy of ourselves here */ 1687 if (is_daemon && !interactive) { 1688 1689 if (rpc_lsasd_daemon() == RPC_DAEMON_FORK) { 1690 start_lsasd(ev_ctx, msg_ctx); 1691 } 1692 1693 if (rpc_fss_daemon() == RPC_DAEMON_FORK) { 1694 start_fssd(ev_ctx, msg_ctx); 1695 } 1696 1697 if (!lp__disable_spoolss() && 1698 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) { 1699 bool bgq = lp_parm_bool(-1, "smbd", "backgroundqueue", true); 1700 1701 if (!printing_subsystem_init(ev_ctx, msg_ctx, true, bgq)) { 1702 exit_daemon("Samba failed to init printing subsystem", EACCES); 1703 } 1704 } 1705 1706 #ifdef WITH_SPOTLIGHT 1707 if ((rpc_mdssvc_mode() == RPC_SERVICE_MODE_EXTERNAL) && 1708 (rpc_mdssd_daemon() == RPC_DAEMON_FORK)) { 1709 start_mdssd(ev_ctx, msg_ctx); 1710 } 1711 #endif 1712 } else if (!lp__disable_spoolss() && 1713 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) { 1714 if (!printing_subsystem_init(ev_ctx, msg_ctx, false, false)) { 1715 exit(1); 1316 1716 } 1317 1717 } 1318 1718 1319 1719 if (!is_daemon) { 1720 int sock; 1721 1320 1722 /* inetd mode */ 1321 1723 TALLOC_FREE(frame); … … 1324 1726 /* We will abort gracefully when the client or remote system 1325 1727 goes away */ 1326 s mbd_set_server_fd(dup(0));1327 1328 /* close our standard file descriptors*/1329 close_low_fds( False); /* Don't close stderr */1728 sock = dup(0); 1729 1730 /* close stdin, stdout (if not logging to it), but not stderr */ 1731 close_low_fds(true, !debug_get_output_is_stdout(), false); 1330 1732 1331 1733 #ifdef HAVE_ATEXIT … … 1334 1736 1335 1737 /* Stop zombies */ 1336 smbd_setup_sig_chld_handler( );1337 1338 smbd_process( smbd_server_conn);1738 smbd_setup_sig_chld_handler(parent); 1739 1740 smbd_process(ev_ctx, msg_ctx, sock, true); 1339 1741 1340 1742 exit_server_cleanly(NULL); … … 1342 1744 } 1343 1745 1344 parent = talloc_zero(smbd_event_context(), struct smbd_parent_context); 1345 if (!parent) { 1346 exit_server("talloc(struct smbd_parent_context) failed"); 1347 } 1348 parent->interactive = interactive; 1349 1350 if (!open_sockets_smbd(parent, smbd_messaging_context(), ports)) 1746 if (!open_sockets_smbd(parent, ev_ctx, msg_ctx, ports)) 1351 1747 exit_server("open_sockets_smbd() failed"); 1748 1749 /* do a printer update now that all messaging has been set up, 1750 * before we allow clients to start connecting */ 1751 if (!lp__disable_spoolss() && 1752 (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) { 1753 printing_subsystem_update(ev_ctx, msg_ctx, false); 1754 } 1352 1755 1353 1756 TALLOC_FREE(frame); … … 1355 1758 frame = talloc_stackframe(); 1356 1759 1357 smbd_parent_loop(parent); 1760 if (!Fork) { 1761 /* if we are running in the foreground then look for 1762 EOF on stdin, and exit if it happens. This allows 1763 us to die if the parent process dies 1764 Only do this on a pipe or socket, no other device. 1765 */ 1766 struct stat st; 1767 if (fstat(0, &st) != 0) { 1768 return false; 1769 } 1770 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) { 1771 tevent_add_fd(ev_ctx, 1772 parent, 1773 0, 1774 TEVENT_FD_READ, 1775 smbd_stdin_handler, 1776 NULL); 1777 } 1778 } 1779 1780 smbd_parent_loop(ev_ctx, parent); 1358 1781 1359 1782 exit_server_cleanly(NULL);
Note:
See TracChangeset
for help on using the changeset viewer.