smbd/server.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    Main SMB server routines
00004    Copyright (C) Andrew Tridgell                1992-1998
00005    Copyright (C) Martin Pool                    2002
00006    Copyright (C) Jelmer Vernooij                2002-2003
00007    
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012    
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017    
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 
00023 #include "includes.h"
00024 
00025 static_decl_rpc;
00026 
00027 static int am_parent = 1;
00028 
00029 /* the last message the was processed */
00030 int last_message = -1;
00031 
00032 /* a useful macro to debug the last message processed */
00033 #define LAST_MESSAGE() (last_message != -1 ? smb_fn_name(last_message) : "")
00034 
00035 extern struct auth_context *negprot_global_auth_context;
00036 extern pstring user_socket_options;
00037 extern SIG_ATOMIC_T got_sig_term;
00038 extern SIG_ATOMIC_T reload_after_sighup;
00039 static SIG_ATOMIC_T got_sig_cld;
00040 
00041 #ifdef WITH_DFS
00042 extern int dcelogin_atmost_once;
00043 #endif /* WITH_DFS */
00044 
00045 /* really we should have a top level context structure that has the
00046    client file descriptor as an element. That would require a major rewrite :(
00047 
00048    the following 2 functions are an alternative - they make the file
00049    descriptor private to smbd
00050  */
00051 static int server_fd = -1;
00052 
00053 int smbd_server_fd(void)
00054 {
00055         return server_fd;
00056 }
00057 
00058 static void smbd_set_server_fd(int fd)
00059 {
00060         server_fd = fd;
00061         client_setfd(fd);
00062 }
00063 
00064 struct event_context *smbd_event_context(void)
00065 {
00066         static struct event_context *ctx;
00067 
00068         if (!ctx && !(ctx = event_context_init(NULL))) {
00069                 smb_panic("Could not init smbd event context\n");
00070         }
00071         return ctx;
00072 }
00073 
00074 struct messaging_context *smbd_messaging_context(void)
00075 {
00076         static struct messaging_context *ctx;
00077 
00078         if (!ctx && !(ctx = messaging_init(NULL, server_id_self(),
00079                                            smbd_event_context()))) {
00080                 smb_panic("Could not init smbd messaging context\n");
00081         }
00082         return ctx;
00083 }
00084 
00085 /*******************************************************************
00086  What to do when smb.conf is updated.
00087  ********************************************************************/
00088 
00089 static void smb_conf_updated(int msg_type, struct process_id src,
00090                              void *buf, size_t len, void *private_data)
00091 {
00092         DEBUG(10,("smb_conf_updated: Got message saying smb.conf was updated. Reloading.\n"));
00093         reload_services(False);
00094 }
00095 
00096 
00097 /*******************************************************************
00098  Delete a statcache entry.
00099  ********************************************************************/
00100 
00101 static void smb_stat_cache_delete(int msg_type, struct process_id src,
00102                                   void *buf, size_t len, void *private_data)
00103 {
00104         const char *name = (const char *)buf;
00105         DEBUG(10,("smb_stat_cache_delete: delete name %s\n", name));
00106         stat_cache_delete(name);
00107 }
00108 
00109 /****************************************************************************
00110  Terminate signal.
00111 ****************************************************************************/
00112 
00113 static void sig_term(void)
00114 {
00115         got_sig_term = 1;
00116         sys_select_signal(SIGTERM);
00117 }
00118 
00119 /****************************************************************************
00120  Catch a sighup.
00121 ****************************************************************************/
00122 
00123 static void sig_hup(int sig)
00124 {
00125         reload_after_sighup = 1;
00126         sys_select_signal(SIGHUP);
00127 }
00128 
00129 /****************************************************************************
00130  Catch a sigcld
00131 ****************************************************************************/
00132 static void sig_cld(int sig)
00133 {
00134         got_sig_cld = 1;
00135         sys_select_signal(SIGCLD);
00136 }
00137 
00138 /****************************************************************************
00139   Send a SIGTERM to our process group.
00140 *****************************************************************************/
00141 
00142 static void  killkids(void)
00143 {
00144         if(am_parent) kill(0,SIGTERM);
00145 }
00146 
00147 /****************************************************************************
00148  Process a sam sync message - not sure whether to do this here or
00149  somewhere else.
00150 ****************************************************************************/
00151 
00152 static void msg_sam_sync(int UNUSED(msg_type), struct process_id UNUSED(pid),
00153                          void *UNUSED(buf), size_t UNUSED(len),
00154                          void *private_data)
00155 {
00156         DEBUG(10, ("** sam sync message received, ignoring\n"));
00157 }
00158 
00159 /****************************************************************************
00160  Process a sam sync replicate message - not sure whether to do this here or
00161  somewhere else.
00162 ****************************************************************************/
00163 
00164 static void msg_sam_repl(int msg_type, struct process_id pid,
00165                          void *buf, size_t len, void *private_data)
00166 {
00167         uint32 low_serial;
00168 
00169         if (len != sizeof(uint32))
00170                 return;
00171 
00172         low_serial = *((uint32 *)buf);
00173 
00174         DEBUG(3, ("received sam replication message, serial = 0x%04x\n",
00175                   low_serial));
00176 }
00177 
00178 /****************************************************************************
00179  Open the socket communication - inetd.
00180 ****************************************************************************/
00181 
00182 static BOOL open_sockets_inetd(void)
00183 {
00184         /* Started from inetd. fd 0 is the socket. */
00185         /* We will abort gracefully when the client or remote system 
00186            goes away */
00187         smbd_set_server_fd(dup(0));
00188         
00189         /* close our standard file descriptors */
00190         close_low_fds(False); /* Don't close stderr */
00191         
00192         set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
00193         set_socket_options(smbd_server_fd(), user_socket_options);
00194 
00195         return True;
00196 }
00197 
00198 static void msg_exit_server(int msg_type, struct process_id src,
00199                             void *buf, size_t len, void *private_data)
00200 {
00201         DEBUG(3, ("got a SHUTDOWN message\n"));
00202         exit_server_cleanly(NULL);
00203 }
00204 
00205 #ifdef DEVELOPER
00206 static void msg_inject_fault(int msg_type, struct process_id src,
00207                             void *buf, size_t len, void *private_data)
00208 {
00209         int sig;
00210 
00211         if (len != sizeof(int)) {
00212                 
00213                 DEBUG(0, ("Process %llu sent bogus signal injection request\n",
00214                         (unsigned long long)src.pid));
00215                 return;
00216         }
00217 
00218         sig = *(int *)buf;
00219         if (sig == -1) {
00220                 exit_server("internal error injected");
00221                 return;
00222         }
00223 
00224 #if HAVE_STRSIGNAL
00225         DEBUG(0, ("Process %llu requested injection of signal %d (%s)\n",
00226                     (unsigned long long)src.pid, sig, strsignal(sig)));
00227 #else
00228         DEBUG(0, ("Process %llu requested injection of signal %d\n",
00229                     (unsigned long long)src.pid, sig));
00230 #endif
00231 
00232         kill(sys_getpid(), sig);
00233 }
00234 #endif /* DEVELOPER */
00235 
00236 struct child_pid {
00237         struct child_pid *prev, *next;
00238         pid_t pid;
00239 };
00240 
00241 static struct child_pid *children;
00242 static int num_children;
00243 
00244 static void add_child_pid(pid_t pid)
00245 {
00246         struct child_pid *child;
00247 
00248         if (lp_max_smbd_processes() == 0) {
00249                 /* Don't bother with the child list if we don't care anyway */
00250                 return;
00251         }
00252 
00253         child = SMB_MALLOC_P(struct child_pid);
00254         if (child == NULL) {
00255                 DEBUG(0, ("Could not add child struct -- malloc failed\n"));
00256                 return;
00257         }
00258         child->pid = pid;
00259         DLIST_ADD(children, child);
00260         num_children += 1;
00261 }
00262 
00263 static void remove_child_pid(pid_t pid)
00264 {
00265         struct child_pid *child;
00266 
00267         if (lp_max_smbd_processes() == 0) {
00268                 /* Don't bother with the child list if we don't care anyway */
00269                 return;
00270         }
00271 
00272         for (child = children; child != NULL; child = child->next) {
00273                 if (child->pid == pid) {
00274                         struct child_pid *tmp = child;
00275                         DLIST_REMOVE(children, child);
00276                         SAFE_FREE(tmp);
00277                         num_children -= 1;
00278                         return;
00279                 }
00280         }
00281 
00282         DEBUG(0, ("Could not find child %d -- ignoring\n", (int)pid));
00283 }
00284 
00285 /****************************************************************************
00286  Have we reached the process limit ?
00287 ****************************************************************************/
00288 
00289 static BOOL allowable_number_of_smbd_processes(void)
00290 {
00291         int max_processes = lp_max_smbd_processes();
00292 
00293         if (!max_processes)
00294                 return True;
00295 
00296         return num_children < max_processes;
00297 }
00298 
00299 /****************************************************************************
00300  Open the socket communication.
00301 ****************************************************************************/
00302 
00303 static BOOL open_sockets_smbd(BOOL is_daemon, BOOL interactive, const char *smb_ports)
00304 {
00305         int num_interfaces = iface_count();
00306         int num_sockets = 0;
00307         int fd_listenset[FD_SETSIZE];
00308         fd_set listen_set;
00309         int s;
00310         int maxfd = 0;
00311         int i;
00312         char *ports;
00313 
00314         if (!is_daemon) {
00315                 return open_sockets_inetd();
00316         }
00317 
00318                 
00319 #ifdef HAVE_ATEXIT
00320         {
00321                 static int atexit_set;
00322                 if(atexit_set == 0) {
00323                         atexit_set=1;
00324                         atexit(killkids);
00325                 }
00326         }
00327 #endif
00328 
00329         /* Stop zombies */
00330         CatchSignal(SIGCLD, sig_cld);
00331                                 
00332         FD_ZERO(&listen_set);
00333 
00334         /* use a reasonable default set of ports - listing on 445 and 139 */
00335         if (!smb_ports) {
00336                 ports = lp_smb_ports();
00337                 if (!ports || !*ports) {
00338                         ports = smb_xstrdup(SMB_PORTS);
00339                 } else {
00340                         ports = smb_xstrdup(ports);
00341                 }
00342         } else {
00343                 ports = smb_xstrdup(smb_ports);
00344         }
00345 
00346         if (lp_interfaces() && lp_bind_interfaces_only()) {
00347                 /* We have been given an interfaces line, and been 
00348                    told to only bind to those interfaces. Create a
00349                    socket per interface and bind to only these.
00350                 */
00351                 
00352                 /* Now open a listen socket for each of the
00353                    interfaces. */
00354                 for(i = 0; i < num_interfaces; i++) {
00355                         struct in_addr *ifip = iface_n_ip(i);
00356                         fstring tok;
00357                         const char *ptr;
00358 
00359                         if(ifip == NULL) {
00360                                 DEBUG(0,("open_sockets_smbd: interface %d has NULL IP address !\n", i));
00361                                 continue;
00362                         }
00363 
00364                         for (ptr=ports; next_token(&ptr, tok, " \t,", sizeof(tok)); ) {
00365                                 unsigned port = atoi(tok);
00366                                 if (port == 0 || port > 0xffff) {
00367                                         continue;
00368                                 }
00369                                 s = fd_listenset[num_sockets] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
00370                                 if(s == -1)
00371                                         return False;
00372 
00373                                 /* ready to listen */
00374                                 set_socket_options(s,"SO_KEEPALIVE"); 
00375                                 set_socket_options(s,user_socket_options);
00376      
00377                                 /* Set server socket to non-blocking for the accept. */
00378                                 set_blocking(s,False); 
00379  
00380                                 if (listen(s, SMBD_LISTEN_BACKLOG) == -1) {
00381                                         DEBUG(0,("listen: %s\n",strerror(errno)));
00382                                         close(s);
00383                                         return False;
00384                                 }
00385                                 FD_SET(s,&listen_set);
00386                                 maxfd = MAX( maxfd, s);
00387 
00388                                 num_sockets++;
00389                                 if (num_sockets >= FD_SETSIZE) {
00390                                         DEBUG(0,("open_sockets_smbd: Too many sockets to bind to\n"));
00391                                         return False;
00392                                 }
00393                         }
00394                 }
00395         } else {
00396                 /* Just bind to 0.0.0.0 - accept connections
00397                    from anywhere. */
00398 
00399                 fstring tok;
00400                 const char *ptr;
00401 
00402                 num_interfaces = 1;
00403                 
00404                 for (ptr=ports; next_token(&ptr, tok, " \t,", sizeof(tok)); ) {
00405                         unsigned port = atoi(tok);
00406                         if (port == 0 || port > 0xffff) continue;
00407                         /* open an incoming socket */
00408                         s = open_socket_in(SOCK_STREAM, port, 0,
00409                                            interpret_addr(lp_socket_address()),True);
00410                         if (s == -1)
00411                                 return(False);
00412                 
00413                         /* ready to listen */
00414                         set_socket_options(s,"SO_KEEPALIVE"); 
00415                         set_socket_options(s,user_socket_options);
00416                         
00417                         /* Set server socket to non-blocking for the accept. */
00418                         set_blocking(s,False); 
00419  
00420                         if (listen(s, SMBD_LISTEN_BACKLOG) == -1) {
00421                                 DEBUG(0,("open_sockets_smbd: listen: %s\n",
00422                                          strerror(errno)));
00423                                 close(s);
00424                                 return False;
00425                         }
00426 
00427                         fd_listenset[num_sockets] = s;
00428                         FD_SET(s,&listen_set);
00429                         maxfd = MAX( maxfd, s);
00430 
00431                         num_sockets++;
00432 
00433                         if (num_sockets >= FD_SETSIZE) {
00434                                 DEBUG(0,("open_sockets_smbd: Too many sockets to bind to\n"));
00435                                 return False;
00436                         }
00437                 }
00438         } 
00439 
00440         SAFE_FREE(ports);
00441 
00442         /* Listen to messages */
00443 
00444         message_register(MSG_SMB_SAM_SYNC, msg_sam_sync, NULL);
00445         message_register(MSG_SMB_SAM_REPL, msg_sam_repl, NULL);
00446         message_register(MSG_SHUTDOWN, msg_exit_server, NULL);
00447         message_register(MSG_SMB_FILE_RENAME, msg_file_was_renamed, NULL);
00448         message_register(MSG_SMB_CONF_UPDATED, smb_conf_updated, NULL); 
00449         message_register(MSG_SMB_STAT_CACHE_DELETE, smb_stat_cache_delete,
00450                          NULL);
00451 
00452 #ifdef DEVELOPER
00453         message_register(MSG_SMB_INJECT_FAULT, msg_inject_fault, NULL); 
00454 #endif
00455 
00456         /* now accept incoming connections - forking a new process
00457            for each incoming connection */
00458         DEBUG(2,("waiting for a connection\n"));
00459         while (1) {
00460                 fd_set lfds;
00461                 int num;
00462                 
00463                 /* Free up temporary memory from the main smbd. */
00464                 lp_TALLOC_FREE();
00465 
00466                 /* Ensure we respond to PING and DEBUG messages from the main smbd. */
00467                 message_dispatch();
00468 
00469                 if (got_sig_cld) {
00470                         pid_t pid;
00471                         got_sig_cld = False;
00472 
00473                         while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
00474                                 remove_child_pid(pid);
00475                         }
00476                 }
00477 
00478                 memcpy((char *)&lfds, (char *)&listen_set, 
00479                        sizeof(listen_set));
00480                 
00481                 num = sys_select(maxfd+1,&lfds,NULL,NULL,NULL);
00482                 
00483                 if (num == -1 && errno == EINTR) {
00484                         if (got_sig_term) {
00485                                 exit_server_cleanly(NULL);
00486                         }
00487 
00488                         /* check for sighup processing */
00489                         if (reload_after_sighup) {
00490                                 change_to_root_user();
00491                                 DEBUG(1,("Reloading services after SIGHUP\n"));
00492                                 reload_services(False);
00493                                 reload_after_sighup = 0;
00494                         }
00495 
00496                         continue;
00497                 }
00498                 
00499                 /* check if we need to reload services */
00500                 check_reload(time(NULL));
00501 
00502                 /* Find the sockets that are read-ready -
00503                    accept on these. */
00504                 for( ; num > 0; num--) {
00505                         struct sockaddr addr;
00506                         socklen_t in_addrlen = sizeof(addr);
00507                         pid_t child = 0;
00508 
00509                         s = -1;
00510                         for(i = 0; i < num_sockets; i++) {
00511                                 if(FD_ISSET(fd_listenset[i],&lfds)) {
00512                                         s = fd_listenset[i];
00513                                         /* Clear this so we don't look
00514                                            at it again. */
00515                                         FD_CLR(fd_listenset[i],&lfds);
00516                                         break;
00517                                 }
00518                         }
00519 
00520                         smbd_set_server_fd(accept(s,&addr,&in_addrlen));
00521                         
00522                         if (smbd_server_fd() == -1 && errno == EINTR)
00523                                 continue;
00524                         
00525                         if (smbd_server_fd() == -1) {
00526                                 DEBUG(0,("open_sockets_smbd: accept: %s\n",
00527                                          strerror(errno)));
00528                                 continue;
00529                         }
00530 
00531                         /* Ensure child is set to blocking mode */
00532                         set_blocking(smbd_server_fd(),True);
00533 
00534                         if (smbd_server_fd() != -1 && interactive)
00535                                 return True;
00536                         
00537                         if (allowable_number_of_smbd_processes() &&
00538                             smbd_server_fd() != -1 &&
00539                             ((child = sys_fork())==0)) {
00540                                 /* Child code ... */
00541 
00542                                 /* Stop zombies, the parent explicitly handles
00543                                  * them, counting worker smbds. */
00544                                 CatchChild();
00545                                 
00546                                 /* close the listening socket(s) */
00547                                 for(i = 0; i < num_sockets; i++)
00548                                         close(fd_listenset[i]);
00549                                 
00550                                 /* close our standard file
00551                                    descriptors */
00552                                 close_low_fds(False);
00553                                 am_parent = 0;
00554                                 
00555                                 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
00556                                 set_socket_options(smbd_server_fd(),user_socket_options);
00557                                 
00558                                 /* this is needed so that we get decent entries
00559                                    in smbstatus for port 445 connects */
00560                                 set_remote_machine_name(get_peer_addr(smbd_server_fd()),
00561                                                         False);
00562                                 
00563                                 /* Reset the state of the random
00564                                  * number generation system, so
00565                                  * children do not get the same random
00566                                  * numbers as each other */
00567 
00568                                 set_need_random_reseed();
00569                                 /* tdb needs special fork handling - remove
00570                                  * CLEAR_IF_FIRST flags */
00571                                 if (tdb_reopen_all(1) == -1) {
00572                                         DEBUG(0,("tdb_reopen_all failed.\n"));
00573                                         smb_panic("tdb_reopen_all failed.");
00574                                 }
00575 
00576                                 return True; 
00577                         }
00578                         /* The parent doesn't need this socket */
00579                         close(smbd_server_fd()); 
00580 
00581                         /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
00582                                 Clear the closed fd info out of server_fd --
00583                                 and more importantly, out of client_fd in
00584                                 util_sock.c, to avoid a possible
00585                                 getpeername failure if we reopen the logs
00586                                 and use %I in the filename.
00587                         */
00588 
00589                         smbd_set_server_fd(-1);
00590 
00591                         if (child != 0) {
00592                                 add_child_pid(child);
00593                         }
00594 
00595                         /* Force parent to check log size after
00596                          * spawning child.  Fix from
00597                          * klausr@ITAP.Physik.Uni-Stuttgart.De.  The
00598                          * parent smbd will log to logserver.smb.  It
00599                          * writes only two messages for each child
00600                          * started/finished. But each child writes,
00601                          * say, 50 messages also in logserver.smb,
00602                          * begining with the debug_count of the
00603                          * parent, before the child opens its own log
00604                          * file logserver.client. In a worst case
00605                          * scenario the size of logserver.smb would be
00606                          * checked after about 50*50=2500 messages
00607                          * (ca. 100kb).
00608                          * */
00609                         force_check_log_size();
00610  
00611                 } /* end for num */
00612         } /* end while 1 */
00613 
00614 /* NOTREACHED   return True; */
00615 }
00616 
00617 /****************************************************************************
00618  Reload printers
00619 **************************************************************************/
00620 void reload_printers(void)
00621 {
00622         int snum;
00623         int n_services = lp_numservices();
00624         int pnum = lp_servicenumber(PRINTERS_NAME);
00625         const char *pname;
00626 
00627         pcap_cache_reload();
00628 
00629         /* remove stale printers */
00630         for (snum = 0; snum < n_services; snum++) {
00631                 /* avoid removing PRINTERS_NAME or non-autoloaded printers */
00632                 if (snum == pnum || !(lp_snum_ok(snum) && lp_print_ok(snum) &&
00633                                       lp_autoloaded(snum)))
00634                         continue;
00635 
00636                 pname = lp_printername(snum);
00637                 if (!pcap_printername_ok(pname)) {
00638                         DEBUG(3, ("removing stale printer %s\n", pname));
00639 
00640                         if (is_printer_published(NULL, snum, NULL))
00641                                 nt_printer_publish(NULL, snum, SPOOL_DS_UNPUBLISH);
00642                         del_a_printer(pname);
00643                         lp_killservice(snum);
00644                 }
00645         }
00646 
00647         load_printers();
00648 }
00649 
00650 /****************************************************************************
00651  Reload the services file.
00652 **************************************************************************/
00653 
00654 BOOL reload_services(BOOL test)
00655 {
00656         BOOL ret;
00657         
00658         if (lp_loaded()) {
00659                 pstring fname;
00660                 pstrcpy(fname,lp_configfile());
00661                 if (file_exist(fname, NULL) &&
00662                     !strcsequal(fname, dyn_CONFIGFILE)) {
00663                         pstrcpy(dyn_CONFIGFILE, fname);
00664                         test = False;
00665                 }
00666         }
00667 
00668         reopen_logs();
00669 
00670         if (test && !lp_file_list_changed())
00671                 return(True);
00672 
00673         lp_killunused(conn_snum_used);
00674 
00675         ret = lp_load(dyn_CONFIGFILE, False, False, True, True);
00676 
00677         reload_printers();
00678 
00679         /* perhaps the config filename is now set */
00680         if (!test)
00681                 reload_services(True);
00682 
00683         reopen_logs();
00684 
00685         load_interfaces();
00686 
00687         if (smbd_server_fd() != -1) {      
00688                 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
00689                 set_socket_options(smbd_server_fd(), user_socket_options);
00690         }
00691 
00692         mangle_reset_cache();
00693         reset_stat_cache();
00694 
00695         /* this forces service parameters to be flushed */
00696         set_current_service(NULL,0,True);
00697 
00698         return(ret);
00699 }
00700 
00701 /****************************************************************************
00702  Exit the server.
00703 ****************************************************************************/
00704 
00705 /* Reasons for shutting down a server process. */
00706 enum server_exit_reason { SERVER_EXIT_NORMAL, SERVER_EXIT_ABNORMAL };
00707 
00708 static void exit_server_common(enum server_exit_reason how,
00709         const char *const reason) NORETURN_ATTRIBUTE;
00710 
00711 static void exit_server_common(enum server_exit_reason how,
00712         const char *const reason)
00713 {
00714         static int firsttime=1;
00715 
00716         if (!firsttime)
00717                 exit(0);
00718         firsttime = 0;
00719 
00720         change_to_root_user();
00721 
00722         if (negprot_global_auth_context) {
00723                 (negprot_global_auth_context->free)(&negprot_global_auth_context);
00724         }
00725 
00726         conn_close_all();
00727 
00728         invalidate_all_vuids();
00729 
00730         print_notify_send_messages(3); /* 3 second timeout. */
00731 
00732         /* delete our entry in the connections database. */
00733         yield_connection(NULL,"");
00734 
00735         respond_to_all_remaining_local_messages();
00736 
00737 #ifdef WITH_DFS
00738         if (dcelogin_atmost_once) {
00739                 dfs_unlogin();
00740         }
00741 #endif
00742 
00743         locking_end();
00744         printing_end();
00745 
00746         if (how != SERVER_EXIT_NORMAL) {
00747                 int oldlevel = DEBUGLEVEL;
00748                 char *last_inbuf = get_InBuffer();
00749 
00750                 DEBUGLEVEL = 10;
00751 
00752                 DEBUGSEP(0);
00753                 DEBUG(0,("Abnormal server exit: %s\n",
00754                         reason ? reason : "no explanation provided"));
00755                 DEBUGSEP(0);
00756 
00757                 log_stack_trace();
00758                 if (last_inbuf) {
00759                         DEBUG(0,("Last message was %s\n", LAST_MESSAGE()));
00760                         show_msg(last_inbuf);
00761                 }
00762 
00763                 DEBUGLEVEL = oldlevel;
00764                 dump_core();
00765 
00766         } else {    
00767                 DEBUG(3,("Server exit (%s)\n",
00768                         (reason ? reason : "normal exit")));
00769         }
00770 
00771         exit(0);
00772 }
00773 
00774 void exit_server(const char *const explanation)
00775 {
00776         exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
00777 }
00778 
00779 void exit_server_cleanly(const char *const explanation)
00780 {
00781         exit_server_common(SERVER_EXIT_NORMAL, explanation);
00782 }
00783 
00784 void exit_server_fault(void)
00785 {
00786         exit_server("critical server fault");
00787 }
00788 
00789 /****************************************************************************
00790  Initialise connect, service and file structs.
00791 ****************************************************************************/
00792 
00793 static BOOL init_structs(void )
00794 {
00795         /*
00796          * Set the machine NETBIOS name if not already
00797          * set from the config file.
00798          */
00799 
00800         if (!init_names())
00801                 return False;
00802 
00803         conn_init();
00804 
00805         file_init();
00806 
00807         /* for RPC pipes */
00808         init_rpc_pipe_hnd();
00809 
00810         init_dptrs();
00811 
00812         secrets_init();
00813 
00814         return True;
00815 }
00816 
00817 /****************************************************************************
00818  main program.
00819 ****************************************************************************/
00820 
00821 /* Declare prototype for build_options() to avoid having to run it through
00822    mkproto.h.  Mixing $(builddir) and $(srcdir) source files in the current
00823    prototype generation system is too complicated. */
00824 
00825 extern void build_options(BOOL screen);
00826 
00827  int main(int argc,const char *argv[])
00828 {
00829         /* shall I run as a daemon */
00830         static BOOL is_daemon = False;
00831         static BOOL interactive = False;
00832         static BOOL Fork = True;
00833         static BOOL no_process_group = False;
00834         static BOOL log_stdout = False;
00835         static char *ports = NULL;
00836         static char *profile_level = NULL;
00837         int opt;
00838         poptContext pc;
00839 
00840         struct poptOption long_options[] = {
00841         POPT_AUTOHELP
00842         {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon (default)" },
00843         {"interactive", 'i', POPT_ARG_VAL, &interactive, True, "Run interactive (not a daemon)"},
00844         {"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools, etc.)" },
00845         {"no-process-group", '\0', POPT_ARG_VAL, &no_process_group, True, "Don't create a new process group" },
00846         {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
00847         {"build-options", 'b', POPT_ARG_NONE, NULL, 'b', "Print build options" },
00848         {"port", 'p', POPT_ARG_STRING, &ports, 0, "Listen on the specified ports"},
00849         {"profiling-level", 'P', POPT_ARG_STRING, &profile_level, 0, "Set profiling level","PROFILE_LEVEL"},
00850         POPT_COMMON_SAMBA
00851         POPT_COMMON_DYNCONFIG
00852         POPT_TABLEEND
00853         };
00854 
00855         load_case_tables();
00856 
00857         TimeInit();
00858 
00859 #ifdef HAVE_SET_AUTH_PARAMETERS
00860         set_auth_parameters(argc,argv);
00861 #endif
00862 
00863         pc = poptGetContext("smbd", argc, argv, long_options, 0);
00864         
00865         while((opt = poptGetNextOpt(pc)) != -1) {
00866                 switch (opt)  {
00867                 case 'b':
00868                         build_options(True); /* Display output to screen as well as debug */ 
00869                         exit(0);
00870                         break;
00871                 }
00872         }
00873 
00874         poptFreeContext(pc);
00875 
00876 #ifdef HAVE_SETLUID
00877         /* needed for SecureWare on SCO */
00878         setluid(0);
00879 #endif
00880 
00881         sec_init();
00882 
00883         set_remote_machine_name("smbd", False);
00884 
00885         if (interactive) {
00886                 Fork = False;
00887                 log_stdout = True;
00888         }
00889 
00890         if (interactive && (DEBUGLEVEL >= 9)) {
00891                 talloc_enable_leak_report();
00892         }
00893 
00894         if (log_stdout && Fork) {
00895                 DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
00896                 exit(1);
00897         }
00898 
00899         setup_logging(argv[0],log_stdout);
00900 
00901         /* we want to re-seed early to prevent time delays causing
00902            client problems at a later date. (tridge) */
00903         generate_random_buffer(NULL, 0);
00904 
00905         /* make absolutely sure we run as root - to handle cases where people
00906            are crazy enough to have it setuid */
00907 
00908         gain_root_privilege();
00909         gain_root_group_privilege();
00910 
00911         fault_setup((void (*)(void *))exit_server_fault);
00912         dump_core_setup("smbd");
00913 
00914         CatchSignal(SIGTERM , SIGNAL_CAST sig_term);
00915         CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
00916         
00917         /* we are never interested in SIGPIPE */
00918         BlockSignals(True,SIGPIPE);
00919 
00920 #if defined(SIGFPE)
00921         /* we are never interested in SIGFPE */
00922         BlockSignals(True,SIGFPE);
00923 #endif
00924 
00925 #if defined(SIGUSR2)
00926         /* We are no longer interested in USR2 */
00927         BlockSignals(True,SIGUSR2);
00928 #endif
00929 
00930         /* POSIX demands that signals are inherited. If the invoking process has
00931          * these signals masked, we will have problems, as we won't recieve them. */
00932         BlockSignals(False, SIGHUP);
00933         BlockSignals(False, SIGUSR1);
00934         BlockSignals(False, SIGTERM);
00935 
00936         /* we want total control over the permissions on created files,
00937            so set our umask to 0 */
00938         umask(0);
00939 
00940         init_sec_ctx();
00941 
00942         reopen_logs();
00943 
00944         DEBUG(0,( "smbd version %s started.\n", SAMBA_VERSION_STRING));
00945         DEBUGADD( 0, ( "%s\n", COPYRIGHT_STARTUP_MESSAGE ) );
00946 
00947         DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
00948                  (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
00949 
00950         /* Output the build options to the debug log */ 
00951         build_options(False);
00952 
00953         if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
00954                 DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
00955                 exit(1);
00956         }
00957 
00958         /*
00959          * Do this before reload_services.
00960          */
00961 
00962         if (!reload_services(False))
00963                 return(-1);     
00964 
00965         init_structs();
00966 
00967 #ifdef WITH_PROFILE
00968         if (!profile_setup(False)) {
00969                 DEBUG(0,("ERROR: failed to setup profiling\n"));
00970                 return -1;
00971         }
00972         if (profile_level != NULL) {
00973                 int pl = atoi(profile_level);
00974                 struct process_id src;
00975 
00976                 DEBUG(1, ("setting profiling level: %s\n",profile_level));
00977                 src.pid = getpid();
00978                 set_profile_level(pl, src);
00979         }
00980 #endif
00981 
00982         DEBUG(3,( "loaded services\n"));
00983 
00984         if (!is_daemon && !is_a_socket(0)) {
00985                 if (!interactive)
00986                         DEBUG(0,("standard input is not a socket, assuming -D option\n"));
00987 
00988                 /*
00989                  * Setting is_daemon here prevents us from eventually calling
00990                  * the open_sockets_inetd()
00991                  */
00992 
00993                 is_daemon = True;
00994         }
00995 
00996         if (is_daemon && !interactive) {
00997                 DEBUG( 3, ( "Becoming a daemon.\n" ) );
00998                 become_daemon(Fork, no_process_group);
00999         }
01000 
01001 #if HAVE_SETPGID
01002         /*
01003          * If we're interactive we want to set our own process group for
01004          * signal management.
01005          */
01006         if (interactive && !no_process_group)
01007                 setpgid( (pid_t)0, (pid_t)0);
01008 #endif
01009 
01010         if (!directory_exist(lp_lockdir(), NULL))
01011                 mkdir(lp_lockdir(), 0755);
01012 
01013         if (is_daemon)
01014                 pidfile_create("smbd");
01015 
01016         /* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
01017         if (!message_init())
01018                 exit(1);
01019 
01020         /* Initialise the password backed before the global_sam_sid
01021            to ensure that we fetch from ldap before we make a domain sid up */
01022 
01023         if(!initialize_password_db(False))
01024                 exit(1);
01025 
01026         if (!secrets_init()) {
01027                 DEBUG(0, ("ERROR: smbd can not open secrets.tdb\n"));
01028                 exit(1);
01029         }
01030 
01031         if(!get_global_sam_sid()) {
01032                 DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
01033                 exit(1);
01034         }
01035 
01036         if (!session_init())
01037                 exit(1);
01038 
01039         if (conn_tdb_ctx() == NULL)
01040                 exit(1);
01041 
01042         if (!locking_init(0))
01043                 exit(1);
01044 
01045         namecache_enable();
01046 
01047         if (!init_registry())
01048                 exit(1);
01049 
01050 #if 0
01051         if (!init_svcctl_db())
01052                 exit(1);
01053 #endif
01054 
01055         if (!print_backend_init())
01056                 exit(1);
01057 
01058         if (!init_guest_info()) {
01059                 DEBUG(0,("ERROR: failed to setup guest info.\n"));
01060                 return -1;
01061         }
01062 
01063         /* Setup the main smbd so that we can get messages. */
01064         /* don't worry about general printing messages here */
01065 
01066         claim_connection(NULL,"",0,True,FLAG_MSG_GENERAL|FLAG_MSG_SMBD);
01067 
01068         /* only start the background queue daemon if we are 
01069            running as a daemon -- bad things will happen if
01070            smbd is launched via inetd and we fork a copy of 
01071            ourselves here */
01072 
01073         if ( is_daemon && !interactive )
01074                 start_background_queue(); 
01075 
01076         /* Always attempt to initialize DMAPI. We will only use it later if
01077          * lp_dmapi_support is set on the share, but we need a single global
01078          * session to work with.
01079          */
01080         dmapi_init_session();
01081 
01082         if (!open_sockets_smbd(is_daemon, interactive, ports))
01083                 exit(1);
01084 
01085         /*
01086          * everything after this point is run after the fork()
01087          */ 
01088 
01089         static_init_rpc;
01090 
01091         init_modules();
01092 
01093         /* Possibly reload the services file. Only worth doing in
01094          * daemon mode. In inetd mode, we know we only just loaded this.
01095          */
01096         if (is_daemon) {
01097                 reload_services(True);
01098         }
01099 
01100         if (!init_account_policy()) {
01101                 DEBUG(0,("Could not open account policy tdb.\n"));
01102                 exit(1);
01103         }
01104 
01105         if (*lp_rootdir()) {
01106                 if (sys_chroot(lp_rootdir()) == 0)
01107                         DEBUG(2,("Changed root to %s\n", lp_rootdir()));
01108         }
01109 
01110         /* Setup oplocks */
01111         if (!init_oplocks())
01112                 exit(1);
01113         
01114         /* Setup aio signal handler. */
01115         initialize_async_io_handler();
01116 
01117         /* register our message handlers */
01118         message_register(MSG_SMB_FORCE_TDIS, msg_force_tdis, NULL);
01119 
01120         smbd_process();
01121 
01122         namecache_shutdown();
01123 
01124         exit_server_cleanly(NULL);
01125         return(0);
01126 }

Sambaに対してSat Aug 29 21:23:26 2009に生成されました。  doxygen 1.4.7