int FTPServer::start () { int status; if ((status = listenSocket.bind (listenPort)) < 0) return status; if ((status = listenSocket.listen (BACKLOG)) < 0) return status; struct sigaction sa; sa.sa_handler = sigchld_handler; sigemptyset (&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction (SIGCHLD, &sa, NULL) == -1) { cerr << "ERROR! sigaction failed.\n"; exit(1); } cout << "Server Initialized!\n\n"; while (true) { Socket incoming = listenSocket.accept (); cout << "Accepted connection from - " << incoming.getDestAddr (); cout << ":" << incoming.getDestPort () << "\n\n"; if (!fork ()) { listenSocket.close (); serveConnection (incoming); exit (0); } incoming.close (); } return 0; }
int main(int argc, char *argv[]) { int fd_socket, read_socket; int listen_port; char client_ip[120]; struct hostent *resolved_client; struct sockaddr_storage tentative_addr; struct sockaddr *cli_addr; int real_addr_size; fd_set readfs, masterfs; int retcod, status; int exit_program = 0; pid_t pid; char ainfo_port_string[16]; struct addrinfo ai_req, *ai_ans, *cur_ans; int address_found; #ifdef MTRACE_ON char mtrace_log[2048]; #endif #ifdef MTRACE_ON sprintf(mtrace_log, "mtrace_%d.log", getpid()); setenv("MALLOC_TRACE", mtrace_log, 1); mtrace(); #endif listen_port = 19999; if (argc > 2) { if (strncmp(argv[1],"-p", 2) == 0) listen_port = atoi(argv[2]); } openlog("blahpd", LOG_PID, LOG_DAEMON); syslog(LOG_DAEMON | LOG_INFO, "Starting blah server (%s)", RCSID_VERSION); printf("Starting BLAHP server...\n"); printf("%s\n", RCSID_VERSION); ai_req.ai_flags = AI_PASSIVE; ai_req.ai_family = PF_UNSPEC; ai_req.ai_socktype = SOCK_STREAM; ai_req.ai_protocol = 0; /* Any stream protocol is OK */ sprintf(ainfo_port_string,"%5d",listen_port); if (getaddrinfo(NULL, ainfo_port_string, &ai_req, &ai_ans) != 0) { fprintf(stderr, "Cannot get address of passive SOCK_STREAM socket.\n"); exit(1); } address_found = 0; for (cur_ans = ai_ans; cur_ans != NULL; cur_ans = cur_ans->ai_next) { if ((fd_socket = socket(cur_ans->ai_family, cur_ans->ai_socktype, cur_ans->ai_protocol)) == -1) { continue; } if (bind(fd_socket,cur_ans->ai_addr, cur_ans->ai_addrlen) == 0) { address_found = 1; break; } close(fd_socket); } freeaddrinfo(ai_ans); if (address_found == 0) { fprintf(stderr, "Cannot create or bind socket: %s\n", strerror(errno)); exit(1); } if (listen(fd_socket,1) == -1) { fprintf(stderr, "Cannot listen from socket: %s\n", strerror(errno)); exit(1); } else printf("Server up and listening on port %d...\n", listen_port); FD_ZERO(&masterfs); FD_SET(0, &masterfs); FD_SET(fd_socket, &masterfs); while (!exit_program) { printf("\nBLAHP Server > "); readfs = masterfs; fflush(stdout); if ((retcod = select(FD_SETSIZE, &readfs, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0)) < 0) { perror("Select error"); close(fd_socket); exit(1); } if (FD_ISSET(0, &readfs)) { exit_program = processConsoleCommand(); } if (FD_ISSET(fd_socket, &readfs)) { real_addr_size = sizeof(tentative_addr); cli_addr = (struct sockaddr *)(&tentative_addr); if ((read_socket = accept(fd_socket, cli_addr, &real_addr_size)) == -1) { fprintf(stderr,"\nCannot accept connection: %s\n", strerror(errno)); exit(1); } if ((getnameinfo(cli_addr, real_addr_size, client_ip, sizeof(client_ip), NULL, 0, 0)) < 0) { client_ip[sizeof(client_ip)-1]='\000'; strncpy(client_ip, "-unknown-", sizeof(client_ip) - 1); } printf("\nIncoming connection from %s\n", client_ip); while (waitpid(-1, &status, WNOHANG) > 0); pid = fork(); if (pid < 0) { fprintf(stderr, "\nCannot fork connection manager: %s\n", strerror(errno)); close(read_socket); } else if (pid >0) { printf("\nNew connection managed by child process %d\n", pid); close(read_socket); } else { close(fd_socket); syslog(LOG_DAEMON | LOG_INFO, "fork to serve connection from %s", client_ip); serveConnection(read_socket, client_ip); } } } printf("Server shutting down...\n"); shutdown(fd_socket, SHUT_RDWR); close(fd_socket); printf("Socket closed\n"); /* Shutdown of child processes */ printf("Goodbye!\n"); exit(0); }
static void doSyslogd(void) { struct sockaddr_un sunx; socklen_t addrLength; /*BRCM begin*/ /* * In CMS, we don't need the pidfile. Don't write it out to save some DRAM. * FILE *pidfile; * char pidfilename[30]; */ /* All the access to /dev/log will be redirected to /var/log/log * * which is TMPFS, memory file system. **/ #define BRCM_PATH_LOG "/var/log/log" /*BRCM end*/ int sock_fd; fd_set fds; /* Set up signal handlers. */ signal(SIGINT, quit_signal); signal(SIGTERM, quit_signal); signal(SIGQUIT, quit_signal); signal(SIGHUP, SIG_IGN); signal(SIGCHLD, SIG_IGN); #ifdef SIGCLD signal(SIGCLD, SIG_IGN); #endif signal(SIGALRM, domark); alarm(MarkInterval); /* Create the syslog file so realpath() can work. */ /*BRCM begin*/ /* if (realpath (_PATH_LOG, lfile) != NULL) { */ if (realpath (BRCM_PATH_LOG, lfile) != NULL) { /*BRCM end*/ unlink (lfile); } memset(&sunx, 0, sizeof(sunx)); sunx.sun_family = AF_UNIX; strncpy(sunx.sun_path, lfile, sizeof(sunx.sun_path)); if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { /*BRCM begin*/ /* bb_perror_msg_and_die("Couldn't get file descriptor for socket " _PATH_LOG); */ bb_error_msg_and_die ("Couldn't get file descriptor for socket " BRCM_PATH_LOG); /*BRCM end*/ } addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) { /*BRCM begin*/ /* bb_perror_msg_and_die("Could not connect to socket " _PATH_LOG); */ bb_error_msg_and_die ("Could not connect to socket " BRCM_PATH_LOG); /*BRCM end*/ } if (chmod(lfile, 0666) < 0) { /*BRCM begin*/ /* bb_perror_msg_and_die("Could not set permission on " _PATH_LOG); */ bb_error_msg_and_die ("Could not set permission on " BRCM_PATH_LOG); /*BRCM end*/ } #ifdef CONFIG_FEATURE_IPC_SYSLOG if (circular_logging == TRUE) { ipcsyslog_init(); } #endif #ifdef CONFIG_FEATURE_REMOTE_LOG if (doRemoteLog == TRUE) { init_RemoteLog(); } #endif /*BRCM begin*/ if (localLogLevel < LOG_EMERG) localLogLevel = LOG_DEBUG; if (remoteLogLevel < LOG_EMERG) remoteLogLevel = LOG_ERR; /* change to priority emerg to be consistent with klogd */ /* logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " BB_BANNER); */ logMessage (LOG_SYSLOG | LOG_EMERG, "BCM96345 started: " BB_BANNER); /* * In CMS, we don't need the pid file, so don't write one and save some DRAM. * sprintf (pidfilename, "%s%s.pid", _PATH_VARRUN, "syslogd"); * if ((pidfile = fopen (pidfilename, "w")) != NULL) { * fprintf (pidfile, "%d\n", getpid ()); * (void) fclose (pidfile); * } * else { * logMessage ((LOG_SYSLOG | LOG_ERR),("Failed to create pid file %s", pidfilename)); * } */ /*BRCM end*/ for (;;) { FD_ZERO(&fds); FD_SET(sock_fd, &fds); if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) { if (errno == EINTR) { /* alarm may have happened. */ continue; } bb_perror_msg_and_die("select error"); } if (FD_ISSET(sock_fd, &fds)) { int i; RESERVE_CONFIG_BUFFER(tmpbuf, MAXLINE + 1); memset(tmpbuf, '\0', MAXLINE + 1); if ((i = recv(sock_fd, tmpbuf, MAXLINE, 0)) > 0) { serveConnection(tmpbuf, i); } else { bb_perror_msg_and_die("UNIX socket error"); } RELEASE_CONFIG_BUFFER(tmpbuf); } /* FD_ISSET() */ } /* for main loop */ }
static void doSyslogd(void) { struct sockaddr_un sunx; socklen_t addrLength; int sock_fd; fd_set fds; /* Set up signal handlers. */ signal(SIGINT, quit_signal); signal(SIGTERM, quit_signal); signal(SIGQUIT, quit_signal); signal(SIGHUP, SIG_IGN); signal(SIGCHLD, SIG_IGN); #ifdef SIGCLD signal(SIGCLD, SIG_IGN); #endif signal(SIGALRM, domark); alarm(MarkInterval); /* Create the syslog file so realpath() can work. */ /* Add support of -p parameter of syslogd */ if (realpath(logSockPath, lfile) != NULL) { unlink(lfile); } memset(&sunx, 0, sizeof(sunx)); sunx.sun_family = AF_UNIX; strncpy(sunx.sun_path, lfile, sizeof(sunx.sun_path)); if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { /* Add support of -p parameter of syslogd */ bb_perror_msg_and_die("Couldn't get file descriptor for socket %s", logSockPath); } addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) { /* Add support of -p parameter of syslogd */ bb_perror_msg_and_die("Could not connect to socket %s", logSockPath); } if (chmod(lfile, 0666) < 0) { /* Add support of -p parameter of syslogd */ bb_perror_msg_and_die("Could not set permission on %s", logSockPath); } #ifdef CONFIG_FEATURE_IPC_SYSLOG if (circular_logging == TRUE) { ipcsyslog_init(); } #endif #ifdef CONFIG_FEATURE_REMOTE_LOG if (doRemoteLog == TRUE) { init_RemoteLog(); } #endif logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " "BusyBox v" BB_VER ); for (;;) { FD_ZERO(&fds); FD_SET(sock_fd, &fds); if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) { if (errno == EINTR) { /* alarm may have happened. */ continue; } bb_perror_msg_and_die("select error"); } if (FD_ISSET(sock_fd, &fds)) { int i; #if MAXLINE > BUFSIZ # define TMP_BUF_SZ BUFSIZ #else # define TMP_BUF_SZ MAXLINE #endif #define tmpbuf bb_common_bufsiz1 if ((i = recv(sock_fd, tmpbuf, TMP_BUF_SZ, 0)) > 0) { tmpbuf[i] = '\0'; serveConnection(tmpbuf, i); } else { bb_perror_msg_and_die("UNIX socket error"); } } /* FD_ISSET() */ } /* for main loop */ }
static void doSyslogd(void) { struct sockaddr_un sunx; socklen_t addrLength; int sock_fd; fd_set fds; /* Set up signal handlers. */ signal(SIGINT, quit_signal); signal(SIGTERM, quit_signal); signal(SIGQUIT, quit_signal); signal(SIGHUP, SIG_IGN); signal(SIGCHLD, SIG_IGN); #ifdef SIGCLD signal(SIGCLD, SIG_IGN); #endif signal(SIGALRM, domark); alarm(MarkInterval); /* Create the syslog file so realpath() can work. */ if (realpath(_PATH_LOG, lfile) != NULL) { unlink(lfile); } memset(&sunx, 0, sizeof(sunx)); sunx.sun_family = AF_UNIX; strncpy(sunx.sun_path, lfile, sizeof(sunx.sun_path)); if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { bb_perror_msg_and_die("Couldn't get file descriptor for socket " _PATH_LOG); } addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) { bb_perror_msg_and_die("Could not connect to socket " _PATH_LOG); } if (chmod(lfile, 0666) < 0) { bb_perror_msg_and_die("Could not set permission on " _PATH_LOG); } #ifdef CONFIG_FEATURE_IPC_SYSLOG if (circular_logging == TRUE) { ipcsyslog_init(); } #endif #ifdef CONFIG_FEATURE_REMOTE_LOG if (doRemoteLog == TRUE) { init_RemoteLog(); } #endif logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " BB_BANNER); for (;;) { FD_ZERO(&fds); FD_SET(sock_fd, &fds); if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) { if (errno == EINTR) { /* alarm may have happened. */ continue; } bb_perror_msg_and_die("select error"); } if (FD_ISSET(sock_fd, &fds)) { int i; RESERVE_CONFIG_BUFFER(tmpbuf, MAXLINE + 1); memset(tmpbuf, '\0', MAXLINE + 1); if ((i = recv(sock_fd, tmpbuf, MAXLINE, 0)) > 0) { serveConnection(tmpbuf, i); } else { bb_perror_msg_and_die("UNIX socket error"); } RELEASE_CONFIG_BUFFER(tmpbuf); } /* FD_ISSET() */ } /* for main loop */ }