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/printing/spoolssd.c

    r860 r988  
    22   Unix SMB/Netbios implementation.
    33   SPOOLSS Daemon
    4    Copyright (C) Simo Sorce 2010
     4   Copyright (C) Simo Sorce <idra@samba.org> 2010-2011
    55
    66   This program is free software; you can redistribute it and/or modify
     
    2424#include "include/printing.h"
    2525#include "printing/nt_printing_migrate_internal.h"
     26#include "printing/queue_process.h"
     27#include "printing/pcap.h"
     28#include "printing/load.h"
    2629#include "ntdomain.h"
    2730#include "librpc/gen_ndr/srv_winreg.h"
    2831#include "librpc/gen_ndr/srv_spoolss.h"
    2932#include "rpc_server/rpc_server.h"
    30 #include "rpc_server/rpc_ep_setup.h"
     33#include "rpc_server/rpc_ep_register.h"
     34#include "rpc_server/rpc_config.h"
    3135#include "rpc_server/spoolss/srv_spoolss_nt.h"
     36#include "librpc/rpc/dcerpc_ep.h"
     37#include "lib/server_prefork.h"
     38#include "lib/server_prefork_util.h"
    3239
    3340#define SPOOLSS_PIPE_NAME "spoolss"
    3441#define DAEMON_NAME "spoolssd"
    3542
    36 void start_spoolssd(struct tevent_context *ev_ctx,
    37                     struct messaging_context *msg_ctx);
    38 
    39 static void spoolss_reopen_logs(void)
    40 {
    41         char *lfile = lp_logfile();
     43static struct server_id parent_id;
     44static struct prefork_pool *spoolss_pool = NULL;
     45static int spoolss_child_id = 0;
     46
     47static struct pf_daemon_config default_pf_spoolss_cfg = {
     48        .prefork_status = PFH_INIT,
     49        .min_children = 5,
     50        .max_children = 25,
     51        .spawn_rate = 5,
     52        .max_allowed_clients = 100,
     53        .child_min_life = 60 /* 1 minute minimum life time */
     54};
     55static struct pf_daemon_config pf_spoolss_cfg = { 0 };
     56
     57pid_t start_spoolssd(struct tevent_context *ev_ctx,
     58                     struct messaging_context *msg_ctx);
     59
     60static void spoolss_reopen_logs(int child_id)
     61{
     62        char *lfile = lp_logfile(talloc_tos());
     63        char *ext;
    4264        int rc;
    4365
     66        if (child_id) {
     67                rc = asprintf(&ext, "%s.%d", DAEMON_NAME, child_id);
     68        } else {
     69                rc = asprintf(&ext, "%s", DAEMON_NAME);
     70        }
     71
     72        if (rc == -1) {
     73                return;
     74        }
     75
     76        rc = 0;
    4477        if (lfile == NULL || lfile[0] == '\0') {
    45                 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
    46                 if (rc > 0) {
    47                         lp_set_logfile(lfile);
    48                         SAFE_FREE(lfile);
    49                 }
     78                rc = asprintf(&lfile, "%s/log.%s",
     79                              get_dyn_LOGFILEBASE(), ext);
    5080        } else {
    51                 if (strstr(lfile, DAEMON_NAME) == NULL) {
    52                         rc = asprintf(&lfile, "%s.%s", lp_logfile(), DAEMON_NAME);
    53                         if (rc > 0) {
    54                                 lp_set_logfile(lfile);
    55                                 SAFE_FREE(lfile);
     81                if (strstr(lfile, ext) == NULL) {
     82                        if (child_id) {
     83                                rc = asprintf(&lfile, "%s.%d",
     84                                              lp_logfile(talloc_tos()),
     85                                              child_id);
     86                        } else {
     87                                rc = asprintf(&lfile, "%s.%s",
     88                                              lp_logfile(talloc_tos()),
     89                                              ext);
    5690                        }
    5791                }
    5892        }
    5993
     94        if (rc > 0) {
     95                lp_set_logfile(lfile);
     96                SAFE_FREE(lfile);
     97        }
     98
     99        SAFE_FREE(ext);
     100
    60101        reopen_logs();
     102}
     103
     104static void update_conf(struct tevent_context *ev,
     105                        struct messaging_context *msg)
     106{
     107        change_to_root_user();
     108        lp_load_global(get_dyn_CONFIGFILE());
     109        load_printers(ev, msg);
     110
     111        spoolss_reopen_logs(spoolss_child_id);
     112        if (spoolss_child_id == 0) {
     113                pfh_daemon_config(DAEMON_NAME,
     114                                  &pf_spoolss_cfg,
     115                                  &default_pf_spoolss_cfg);
     116                pfh_manage_pool(ev, msg, &pf_spoolss_cfg, spoolss_pool);
     117        }
    61118}
    62119
     
    71128
    72129        DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
    73         change_to_root_user();
    74         spoolss_reopen_logs();
    75 }
    76 
    77 static void spoolss_pcap_updated(struct messaging_context *msg,
    78                                  void *private_data,
    79                                  uint32_t msg_type,
    80                                  struct server_id server_id,
    81                                  DATA_BLOB *data)
    82 {
    83         struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
    84                                                              struct tevent_context);
    85 
    86         DEBUG(10, ("Got message saying pcap was updated. Reloading.\n"));
    87         change_to_root_user();
    88         reload_printers(ev_ctx, msg);
     130        update_conf(ev_ctx, msg);
    89131}
    90132
     
    118160                                    int count,
    119161                                    void *siginfo,
    120                                     void *private_data)
    121 {
    122         struct messaging_context *msg_ctx = talloc_get_type_abort(private_data,
    123                                                                   struct messaging_context);
    124 
    125         change_to_root_user();
     162                                    void *pvt)
     163{
     164        struct messaging_context *msg_ctx;
     165
     166        msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
     167
    126168        DEBUG(1,("Reloading printers after SIGHUP\n"));
    127         spoolss_reopen_logs();
     169        update_conf(ev, msg_ctx);
     170
     171        /* relay to all children */
     172        if (spoolss_pool) {
     173                prefork_send_signal_to_all(spoolss_pool, SIGHUP);
     174        }
    128175}
    129176
     
    158205}
    159206
    160 void start_spoolssd(struct tevent_context *ev_ctx,
    161                     struct messaging_context *msg_ctx)
    162 {
     207/* Children */
     208
     209static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
     210                                         struct tevent_signal *se,
     211                                         int signum,
     212                                         int count,
     213                                         void *siginfo,
     214                                         void *pvt)
     215{
     216        struct messaging_context *msg_ctx;
     217
     218        msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
     219
     220        change_to_root_user();
     221        DEBUG(1,("Reloading printers after SIGHUP\n"));
     222        load_printers(ev, msg_ctx);
     223        spoolss_reopen_logs(spoolss_child_id);
     224}
     225
     226static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
     227                                           struct messaging_context *msg_ctx,
     228                                           struct pf_worker_data *pf)
     229{
     230        struct tevent_signal *se;
     231
     232        se = tevent_add_signal(ev_ctx,
     233                               ev_ctx,
     234                               SIGHUP, 0,
     235                               spoolss_chld_sig_hup_handler,
     236                               msg_ctx);
     237        if (!se) {
     238                DEBUG(1, ("failed to setup SIGHUP handler"));
     239                return false;
     240        }
     241
     242        return true;
     243}
     244
     245static void parent_ping(struct messaging_context *msg_ctx,
     246                        void *private_data,
     247                        uint32_t msg_type,
     248                        struct server_id server_id,
     249                        DATA_BLOB *data)
     250{
     251
     252        /* The fact we received this message is enough to let make the event
     253         * loop if it was idle. spoolss_children_main will cycle through
     254         * spoolss_next_client at least once. That function will take whatever
     255         * action is necessary */
     256
     257        DEBUG(10, ("Got message that the parent changed status.\n"));
     258        return;
     259}
     260
     261static bool spoolss_child_init(struct tevent_context *ev_ctx,
     262                               int child_id, struct pf_worker_data *pf)
     263{
     264        NTSTATUS status;
    163265        struct rpc_srv_callbacks spoolss_cb;
    164         pid_t pid;
    165         NTSTATUS status;
    166         int ret;
    167 
    168         DEBUG(1, ("Forking SPOOLSS Daemon\n"));
    169 
    170         pid = sys_fork();
    171 
    172         if (pid == -1) {
    173                 DEBUG(0, ("Failed to fork SPOOLSS [%s], aborting ...\n",
    174                            strerror(errno)));
    175                 exit(1);
    176         }
    177 
    178         if (pid) {
    179                 /* parent */
    180                 return;
    181         }
    182 
    183         /* child */
    184         close_low_fds(false);
    185 
    186         status = reinit_after_fork(msg_ctx,
    187                                    ev_ctx,
    188                                    procid_self(), true);
     266        struct messaging_context *msg_ctx = server_messaging_context();
     267        bool ok;
     268
     269        status = reinit_after_fork(msg_ctx, ev_ctx, true, "spoolssd-child");
    189270        if (!NT_STATUS_IS_OK(status)) {
    190271                DEBUG(0,("reinit_after_fork() failed\n"));
     
    192273        }
    193274
    194         spoolss_reopen_logs();
    195 
    196         spoolss_setup_sig_term_handler(ev_ctx);
    197         spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
    198 
    199         if (!serverid_register(procid_self(),
    200                                 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
    201                                 |FLAG_MSG_PRINT_GENERAL)) {
    202                 exit(1);
     275        spoolss_child_id = child_id;
     276        spoolss_reopen_logs(child_id);
     277
     278        ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
     279        if (!ok) {
     280                return false;
     281        }
     282
     283        if (!serverid_register(messaging_server_id(msg_ctx),
     284                                FLAG_MSG_GENERAL |
     285                                FLAG_MSG_PRINT_GENERAL)) {
     286                return false;
    203287        }
    204288
    205289        if (!locking_init()) {
    206                 exit(1);
    207         }
    208 
    209         messaging_register(msg_ctx, NULL,
    210                            MSG_PRINTER_UPDATE, print_queue_receive);
     290                return false;
     291        }
     292
    211293        messaging_register(msg_ctx, ev_ctx,
    212294                           MSG_SMB_CONF_UPDATED, smb_conf_updated);
    213295        messaging_register(msg_ctx, ev_ctx,
    214                            MSG_PRINTER_PCAP, spoolss_pcap_updated);
     296                           MSG_PREFORK_PARENT_EVENT, parent_ping);
     297
     298        /* As soon as messaging is up check if pcap has been loaded already.
     299         * If so then we probably missed a message and should load_printers()
     300         * ourselves. If pcap has not been loaded yet, then ignore, we will get
     301         * a message as soon as the bq process completes the reload. */
     302        if (pcap_cache_loaded(NULL)) {
     303                load_printers(ev_ctx, msg_ctx);
     304        }
     305
     306        /* try to reinit rpc queues */
     307        spoolss_cb.init = spoolss_init_cb;
     308        spoolss_cb.shutdown = spoolss_shutdown_cb;
     309        spoolss_cb.private_data = msg_ctx;
     310
     311        status = rpc_winreg_init(NULL);
     312        if (!NT_STATUS_IS_OK(status)) {
     313                DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
     314                          nt_errstr(status)));
     315                return false;
     316        }
     317
     318        status = rpc_spoolss_init(&spoolss_cb);
     319        if (!NT_STATUS_IS_OK(status)) {
     320                DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
     321                          nt_errstr(status)));
     322                return false;
     323        }
     324
     325        return true;
     326}
     327
     328struct spoolss_children_data {
     329        struct tevent_context *ev_ctx;
     330        struct messaging_context *msg_ctx;
     331        struct pf_worker_data *pf;
     332        int listen_fd_size;
     333        int *listen_fds;
     334};
     335
     336static void spoolss_next_client(void *pvt);
     337
     338static int spoolss_children_main(struct tevent_context *ev_ctx,
     339                                 struct messaging_context *msg_ctx,
     340                                 struct pf_worker_data *pf,
     341                                 int child_id,
     342                                 int listen_fd_size,
     343                                 int *listen_fds,
     344                                 void *private_data)
     345{
     346        struct spoolss_children_data *data;
     347        bool ok;
     348        int ret = 0;
     349
     350        ok = spoolss_child_init(ev_ctx, child_id, pf);
     351        if (!ok) {
     352                return 1;
     353        }
     354
     355        data = talloc(ev_ctx, struct spoolss_children_data);
     356        if (!data) {
     357                return 1;
     358        }
     359        data->pf = pf;
     360        data->ev_ctx = ev_ctx;
     361        data->msg_ctx = msg_ctx;
     362        data->listen_fd_size = listen_fd_size;
     363        data->listen_fds = listen_fds;
     364
     365        /* loop until it is time to exit */
     366        while (pf->status != PF_WORKER_EXITING) {
     367                /* try to see if it is time to schedule the next client */
     368                spoolss_next_client(data);
     369
     370                ret = tevent_loop_once(ev_ctx);
     371                if (ret != 0) {
     372                        DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
     373                                  ret, strerror(errno)));
     374                        pf->status = PF_WORKER_EXITING;
     375                }
     376        }
     377
     378        return ret;
     379}
     380
     381static void spoolss_client_terminated(void *pvt)
     382{
     383        struct spoolss_children_data *data;
     384
     385        data = talloc_get_type_abort(pvt, struct spoolss_children_data);
     386
     387        pfh_client_terminated(data->pf);
     388
     389        spoolss_next_client(pvt);
     390}
     391
     392struct spoolss_new_client {
     393        struct spoolss_children_data *data;
     394        struct tsocket_address *srv_addr;
     395        struct tsocket_address *cli_addr;
     396};
     397
     398static void spoolss_handle_client(struct tevent_req *req);
     399
     400static void spoolss_next_client(void *pvt)
     401{
     402        struct tevent_req *req;
     403        struct spoolss_children_data *data;
     404        struct spoolss_new_client *next;
     405
     406        data = talloc_get_type_abort(pvt, struct spoolss_children_data);
     407
     408        if (!pfh_child_allowed_to_accept(data->pf)) {
     409                /* nothing to do for now we are already listening
     410                 * or we are not allowed to listen further */
     411                return;
     412        }
     413
     414        next = talloc_zero(data, struct spoolss_new_client);
     415        if (!next) {
     416                DEBUG(1, ("Out of memory!?\n"));
     417                return;
     418        }
     419        next->data = data;
     420
     421        req = prefork_listen_send(next, data->ev_ctx, data->pf,
     422                                  data->listen_fd_size,
     423                                  data->listen_fds);
     424        if (!req) {
     425                DEBUG(1, ("Failed to make listening request!?\n"));
     426                talloc_free(next);
     427                return;
     428        }
     429        tevent_req_set_callback(req, spoolss_handle_client, next);
     430}
     431
     432static void spoolss_handle_client(struct tevent_req *req)
     433{
     434        struct spoolss_children_data *data;
     435        struct spoolss_new_client *client;
     436        const DATA_BLOB ping = data_blob_null;
     437        int ret;
     438        int sd;
     439
     440        client = tevent_req_callback_data(req, struct spoolss_new_client);
     441        data = client->data;
     442
     443        ret = prefork_listen_recv(req, client, &sd,
     444                                  &client->srv_addr, &client->cli_addr);
     445
     446        /* this will free the request too */
     447        talloc_free(client);
     448
     449        if (ret != 0) {
     450                DEBUG(6, ("No client connection was available after all!\n"));
     451                return;
     452        }
     453
     454        /* Warn parent that our status changed */
     455        messaging_send(data->msg_ctx, parent_id,
     456                        MSG_PREFORK_CHILD_EVENT, &ping);
     457
     458        DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
     459                  (int)(data->pf->pid)));
     460
     461        named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
     462                                   SPOOLSS_PIPE_NAME, sd,
     463                                   spoolss_client_terminated, data);
     464}
     465
     466/* ==== Main Process Functions ==== */
     467
     468extern pid_t background_lpq_updater_pid;
     469static char *bq_logfile;
     470
     471static void check_updater_child(struct tevent_context *ev_ctx,
     472                                struct messaging_context *msg_ctx)
     473{
     474        int status;
     475        pid_t pid;
     476
     477        if (background_lpq_updater_pid == -1) {
     478                return;
     479        }
     480
     481        pid = sys_waitpid(background_lpq_updater_pid, &status, WNOHANG);
     482        if (pid > 0) {
     483                DEBUG(2, ("The background queue child died... Restarting!\n"));
     484                pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
     485                background_lpq_updater_pid = pid;
     486        }
     487}
     488
     489static void child_ping(struct messaging_context *msg_ctx,
     490                        void *private_data,
     491                        uint32_t msg_type,
     492                        struct server_id server_id,
     493                        DATA_BLOB *data)
     494{
     495        struct tevent_context *ev_ctx;
     496
     497        ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
     498
     499        DEBUG(10, ("Got message that a child changed status.\n"));
     500        pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
     501}
     502
     503static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
     504                                    struct messaging_context *msg_ctx,
     505                                    struct timeval current_time);
     506static void spoolssd_check_children(struct tevent_context *ev_ctx,
     507                                    struct tevent_timer *te,
     508                                    struct timeval current_time,
     509                                    void *pvt);
     510
     511static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
     512                                     struct prefork_pool *pfp,
     513                                     void *pvt)
     514{
     515        struct messaging_context *msg_ctx;
     516
     517        msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
     518
     519        /* run pool management so we can fork/retire or increase
     520         * the allowed connections per child based on load */
     521        pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
     522
     523        /* also check if the updater child is alive and well */
     524        check_updater_child(ev_ctx, msg_ctx);
     525}
     526
     527static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
     528                                            struct messaging_context *msg_ctx)
     529{
     530        bool ok;
     531
     532        /* add our oun sigchld callback */
     533        prefork_set_sigchld_callback(spoolss_pool,
     534                                     spoolssd_sigchld_handler, msg_ctx);
     535
     536        ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
     537                                     tevent_timeval_current());
     538        return ok;
     539}
     540
     541static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
     542                                    struct messaging_context *msg_ctx,
     543                                    struct timeval current_time)
     544{
     545        struct tevent_timer *te;
     546        struct timeval next_event;
     547
     548        /* check situation again in 10 seconds */
     549        next_event = tevent_timeval_current_ofs(10, 0);
     550
     551        /* TODO: check when the socket becomes readable, so that children
     552         * are checked only when there is some activity ? */
     553        te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
     554                                spoolssd_check_children, msg_ctx);
     555        if (!te) {
     556                DEBUG(2, ("Failed to set up children monitoring!\n"));
     557                return false;
     558        }
     559
     560        return true;
     561}
     562
     563static void spoolssd_check_children(struct tevent_context *ev_ctx,
     564                                    struct tevent_timer *te,
     565                                    struct timeval current_time,
     566                                    void *pvt)
     567{
     568        struct messaging_context *msg_ctx;
     569
     570        msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
     571
     572        pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
     573
     574        spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
     575}
     576
     577static void print_queue_forward(struct messaging_context *msg,
     578                                void *private_data,
     579                                uint32_t msg_type,
     580                                struct server_id server_id,
     581                                DATA_BLOB *data)
     582{
     583        messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
     584                           MSG_PRINTER_UPDATE, data->data, data->length);
     585}
     586
     587static char *get_bq_logfile(void)
     588{
     589        char *lfile = lp_logfile(talloc_tos());
     590        int rc;
     591
     592        if (lfile == NULL || lfile[0] == '\0') {
     593                rc = asprintf(&lfile, "%s/log.%s.bq",
     594                                        get_dyn_LOGFILEBASE(), DAEMON_NAME);
     595        } else {
     596                rc = asprintf(&lfile, "%s.bq", lp_logfile(talloc_tos()));
     597        }
     598        if (rc == -1) {
     599                lfile = NULL;
     600        }
     601        return lfile;
     602}
     603
     604pid_t start_spoolssd(struct tevent_context *ev_ctx,
     605                    struct messaging_context *msg_ctx)
     606{
     607        enum rpc_service_mode_e epm_mode = rpc_epmapper_mode();
     608        struct rpc_srv_callbacks spoolss_cb;
     609        struct dcerpc_binding_vector *v;
     610        TALLOC_CTX *mem_ctx;
     611        pid_t pid;
     612        NTSTATUS status;
     613        int listen_fd;
     614        int ret;
     615        bool ok;
     616
     617        DEBUG(1, ("Forking SPOOLSS Daemon\n"));
     618
     619        /*
     620         * Block signals before forking child as it will have to
     621         * set its own handlers. Child will re-enable SIGHUP as
     622         * soon as the handlers are set up.
     623         */
     624        BlockSignals(true, SIGTERM);
     625        BlockSignals(true, SIGHUP);
     626
     627        pid = fork();
     628
     629        if (pid == -1) {
     630                DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
     631                           strerror(errno)));
     632        }
     633
     634        /* parent or error */
     635        if (pid != 0) {
     636
     637                /* Re-enable SIGHUP before returnig */
     638                BlockSignals(false, SIGTERM);
     639                BlockSignals(false, SIGHUP);
     640                return pid;
     641        }
     642
     643        status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true,
     644                                        "spoolssd-master");
     645        if (!NT_STATUS_IS_OK(status)) {
     646                DEBUG(0,("reinit_after_fork() failed\n"));
     647                smb_panic("reinit_after_fork() failed");
     648        }
     649
     650        /* save the parent process id so the children can use it later */
     651        parent_id = messaging_server_id(msg_ctx);
     652
     653        spoolss_reopen_logs(0);
     654        pfh_daemon_config(DAEMON_NAME,
     655                          &pf_spoolss_cfg,
     656                          &default_pf_spoolss_cfg);
     657
     658        spoolss_setup_sig_term_handler(ev_ctx);
     659        spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
     660
     661        BlockSignals(false, SIGTERM);
     662        BlockSignals(false, SIGHUP);
     663
     664        /* always start the backgroundqueue listner in spoolssd */
     665        bq_logfile = get_bq_logfile();
     666        pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
     667        if (pid > 0) {
     668                background_lpq_updater_pid = pid;
     669        }
     670
     671        /* the listening fd must be created before the children are actually
     672         * forked out. */
     673        listen_fd = create_named_pipe_socket(SPOOLSS_PIPE_NAME);
     674        if (listen_fd == -1) {
     675                exit(1);
     676        }
     677
     678        ret = listen(listen_fd, pf_spoolss_cfg.max_allowed_clients);
     679        if (ret == -1) {
     680                DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
     681                          strerror(errno)));
     682                exit(1);
     683        }
     684
     685        /* start children before any more initialization is done */
     686        ok = prefork_create_pool(ev_ctx, /* mem_ctx */
     687                                 ev_ctx, msg_ctx,
     688                                 1, &listen_fd,
     689                                 pf_spoolss_cfg.min_children,
     690                                 pf_spoolss_cfg.max_children,
     691                                 &spoolss_children_main, NULL,
     692                                 &spoolss_pool);
     693        if (!ok) {
     694                exit(1);
     695        }
     696
     697        if (!serverid_register(messaging_server_id(msg_ctx),
     698                                FLAG_MSG_GENERAL |
     699                                FLAG_MSG_PRINT_GENERAL)) {
     700                exit(1);
     701        }
     702
     703        if (!locking_init()) {
     704                exit(1);
     705        }
     706
     707        messaging_register(msg_ctx, ev_ctx,
     708                           MSG_SMB_CONF_UPDATED, smb_conf_updated);
     709        messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
     710                           print_queue_forward);
     711        messaging_register(msg_ctx, ev_ctx,
     712                           MSG_PREFORK_CHILD_EVENT, child_ping);
     713
     714        /*
     715         * As soon as messaging is up check if pcap has been loaded already.
     716         * If pcap has not been loaded yet, then ignore, as we will reload on
     717         * client enumeration anyway.
     718         */
     719        if (pcap_cache_loaded(NULL)) {
     720                load_printers(ev_ctx, msg_ctx);
     721        }
     722
     723        mem_ctx = talloc_new(NULL);
     724        if (mem_ctx == NULL) {
     725                exit(1);
     726        }
    215727
    216728        /*
     
    225737        status = rpc_winreg_init(NULL);
    226738        if (!NT_STATUS_IS_OK(status)) {
    227                 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
     739                DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
    228740                          nt_errstr(status)));
    229741                exit(1);
     
    232744        status = rpc_spoolss_init(&spoolss_cb);
    233745        if (!NT_STATUS_IS_OK(status)) {
    234                 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
     746                DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
    235747                          nt_errstr(status)));
    236748                exit(1);
    237749        }
    238750
    239         if (!setup_named_pipe_socket(SPOOLSS_PIPE_NAME, ev_ctx)) {
    240                 exit(1);
    241         }
    242 
    243         status = rpc_ep_setup_register(ev_ctx, msg_ctx, &ndr_table_spoolss, NULL, 0);
    244         if (!NT_STATUS_IS_OK(status)) {
    245                 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
    246                           nt_errstr(status)));
    247                 exit(1);
    248         }
    249 
    250         DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
     751        if (epm_mode != RPC_SERVICE_MODE_DISABLED &&
     752            (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
     753                status = dcerpc_binding_vector_new(mem_ctx, &v);
     754                if (!NT_STATUS_IS_OK(status)) {
     755                        DEBUG(0, ("Failed to create binding vector (%s)\n",
     756                                  nt_errstr(status)));
     757                        exit(1);
     758                }
     759
     760                status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
     761                if (!NT_STATUS_IS_OK(status)) {
     762                        DEBUG(0, ("Failed to add np to binding vector (%s)\n",
     763                                  nt_errstr(status)));
     764                        exit(1);
     765                }
     766
     767                status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
     768                if (!NT_STATUS_IS_OK(status)) {
     769                        DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
     770                                  nt_errstr(status)));
     771                        exit(1);
     772                }
     773        }
     774
     775        talloc_free(mem_ctx);
     776
     777        ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
     778        if (!ok) {
     779                DEBUG(0, ("Failed to setup children monitoring!\n"));
     780                exit(1);
     781        }
     782
     783        DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
     784
     785        pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
    251786
    252787        /* loop forever */
     
    254789
    255790        /* should not be reached */
    256         DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
     791        DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
    257792                 ret, (ret == 0) ? "out of events" : strerror(errno)));
    258793        exit(1);
Note: See TracChangeset for help on using the changeset viewer.