Ignore:
Timestamp:
Nov 24, 2016, 1:14:11 PM (9 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: update vendor to version 4.4.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/current/source3/smbd/server.c

    r860 r988  
    3030#include "libcli/auth/schannel.h"
    3131#include "secrets.h"
    32 #include "memcache.h"
     32#include "../lib/util/memcache.h"
    3333#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"
    3838#include "serverid.h"
    3939#include "passdb.h"
     
    4141#include "messages.h"
    4242#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
     59struct smbd_open_socket;
     60struct smbd_child_pid;
     61
     62struct 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
     80struct 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
     87struct smbd_child_pid {
     88        struct smbd_child_pid *prev, *next;
     89        pid_t pid;
     90};
    4391
    4492extern void start_epmd(struct tevent_context *ev_ctx,
    4593                       struct messaging_context *msg_ctx);
    4694
    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 }
     95extern void start_lsasd(struct tevent_context *ev_ctx,
     96                        struct messaging_context *msg_ctx);
     97
     98extern void start_fssd(struct tevent_context *ev_ctx,
     99                       struct messaging_context *msg_ctx);
     100
     101extern void start_mdssd(struct tevent_context *ev_ctx,
     102                        struct messaging_context *msg_ctx);
    93103
    94104/*******************************************************************
     
    96106 ********************************************************************/
    97107
    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)
     108static 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)
    103113{
    104114        struct tevent_context *ev_ctx =
    105115                talloc_get_type_abort(private_data, struct tevent_context);
    106116
    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 "
    108118                  "updated. Reloading.\n"));
    109119        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}
    182123
    183124/*******************************************************************
     
    203144{
    204145        if(am_parent) kill(0,SIGTERM);
    205 }
    206 
    207 /****************************************************************************
    208  Process a sam sync message - not sure whether to do this here or
    209  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"));
    219146}
    220147
     
    237164{
    238165        int sig;
     166        struct server_id_buf tmp;
    239167
    240168        if (data->length != sizeof(sig)) {
    241169                DEBUG(0, ("Process %s sent bogus signal injection request\n",
    242                           procid_str_static(&src)));
     170                          server_id_str_buf(src, &tmp)));
    243171                return;
    244172        }
     
    252180#if HAVE_STRSIGNAL
    253181        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)));
    255183#else
    256184        DEBUG(0, ("Process %s requested injection of signal %d\n",
    257                   procid_str_static(&src), sig));
     185                  server_id_str_buf(src, &tmp), sig));
    258186#endif
    259187
    260         kill(sys_getpid(), sig);
     188        kill(getpid(), sig);
    261189}
    262190#endif /* DEVELOPER */
     191
     192static 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
     214static 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}
    263222
    264223/*
     
    274233                           DATA_BLOB *data)
    275234{
    276         struct child_pid *child;
    277 
    278235        debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
    279236
    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
     240static 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
     260static 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
     272static 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
     295static 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);
    293301        if (child == NULL) {
    294302                DEBUG(0, ("Could not add child struct -- malloc failed\n"));
     
    296304        }
    297305        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
     310static 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
     323static void notifyd_stopped(struct tevent_req *req);
     324
     325static 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
     367static 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
     376static 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
     414static void cleanupd_stopped(struct tevent_req *req);
     415
     416static 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
     522static 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));
    300528}
    301529
     
    311539*/
    312540
    313 static void cleanup_timeout_fn(struct event_context *event_ctx,
    314                                 struct timed_event *te,
     541static void cleanup_timeout_fn(struct tevent_context *event_ctx,
     542                                struct tevent_timer *te,
    315543                                struct timeval now,
    316544                                void *private_data)
    317545{
    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
     556static 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) {
    338565                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;
    343570                        break;
    344571                }
     
    350577                return;
    351578        }
     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)));
    352600
    353601        if (unclean_shutdown) {
     
    358606                DEBUG(3,(__location__ " Unclean shutdown of pid %u\n",
    359607                        (unsigned int)pid));
    360                 if (!cleanup_te) {
     608                if (parent->cleanup_te == NULL) {
    361609                        /* call the cleanup timer, but not too often */
    362610                        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,
    364613                                                timeval_current_ofs(cleanup_time, 0),
    365614                                                cleanup_timeout_fn,
    366                                                 &cleanup_te);
     615                                                parent);
    367616                        DEBUG(1,("Scheduled cleanup of brl and lock database after unclean shutdown\n"));
    368617                }
    369         }
    370 
    371         if (!serverid_deregister(child_id)) {
    372                 DEBUG(1, ("Could not remove pid %d from serverid.tdb\n",
    373                           (int)pid));
    374618        }
    375619}
     
    379623****************************************************************************/
    380624
    381 static bool allowable_number_of_smbd_processes(void)
     625static bool allowable_number_of_smbd_processes(struct smbd_parent_context *parent)
    382626{
    383627        int max_processes = lp_max_smbd_processes();
     
    386630                return True;
    387631
    388         return num_children < max_processes;
     632        return parent->num_children < max_processes;
    389633}
    390634
     
    398642        pid_t pid;
    399643        int status;
     644        struct smbd_parent_context *parent =
     645                talloc_get_type_abort(private_data,
     646                struct smbd_parent_context);
    400647
    401648        while ((pid = sys_waitpid(-1, &status, WNOHANG)) > 0) {
     
    415662                        unclean_shutdown = True;
    416663                }
    417                 remove_child_pid(pid, 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
     668static void smbd_setup_sig_chld_handler(struct smbd_parent_context *parent)
    422669{
    423670        struct tevent_signal *se;
    424671
    425         se = tevent_add_signal(smbd_event_context(),
    426                                smbd_event_context(),
     672        se = tevent_add_signal(parent->ev_ctx,
     673                               parent, /* mem_ctx */
    427674                               SIGCHLD, 0,
    428675                               smbd_sig_chld_handler,
    429                                NULL);
     676                               parent);
    430677        if (!se) {
    431678                exit_server("failed to setup SIGCHLD handler");
    432679        }
    433680}
    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 };
    450681
    451682static void smbd_open_socket_close_fn(struct tevent_context *ev,
     
    465696        struct smbd_open_socket *s = talloc_get_type_abort(private_data,
    466697                                     struct smbd_open_socket);
     698        struct messaging_context *msg_ctx = s->parent->msg_ctx;
    467699        struct sockaddr_storage addr;
    468700        socklen_t in_addrlen = sizeof(addr);
    469701        int fd;
    470702        pid_t pid = 0;
    471         uint64_t unique_id;
    472703
    473704        fd = accept(s->fd, (struct sockaddr *)(void *)&addr,&in_addrlen);
    474         smbd_set_server_fd(fd);
    475 
    476705        if (fd == -1 && errno == EINTR)
    477706                return;
    478707
    479708        if (fd == -1) {
    480                 DEBUG(0,("open_sockets_smbd: accept: %s\n",
     709                DEBUG(0,("accept: %s\n",
    481710                         strerror(errno)));
    482711                return;
     
    484713
    485714        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);
    487717                exit_server_cleanly("end of interactive mode");
    488718                return;
    489719        }
    490720
    491         if (!allowable_number_of_smbd_processes()) {
     721        if (!allowable_number_of_smbd_processes(s->parent)) {
    492722                close(fd);
    493                 smbd_set_server_fd(-1);
    494723                return;
    495724        }
    496725
    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();
    504727        if (pid == 0) {
    505728                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 handles
    513                  * them, counting worker smbds. */
    514                 CatchChild();
    515 
    516                 /* close our standard file
    517                    descriptors */
    518                 close_low_fds(False);
    519729
    520730                /*
     
    525735                s = NULL;
    526736
    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);
    530742                if (!NT_STATUS_IS_OK(status)) {
    531743                        if (NT_STATUS_EQUAL(status,
     
    548760                }
    549761
    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);
    563763         exit:
    564764                exit_server_cleanly("end of child");
     
    567767
    568768        if (pid < 0) {
    569                 DEBUG(0,("smbd_accept_connection: sys_fork() failed: %s\n",
     769                DEBUG(0,("smbd_accept_connection: fork() failed: %s\n",
    570770                         strerror(errno)));
    571771        }
     
    582782        */
    583783
    584         smbd_set_server_fd(-1);
    585 
    586784        if (pid != 0) {
    587                 add_child_pid(pid);
     785                add_child_pid(s->parent, pid);
    588786        }
    589787
     
    606804
    607805static bool smbd_open_one_socket(struct smbd_parent_context *parent,
     806                                 struct tevent_context *ev_ctx,
    608807                                 const struct sockaddr_storage *ifss,
    609808                                 uint16_t port)
     
    623822                               true);
    624823        if (s->fd == -1) {
    625                 DEBUG(0,("smbd_open_once_socket: open_socket_in: "
     824                DEBUG(0,("smbd_open_one_socket: open_socket_in: "
    626825                        "%s\n", strerror(errno)));
    627826                TALLOC_FREE(s);
     
    641840
    642841        if (listen(s->fd, SMBD_LISTEN_BACKLOG) == -1) {
    643                 DEBUG(0,("open_sockets_smbd: listen: "
     842                DEBUG(0,("smbd_open_one_socket: listen: "
    644843                        "%s\n", strerror(errno)));
    645844                        close(s->fd);
     
    648847        }
    649848
    650         s->fde = tevent_add_fd(smbd_event_context(),
     849        s->fde = tevent_add_fd(ev_ctx,
    651850                               s,
    652851                               s->fd, TEVENT_FD_READ,
     
    654853                               s);
    655854        if (!s->fde) {
    656                 DEBUG(0,("open_sockets_smbd: "
     855                DEBUG(0,("smbd_open_one_socket: "
    657856                         "tevent_add_fd: %s\n",
    658857                         strerror(errno)));
     
    663862        tevent_fd_set_close_fn(s->fde, smbd_open_socket_close_fn);
    664863
    665         DLIST_ADD_END(parent->sockets, s, struct smbd_open_socket *);
     864        DLIST_ADD_END(parent->sockets, s);
    666865
    667866        return true;
     
    673872
    674873static bool open_sockets_smbd(struct smbd_parent_context *parent,
     874                              struct tevent_context *ev_ctx,
    675875                              struct messaging_context *msg_ctx,
    676876                              const char *smb_ports)
    677877{
    678878        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;
    683881        unsigned dns_port = 0;
    684882
     
    688886
    689887        /* Stop zombies */
    690         smbd_setup_sig_chld_handler();
     888        smbd_setup_sig_chld_handler(parent);
     889
     890        ports = lp_smb_ports();
    691891
    692892        /* 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]);
    707901
    708902                if (port == 0 || port > 0xffff) {
     
    723917                        const struct sockaddr_storage *ifss =
    724918                                        iface_n_sockaddr_storage(i);
    725 
    726919                        if (ifss == NULL) {
    727920                                DEBUG(0,("open_sockets_smbd: "
     
    731924                        }
    732925
    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]);
    736928
    737929                                /* Keep the first port for mDNS service
     
    742934                                }
    743935
    744                                 if (!smbd_open_one_socket(parent, ifss, port)) {
     936                                if (!smbd_open_one_socket(parent,
     937                                                          ev_ctx,
     938                                                          ifss,
     939                                                          port)) {
    745940                                        return false;
    746941                                }
     
    751946                   from anywhere. */
    752947
    753                 const char *sock_addr = lp_socket_address();
     948                const char *sock_addr;
    754949                char *sock_tok;
    755950                const char *sock_ptr;
    756951
    757                 if (strequal(sock_addr, "0.0.0.0") ||
    758                     strequal(sock_addr, "::")) {
    759952#if HAVE_IPV6
    760                         sock_addr = "::,0.0.0.0";
     953                sock_addr = "::,0.0.0.0";
    761954#else
    762                         sock_addr = "0.0.0.0";
     955                sock_addr = "0.0.0.0";
    763956#endif
    764                 }
    765957
    766958                for (sock_ptr=sock_addr;
    767959                     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++) {
    769961                                struct sockaddr_storage ss;
    770                                 unsigned port = atoi(tok);
     962                                unsigned port = atoi(ports[j]);
    771963
    772964                                /* Keep the first port for mDNS service
     
    783975                                }
    784976
    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);
    788987                        }
    789988                }
     
    8021001           includes checking to see that smbd is listening. */
    8031002
    804         if (!serverid_register(procid_self(),
     1003        if (!serverid_register(messaging_server_id(msg_ctx),
    8051004                               FLAG_MSG_GENERAL|FLAG_MSG_SMBD
     1005                               |FLAG_MSG_PRINT_GENERAL
    8061006                               |FLAG_MSG_DBWRAP)) {
    8071007                DEBUG(0, ("open_sockets_smbd: Failed to register "
     
    8121012        /* Listen to messages */
    8131013
    814         messaging_register(msg_ctx, NULL, MSG_SMB_SAM_SYNC, msg_sam_sync);
    8151014        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);
    8201017        messaging_register(msg_ctx, NULL, MSG_SMB_STAT_CACHE_DELETE,
    8211018                           smb_stat_cache_delete);
    8221019        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);
    8261031
    8271032#ifdef CLUSTER_SUPPORT
    8281033        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);
    8301040        }
    8311041#endif
     
    8381048        if (lp_multicast_dns_register() && (dns_port != 0)) {
    8391049#ifdef WITH_DNSSD_SUPPORT
    840                 smbd_setup_mdns_registration(smbd_event_context(),
     1050                smbd_setup_mdns_registration(ev_ctx,
    8411051                                             parent, dns_port);
    8421052#endif
     
    8441054                void *avahi_conn;
    8451055
    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);
    8481059                if (avahi_conn == NULL) {
    8491060                        DEBUG(10, ("avahi_start_register failed\n"));
     
    8551066}
    8561067
    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 */
     1072static 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
     1085struct smbd_parent_tevent_trace_state {
     1086        TALLOC_CTX *frame;
     1087};
     1088
     1089static 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
     1112static 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
    8591123        /* now accept incoming connections - forking a new process
    8601124           for each incoming connection */
    8611125        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);
    8731134
    8741135/* NOTREACHED   return True; */
     
    8941155
    8951156        return True;
     1157}
     1158
     1159static 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
     1169static 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);
    8961185}
    8971186
     
    9371226        {"profiling-level", 'P', POPT_ARG_STRING, &profile_level, 0, "Set profiling level","PROFILE_LEVEL"},
    9381227        POPT_COMMON_SAMBA
    939         POPT_COMMON_DYNCONFIG
    9401228        POPT_TABLEEND
    9411229        };
     
    9431231        TALLOC_CTX *frame;
    9441232        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        };
    9451256
    9461257        /*
     
    9501261        frame = talloc_stackframe();
    9511262
    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);
    9561268
    9571269        smbd_init_globals();
     
    10251337        }
    10261338
    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         */
    10291342        generate_random_buffer(NULL, 0);
    10301343
     
    10371350        gain_root_group_privilege();
    10381351
    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()));
    10411354
    10421355        /* we are never interested in SIGPIPE */
     
    10791392        build_options(False);
    10801393
    1081         if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
     1394        if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4) {
    10821395                DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
    10831396                exit(1);
     
    10851398
    10861399        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()) {
    10881405                exit(1);
    10891406        }
     
    10921409        init_sec_ctx();
    10931410
    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) {
    10951419                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        }
    10961430
    10971431        /*
     
    10991433         * server info and rpc services set up. It will be called later.
    11001434         */
    1101         if (!reload_services(smbd_messaging_context(), -1, False)) {
     1435        if (!reload_services(NULL, NULL, false)) {
    11021436                exit(1);
    11031437        }
    11041438
     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
    11051446        /* ...NOTE... Log files are working from this point! */
    11061447
     
    11091450        init_structs();
    11101451
    1111 #ifdef WITH_PROFILE
    1112         if (!profile_setup(smbd_messaging_context(), False)) {
     1452        if (!profile_setup(msg_ctx, False)) {
    11131453                DEBUG(0,("ERROR: failed to setup profiling\n"));
    11141454                return -1;
    11151455        }
     1456
    11161457        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));
    11251463
    11261464        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                }
    11291469
    11301470                /*
     
    11371477
    11381478        if (is_daemon && !interactive) {
    1139                 DEBUG( 3, ( "Becoming a daemon.\n" ) );
     1479                DEBUG(3, ("Becoming a daemon.\n"));
    11401480                become_daemon(Fork, no_process_group, log_stdout);
    11411481        }
    1142 
    1143         set_my_unique_id(serverid_get_random_unique_id());
    11441482
    11451483#if HAVE_SETPGID
     
    11521490#endif
    11531491
    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);
    11561497
    11571498        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);
    11631502        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        }
    11731543
    11741544        /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
    11751545
    11761546        if (smbd_memcache() == NULL) {
    1177                 exit(1);
     1547                exit_daemon("no memcache available", EACCES);
    11781548        }
    11791549
     
    11831553           to ensure that we fetch from ldap before we make a domain sid up */
    11841554
    1185         if(!initialize_password_db(False, smbd_event_context()))
     1555        if(!initialize_password_db(false, ev_ctx))
    11861556                exit(1);
    11871557
    11881558        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)) {
    11901606                exit(1);
    11911607        }
    11921608
    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);
    12291615        }
    12301616
    12311617        if (!W_ERROR_IS_OK(registry_init_full()))
    1232                 exit(1);
     1618                exit_daemon("Samba cannot init registry", EACCES);
    12331619
    12341620        /* Open the share_info.tdb here, so we don't have to open
     
    12371623           fds used. */
    12381624        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();
    12441629        if (!NT_STATUS_IS_OK(status)) {
    12451630                DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
     
    12531638        }
    12541639
    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"));
    12571642                return -1;
    12581643        }
     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        }
    12591669
    12601670        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);
    13161716                }
    13171717        }
    13181718
    13191719        if (!is_daemon) {
     1720                int sock;
     1721
    13201722                /* inetd mode */
    13211723                TALLOC_FREE(frame);
     
    13241726                /* We will abort gracefully when the client or remote system
    13251727                   goes away */
    1326                 smbd_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);
    13301732
    13311733#ifdef HAVE_ATEXIT
     
    13341736
    13351737                /* 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);
    13391741
    13401742                exit_server_cleanly(NULL);
     
    13421744        }
    13431745
    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))
    13511747                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        }
    13521755
    13531756        TALLOC_FREE(frame);
     
    13551758        frame = talloc_stackframe();
    13561759
    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);
    13581781
    13591782        exit_server_cleanly(NULL);
Note: See TracChangeset for help on using the changeset viewer.