static pid_t parse_dest(const char *dest) { pid_t pid; /* Zero is a special return value for broadcast smbd */ if (strequal(dest, "smbd")) return 0; /* Try self - useful for testing */ if (strequal(dest, "self")) return sys_getpid(); /* Check for numeric pid number */ if ((pid = atoi(dest)) != 0) return pid; /* Look up other destinations in pidfile directory */ if ((pid = pidfile_pid(dest)) != 0) return pid; fprintf(stderr,"Can't find pid for destination '%s'\n", dest); return -1; }
/**************************************************************************** send a message to a named destination ****************************************************************************/ static BOOL send_message(char *dest, int msg_type, void *buf, int len, BOOL duplicates) { BOOL retval = False; pid_t pid = 0; TDB_CONTEXT *the_tdb; the_tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDWR, 0); if (!the_tdb) { fprintf(stderr,"Failed to open connections database in send_message.\n"); return False; } /* "smbd" is the only broadcast operation */ if (strequal(dest,"smbd")) { retval = message_send_all(the_tdb,msg_type, buf, len, duplicates, NULL); } else if (strequal(dest,"nmbd")) { pid = pidfile_pid(dest); if (pid == 0) { fprintf(stderr,"Can't find pid for nmbd\n"); } } else if (strequal(dest,"self")) { pid = getpid(); } else { pid = atoi(dest); if (pid == 0) { fprintf(stderr,"Not a valid pid\n"); } } tdb_close(the_tdb); if (pid) return message_send_pid(pid, msg_type, buf, len, duplicates); else return retval; }
/* create a pid file in the pid directory. open it and leave it locked */ void pidfile_create(const char *program_name) { int fd; char buf[20]; char *short_configfile; pstring name; pstring pidFile; pid_t pid; /* Add a suffix to the program name if this is a process with a * none default configuration file name. */ if (strcmp( CONFIGFILE, dyn_CONFIGFILE) == 0) { strncpy( name, program_name, sizeof( name)-1); } else { short_configfile = strrchr( dyn_CONFIGFILE, '/'); if (short_configfile == NULL) { /* conf file in current directory */ short_configfile = dyn_CONFIGFILE; } else { /* full/relative path provided */ short_configfile++; } slprintf( name, sizeof( name)-1, "%s-%s", program_name, short_configfile ); } slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_piddir(), name); pid = pidfile_pid(name); if (pid != 0) { DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n", name, pidFile, (int)pid)); exit(1); } fd = sys_open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL, 0644); if (fd == -1) { DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile, strerror(errno))); exit(1); } if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_WRLCK)==False) { DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n", name, pidFile, strerror(errno))); exit(1); } memset(buf, 0, sizeof(buf)); slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) sys_getpid()); if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) { DEBUG(0,("ERROR: can't write to file %s: %s\n", pidFile, strerror(errno))); exit(1); } /* Leave pid file open & locked for the duration... */ }
/* stop nmbd */ void stop_nmbd(void) { pid_t pid = pidfile_pid("nmbd"); if (geteuid() != 0) return; if (pid <= 0) return; kill(pid, SIGTERM); }
/* stop winbindd */ void stop_winbindd(void) { pid_t pid = pidfile_pid("winbindd"); if (geteuid() != 0) return; if (pid <= 0) return; kill(pid, SIGTERM); }
static struct server_id parse_dest(const char *dest) { struct server_id result = {-1}; pid_t pid; /* Zero is a special return value for broadcast to all processes */ if (strequal(dest, "all")) { return interpret_pid(MSG_BROADCAST_PID_STR); } /* Try self - useful for testing */ if (strequal(dest, "self")) { return pid_to_procid(sys_getpid()); } /* Fix winbind typo. */ if (strequal(dest, "winbind")) { dest = "winbindd"; } if (!(strequal(dest, "winbindd") || strequal(dest, "nmbd"))) { /* Check for numeric pid number */ result = interpret_pid(dest); /* Zero isn't valid if not smbd. */ if (result.pid && procid_valid(&result)) { return result; } } /* Look up other destinations in pidfile directory */ if ((pid = pidfile_pid(dest)) != 0) { return pid_to_procid(pid); } fprintf(stderr,"Can't find pid for destination '%s'\n", dest); return result; }
/** * create a pid file in the pid directory. open it and leave it locked */ void pidfile_create(const char *piddir, const char *name) { int fd; char buf[20]; char *pidFile; pid_t pid; if (asprintf(&pidFile, "%s/%s.pid", piddir, name) < 0) { DEBUG(0,("ERROR: Out of memory\n")); exit(1); } pid = pidfile_pid(piddir, name); if (pid != 0) { DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n", name, pidFile, (int)pid)); exit(1); } fd = open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL, 0644); if (fd == -1) { DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile, strerror(errno))); exit(1); } if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==false) { DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n", name, pidFile, strerror(errno))); exit(1); } memset(buf, 0, sizeof(buf)); slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) getpid()); if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) { DEBUG(0,("ERROR: can't write to file %s: %s\n", pidFile, strerror(errno))); exit(1); } /* Leave pid file open & locked for the duration... */ SAFE_FREE(pidFile); }
bool cli_send_mailslot(struct messaging_context *msg_ctx, bool unique, const char *mailslot, uint16 priority, char *buf, int len, const char *srcname, int src_type, const char *dstname, int dest_type, const struct sockaddr_storage *dest_ss) { struct packet_struct p; struct dgram_packet *dgram = &p.packet.dgram; char *ptr, *p2; char tmp[4]; pid_t nmbd_pid; char addr[INET6_ADDRSTRLEN]; if ((nmbd_pid = pidfile_pid("nmbd")) == 0) { DEBUG(3, ("No nmbd found\n")); return False; } if (dest_ss->ss_family != AF_INET) { DEBUG(3, ("cli_send_mailslot: can't send to IPv6 address.\n")); return false; } memset((char *)&p, '\0', sizeof(p)); /* * Next, build the DGRAM ... */ /* DIRECT GROUP or UNIQUE datagram. */ dgram->header.msg_type = unique ? 0x10 : 0x11; dgram->header.flags.node_type = M_NODE; dgram->header.flags.first = True; dgram->header.flags.more = False; dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100); /* source ip is filled by nmbd */ dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */ dgram->header.packet_offset = 0; make_nmb_name(&dgram->source_name,srcname,src_type); make_nmb_name(&dgram->dest_name,dstname,dest_type); ptr = &dgram->data[0]; /* Setup the smb part. */ ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */ memcpy(tmp,ptr,4); if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) { DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n")); return False; } cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True); memcpy(ptr,tmp,4); SCVAL(ptr,smb_com,SMBtrans); SSVAL(ptr,smb_vwv1,len); SSVAL(ptr,smb_vwv11,len); SSVAL(ptr,smb_vwv12,70 + strlen(mailslot)); SSVAL(ptr,smb_vwv13,3); SSVAL(ptr,smb_vwv14,1); SSVAL(ptr,smb_vwv15,priority); SSVAL(ptr,smb_vwv16,2); p2 = smb_buf(ptr); fstrcpy(p2,mailslot); p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2); if (!p2) { return False; } memcpy(p2,buf,len); p2 += len; dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */ p.packet_type = DGRAM_PACKET; p.ip = ((const struct sockaddr_in *)dest_ss)->sin_addr; p.timestamp = time(NULL); DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ", mailslot, nmb_namestr(&dgram->source_name))); print_sockaddr(addr, sizeof(addr), dest_ss); DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr)); return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx, pid_to_procid(nmbd_pid), MSG_SEND_PACKET, (uint8 *)&p, sizeof(p))); }
/* show the current server status */ void status_page(void) { const char *v; int autorefresh=0; int refresh_interval=30; int nr_running=0; bool waitup = False; TALLOC_CTX *ctx = talloc_stackframe(); const char form_name[] = "status"; smbd_pid = pid_to_procid(pidfile_pid("smbd")); if (!verify_xsrf_token(form_name)) { goto output_page; } if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) { stop_smbd(); start_smbd(); waitup=True; } if (cgi_variable("smbd_start") || cgi_variable("all_start")) { start_smbd(); waitup=True; } if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) { stop_smbd(); waitup=True; } if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) { stop_nmbd(); start_nmbd(); waitup=True; } if (cgi_variable("nmbd_start") || cgi_variable("all_start")) { start_nmbd(); waitup=True; } if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) { stop_nmbd(); waitup=True; } #ifdef WITH_WINBIND if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) { stop_winbindd(); start_winbindd(); waitup=True; } if (cgi_variable("winbindd_start") || cgi_variable("all_start")) { start_winbindd(); waitup=True; } if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) { stop_winbindd(); waitup=True; } #endif /* wait for daemons to start/stop */ if (waitup) sleep(SLEEP_TIME); if (cgi_variable("autorefresh")) { autorefresh = 1; } else if (cgi_variable("norefresh")) { autorefresh = 0; } else if (cgi_variable("refresh")) { autorefresh = 1; } if ((v=cgi_variable("refresh_interval"))) { refresh_interval = atoi(v); } if (cgi_variable("show_client_in_col_1")) { PID_or_Machine = 1; } if (cgi_variable("show_pid_in_col_1")) { PID_or_Machine = 0; } connections_forall_read(traverse_fn1, NULL); initPid2Machine (); output_page: printf("<H2>%s</H2>\n", _("Server Status")); printf("<FORM method=post>\n"); print_xsrf_token(cgi_user_name(), cgi_user_pass(), form_name); if (!autorefresh) { printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh")); printf("<br>%s", _("Refresh Interval: ")); printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n", refresh_interval); } else { printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing")); printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval); printf("<input type=hidden name=\"refresh\" value=\"1\">\n"); } printf("<p>\n"); printf("<table>\n"); printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), samba_version_string()); fflush(stdout); printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running")); if (geteuid() == 0) { if (smbd_running()) { nr_running++; printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd")); } else { printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd")); } printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd")); } printf("</tr>\n"); fflush(stdout); printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running")); if (geteuid() == 0) { if (nmbd_running()) { nr_running++; printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd")); } else { printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd")); } printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd")); } printf("</tr>\n"); #ifdef WITH_WINBIND fflush(stdout); printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running")); if (geteuid() == 0) { if (winbindd_running()) { nr_running++; printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd")); } else { printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd")); } printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd")); } printf("</tr>\n"); #endif if (geteuid() == 0) { printf("<tr><td></td><td></td>\n"); if (nr_running >= 1) { /* stop, restart all */ printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All")); printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All")); } else if (nr_running == 0) { /* start all */ printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All")); } printf("</tr>\n"); } printf("</table>\n"); fflush(stdout); printf("<p><h3>%s</h3>\n", _("Active Connections")); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date")); if (geteuid() == 0) { printf("<th>%s</th>\n", _("Kill")); } printf("</tr>\n"); connections_forall_read(traverse_fn2, NULL); printf("</table><p>\n"); printf("<p><h3>%s</h3>\n", _("Active Shares")); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n", _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date")); connections_forall_read(traverse_fn3, NULL); printf("</table><p>\n"); printf("<h3>%s</h3>\n", _("Open Files")); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n", _("PID"), _("UID"), _("Sharing"), _("R/W"), _("Oplock"), _("Share"), _("File"), _("Date")); locking_init_readonly(); share_mode_forall(print_share_mode, NULL); locking_end(); printf("</table>\n"); printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1")); printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1")); printf("</FORM>\n"); if (autorefresh) { /* this little JavaScript allows for automatic refresh of the page. There are other methods but this seems to be the best alternative */ printf("<script language=\"JavaScript\">\n"); printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n", cgi_baseurl(), refresh_interval, refresh_interval*1000); printf("//-->\n</script>\n"); } TALLOC_FREE(ctx); }
/* show the current server status */ void status_page(void) { const char *v; int autorefresh=0; int refresh_interval=30; TDB_CONTEXT *tdb; int nr_running=0; BOOL waitup = False; smbd_pid = pidfile_pid("smbd"); if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) { stop_smbd(); start_smbd(); waitup=True; } if (cgi_variable("smbd_start") || cgi_variable("all_start")) { start_smbd(); waitup=True; } if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) { stop_smbd(); waitup=True; } if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) { stop_nmbd(); start_nmbd(); waitup=True; } if (cgi_variable("nmbd_start") || cgi_variable("all_start")) { start_nmbd(); waitup=True; } if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) { stop_nmbd(); waitup=True; } #ifdef WITH_WINBIND if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) { stop_winbindd(); start_winbindd(); waitup=True; } if (cgi_variable("winbindd_start") || cgi_variable("all_start")) { start_winbindd(); waitup=True; } if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) { stop_winbindd(); waitup=True; } #endif /* wait for daemons to start/stop */ if (waitup) sleep(SLEEP_TIME); if (cgi_variable("autorefresh")) { autorefresh = 1; } else if (cgi_variable("norefresh")) { autorefresh = 0; } else if (cgi_variable("refresh")) { autorefresh = 1; } if ((v=cgi_variable("refresh_interval"))) { refresh_interval = atoi(v); } if (cgi_variable("show_client_in_col_1")) { PID_or_Machine = 1; } if (cgi_variable("show_pid_in_col_1")) { PID_or_Machine = 0; } tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0); if (tdb) tdb_traverse(tdb, traverse_fn1, NULL); initPid2Machine (); printf("<H2>%s</H2>\n", _("Server Status")); printf("<FORM method=post>\n"); if (!autorefresh) { printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh")); printf("<br>%s", _("Refresh Interval: ")); printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n", refresh_interval); } else { printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing")); printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval); printf("<input type=hidden name=\"refresh\" value=\"1\">\n"); } printf("<p>\n"); if (!tdb) { /* open failure either means no connections have been made */ } printf("<table>\n"); printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING); fflush(stdout); printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running")); if (geteuid() == 0) { if (smbd_running()) { nr_running++; printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd")); } else { printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd")); } printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd")); } printf("</tr>\n"); fflush(stdout); printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running")); if (geteuid() == 0) { if (nmbd_running()) { nr_running++; printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd")); } else { printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd")); } printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd")); } printf("</tr>\n"); #ifdef WITH_WINBIND fflush(stdout); printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running")); if (geteuid() == 0) { if (winbindd_running()) { nr_running++; printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd")); } else { printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd")); } printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd")); } printf("</tr>\n"); #endif if (geteuid() == 0) { printf("<tr><td></td><td></td>\n"); if (nr_running >= 1) { /* stop, restart all */ printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All")); printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All")); } else if (nr_running == 0) { /* start all */ printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All")); } printf("</tr>\n"); } printf("</table>\n"); fflush(stdout); printf("<p><h3>%s</h3>\n", _("Active Connections")); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date")); if (geteuid() == 0) { printf("<th>%s</th>\n", _("Kill")); } printf("</tr>\n"); if (tdb) tdb_traverse(tdb, traverse_fn2, NULL); printf("</table><p>\n"); printf("<p><h3>%s</h3>\n", _("Active Shares")); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n", _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date")); if (tdb) tdb_traverse(tdb, traverse_fn3, NULL); printf("</table><p>\n"); printf("<h3>%s</h3>\n", _("Open Files")); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n", _("PID"), _("Sharing"), _("R/W"), _("Oplock"), _("File"), _("Date")); locking_init(1); share_mode_forall(print_share_mode); locking_end(); printf("</table>\n"); if (tdb) tdb_close(tdb); printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1")); printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1")); printf("</FORM>\n"); if (autorefresh) { /* this little JavaScript allows for automatic refresh of the page. There are other methods but this seems to be the best alternative */ printf("<script language=\"JavaScript\">\n"); printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n", cgi_baseurl(), refresh_interval, refresh_interval*1000); printf("//-->\n</script>\n"); } }
/* create a pid file in the pid directory. open it and leave it locked */ void pidfile_create(const char *program_name) { int fd; char buf[20]; const char *short_configfile; char *name; pid_t pid; /* Add a suffix to the program name if this is a process with a * none default configuration file name. */ if (strcmp( CONFIGFILE, get_dyn_CONFIGFILE()) == 0) { name = SMB_STRDUP(program_name); } else { short_configfile = strrchr( get_dyn_CONFIGFILE(), '/'); if (short_configfile == NULL) { /* conf file in current directory */ short_configfile = get_dyn_CONFIGFILE(); } else { /* full/relative path provided */ short_configfile++; } if (asprintf(&name, "%s-%s", program_name, short_configfile) == -1) { smb_panic("asprintf failed"); } } if (asprintf(&pidFile_name, "%s/%s.pid", lp_piddir(), name) == -1) { smb_panic("asprintf failed"); } pid = pidfile_pid(name); if (pid != 0) { DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n", name, pidFile_name, (int)pid)); exit(1); } fd = open(pidFile_name, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL, 0644); if (fd == -1) { DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile_name, strerror(errno))); exit(1); } if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==False) { DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n", name, pidFile_name, strerror(errno))); exit(1); } memset(buf, 0, sizeof(buf)); slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int)getpid()); if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) { DEBUG(0,("ERROR: can't write to file %s: %s\n", pidFile_name, strerror(errno))); exit(1); } /* Leave pid file open & locked for the duration... */ SAFE_FREE(name); /* set the close on exec so that we don't leak the fd */ fcntl(fd, F_SETFD, FD_CLOEXEC); }
/* show the current server status */ void status_page(void) { const char *v; int autorefresh=0; int refresh_interval=30; TDB_CONTEXT *tdb; smbd_pid = pidfile_pid("smbd"); if (cgi_variable("smbd_restart")) { stop_smbd(); start_smbd(); } if (cgi_variable("smbd_start")) { start_smbd(); } if (cgi_variable("smbd_stop")) { stop_smbd(); } if (cgi_variable("nmbd_restart")) { stop_nmbd(); start_nmbd(); } if (cgi_variable("nmbd_start")) { start_nmbd(); } if (cgi_variable("nmbd_stop")) { stop_nmbd(); } if (cgi_variable("autorefresh")) { autorefresh = 1; } else if (cgi_variable("norefresh")) { autorefresh = 0; } else if (cgi_variable("refresh")) { autorefresh = 1; } if ((v=cgi_variable("refresh_interval"))) { refresh_interval = atoi(v); } if (cgi_variable("show_client_in_col_1")) { PID_or_Machine = 1; } tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0); if (tdb) tdb_traverse(tdb, traverse_fn1, NULL); initPid2Machine (); printf("<H2>Server Status</H2>\n"); printf("<FORM method=post>\n"); if (!autorefresh) { printf("<input type=submit value=\"Auto Refresh\" name=autorefresh>\n"); printf("<br>Refresh Interval: "); printf("<input type=text size=2 name=\"refresh_interval\" value=%d>\n", refresh_interval); } else { printf("<input type=submit value=\"Stop Refreshing\" name=norefresh>\n"); printf("<br>Refresh Interval: %d\n", refresh_interval); printf("<input type=hidden name=refresh value=1>\n"); } printf("<p>\n"); if (!tdb) { /* open failure either means no connections have been made or status=no */ if (!lp_status(-1)) printf("You need to have status=yes in your smb config file\n"); } printf("<table>\n"); printf("<tr><td>version:</td><td>%s</td></tr>",VERSION); fflush(stdout); printf("<tr><td>smbd:</td><td>%srunning</td>\n",smbd_running()?"":"not "); if (geteuid() == 0) { if (smbd_running()) { printf("<td><input type=submit name=\"smbd_stop\" value=\"Stop smbd\"></td>\n"); } else { printf("<td><input type=submit name=\"smbd_start\" value=\"Start smbd\"></td>\n"); } printf("<td><input type=submit name=\"smbd_restart\" value=\"Restart smbd\"></td>\n"); } printf("</tr>\n"); fflush(stdout); printf("<tr><td>nmbd:</td><td>%srunning</td>\n",nmbd_running()?"":"not "); if (geteuid() == 0) { if (nmbd_running()) { printf("<td><input type=submit name=\"nmbd_stop\" value=\"Stop nmbd\"></td>\n"); } else { printf("<td><input type=submit name=\"nmbd_start\" value=\"Start nmbd\"></td>\n"); } printf("<td><input type=submit name=\"nmbd_restart\" value=\"Restart nmbd\"></td>\n"); } printf("</tr>\n"); printf("</table>\n"); fflush(stdout); printf("<p><h3>Active Connections</h3>\n"); printf("<table border=1>\n"); printf("<tr><th>PID</th><th>Client</th><th>IP address</th><th>Date</th>\n"); if (geteuid() == 0) { printf("<th>Kill</th>\n"); } printf("</tr>\n"); if (tdb) tdb_traverse(tdb, traverse_fn2, NULL); printf("</table><p>\n"); printf("<p><h3>Active Shares</h3>\n"); printf("<table border=1>\n"); printf("<tr><th>Share</th><th>User</th><th>Group</th><th>PID</th><th>Client</th><th>Date</th></tr>\n\n"); if (tdb) tdb_traverse(tdb, traverse_fn3, NULL); printf("</table><p>\n"); printf("<h3>Open Files</h3>\n"); printf("<table border=1>\n"); printf("<tr><th>%s</th><th>Sharing</th><th>R/W</th><th>Oplock</th><th>File</th><th>Date</th></tr>\n", PID_or_Machine ? "Client" : "PID"); locking_init(1); share_mode_forall(print_share_mode); locking_end(); printf("</table>\n"); if (tdb) tdb_close(tdb); printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"Show Client in col 1\">\n"); printf("<input type=submit name=\"show_pid_in_col_1\" value=\"Show PID in col 1\">\n"); printf("</FORM>\n"); if (autorefresh) { /* this little JavaScript allows for automatic refresh of the page. There are other methods but this seems to be the best alternative */ printf("<script language=\"JavaScript\">\n"); printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n", cgi_baseurl(), refresh_interval, refresh_interval*1000); printf("//-->\n</script>\n"); } }