root/source4/smbd/process_standard.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. none_setproctitle
  2. standard_model_init
  3. standard_accept_connection
  4. standard_new_task
  5. standard_terminate
  6. standard_set_title
  7. process_model_standard_init

   1 /* 
   2    Unix SMB/CIFS implementation.
   3 
   4    process model: standard (1 process per client connection)
   5 
   6    Copyright (C) Andrew Tridgell 1992-2005
   7    Copyright (C) James J Myers 2003 <myersjj@samba.org>
   8    Copyright (C) Stefan (metze) Metzmacher 2004
   9    
  10    This program is free software; you can redistribute it and/or modify
  11    it under the terms of the GNU General Public License as published by
  12    the Free Software Foundation; either version 3 of the License, or
  13    (at your option) any later version.
  14    
  15    This program is distributed in the hope that it will be useful,
  16    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18    GNU General Public License for more details.
  19    
  20    You should have received a copy of the GNU General Public License
  21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  22 */
  23 
  24 #include "includes.h"
  25 #include "lib/events/events.h"
  26 #include "../tdb/include/tdb.h"
  27 #include "lib/socket/socket.h"
  28 #include "smbd/process_model.h"
  29 #include "param/secrets.h"
  30 #include "system/filesys.h"
  31 #include "cluster/cluster.h"
  32 #include "param/param.h"
  33 
  34 #ifdef HAVE_SETPROCTITLE
  35 #ifdef HAVE_SETPROCTITLE_H
  36 #include <setproctitle.h>
  37 #endif
  38 #else
  39 #define setproctitle none_setproctitle
  40 static int none_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
     /* [<][>][^][v][top][bottom][index][help] */
  41 static int none_setproctitle(const char *fmt, ...)
  42 {
  43         return 0;
  44 }
  45 #endif
  46 
  47 /*
  48   called when the process model is selected
  49 */
  50 static void standard_model_init(struct tevent_context *ev)
     /* [<][>][^][v][top][bottom][index][help] */
  51 {
  52         signal(SIGCHLD, SIG_IGN);
  53 }
  54 
  55 /*
  56   called when a listening socket becomes readable. 
  57 */
  58 static void standard_accept_connection(struct tevent_context *ev, 
     /* [<][>][^][v][top][bottom][index][help] */
  59                                        struct loadparm_context *lp_ctx,
  60                                        struct socket_context *sock, 
  61                                        void (*new_conn)(struct tevent_context *,
  62                                                         struct loadparm_context *, struct socket_context *, 
  63                                                         struct server_id , void *), 
  64                                        void *private_data)
  65 {
  66         NTSTATUS status;
  67         struct socket_context *sock2;
  68         pid_t pid;
  69         struct tevent_context *ev2;
  70         struct socket_address *c, *s;
  71 
  72         /* accept an incoming connection. */
  73         status = socket_accept(sock, &sock2);
  74         if (!NT_STATUS_IS_OK(status)) {
  75                 DEBUG(0,("standard_accept_connection: accept: %s\n",
  76                          nt_errstr(status)));
  77                 /* this looks strange, but is correct. We need to throttle things until
  78                    the system clears enough resources to handle this new socket */
  79                 sleep(1);
  80                 return;
  81         }
  82 
  83         pid = fork();
  84 
  85         if (pid != 0) {
  86                 /* parent or error code ... */
  87                 talloc_free(sock2);
  88                 /* go back to the event loop */
  89                 return;
  90         }
  91 
  92         pid = getpid();
  93 
  94         /* This is now the child code. We need a completely new event_context to work with */
  95         ev2 = s4_event_context_init(NULL);
  96 
  97         /* the service has given us a private pointer that
  98            encapsulates the context it needs for this new connection -
  99            everything else will be freed */
 100         talloc_steal(ev2, private_data);
 101         talloc_steal(private_data, sock2);
 102 
 103         /* this will free all the listening sockets and all state that
 104            is not associated with this new connection */
 105         talloc_free(sock);
 106         talloc_free(ev);
 107 
 108         /* we don't care if the dup fails, as its only a select()
 109            speed optimisation */
 110         socket_dup(sock2);
 111                         
 112         /* tdb needs special fork handling */
 113         if (tdb_reopen_all(1) == -1) {
 114                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
 115         }
 116 
 117         /* Ensure that the forked children do not expose identical random streams */
 118         set_need_random_reseed();
 119 
 120         /* setup the process title */
 121         c = socket_get_peer_addr(sock2, ev2);
 122         s = socket_get_my_addr(sock2, ev2);
 123         if (s && c) {
 124                 setproctitle("conn c[%s:%u] s[%s:%u] server_id[%d]",
 125                              c->addr, c->port, s->addr, s->port, pid);
 126         }
 127         talloc_free(c);
 128         talloc_free(s);
 129 
 130         /* setup this new connection.  Cluster ID is PID based for this process modal */
 131         new_conn(ev2, lp_ctx, sock2, cluster_id(pid, 0), private_data);
 132 
 133         /* we can't return to the top level here, as that event context is gone,
 134            so we now process events in the new event context until there are no
 135            more to process */      
 136         event_loop_wait(ev2);
 137 
 138         talloc_free(ev2);
 139         exit(0);
 140 }
 141 
 142 /*
 143   called to create a new server task
 144 */
 145 static void standard_new_task(struct tevent_context *ev, 
     /* [<][>][^][v][top][bottom][index][help] */
 146                               struct loadparm_context *lp_ctx,
 147                               const char *service_name,
 148                               void (*new_task)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *), 
 149                               void *private_data)
 150 {
 151         pid_t pid;
 152         struct tevent_context *ev2;
 153 
 154         pid = fork();
 155 
 156         if (pid != 0) {
 157                 /* parent or error code ... go back to the event loop */
 158                 return;
 159         }
 160 
 161         pid = getpid();
 162 
 163         /* This is now the child code. We need a completely new event_context to work with */
 164         ev2 = s4_event_context_init(NULL);
 165 
 166         /* the service has given us a private pointer that
 167            encapsulates the context it needs for this new connection -
 168            everything else will be freed */
 169         talloc_steal(ev2, private_data);
 170 
 171         /* this will free all the listening sockets and all state that
 172            is not associated with this new connection */
 173         talloc_free(ev);
 174 
 175         /* tdb needs special fork handling */
 176         if (tdb_reopen_all(1) == -1) {
 177                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
 178         }
 179 
 180         /* Ensure that the forked children do not expose identical random streams */
 181         set_need_random_reseed();
 182 
 183         setproctitle("task %s server_id[%d]", service_name, pid);
 184 
 185         /* setup this new task.  Cluster ID is PID based for this process modal */
 186         new_task(ev2, lp_ctx, cluster_id(pid, 0), private_data);
 187 
 188         /* we can't return to the top level here, as that event context is gone,
 189            so we now process events in the new event context until there are no
 190            more to process */      
 191         event_loop_wait(ev2);
 192 
 193         talloc_free(ev2);
 194         exit(0);
 195 }
 196 
 197 
 198 /* called when a task goes down */
 199 _NORETURN_ static void standard_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx, 
     /* [<][>][^][v][top][bottom][index][help] */
 200                                           const char *reason) 
 201 {
 202         DEBUG(2,("standard_terminate: reason[%s]\n",reason));
 203 
 204         /* this reload_charcnv() has the effect of freeing the iconv context memory,
 205            which makes leak checking easier */
 206         reload_charcnv(lp_ctx);
 207 
 208         talloc_free(ev);
 209 
 210         /* terminate this process */
 211         exit(0);
 212 }
 213 
 214 /* called to set a title of a task or connection */
 215 static void standard_set_title(struct tevent_context *ev, const char *title) 
     /* [<][>][^][v][top][bottom][index][help] */
 216 {
 217         if (title) {
 218                 setproctitle("%s", title);
 219         } else {
 220                 setproctitle(NULL);
 221         }
 222 }
 223 
 224 static const struct model_ops standard_ops = {
 225         .name                   = "standard",
 226         .model_init             = standard_model_init,
 227         .accept_connection      = standard_accept_connection,
 228         .new_task               = standard_new_task,
 229         .terminate              = standard_terminate,
 230         .set_title              = standard_set_title,
 231 };
 232 
 233 /*
 234   initialise the standard process model, registering ourselves with the process model subsystem
 235  */
 236 NTSTATUS process_model_standard_init(void)
     /* [<][>][^][v][top][bottom][index][help] */
 237 {
 238         return register_process_model(&standard_ops);
 239 }

/* [<][>][^][v][top][bottom][index][help] */