This patch splits the code in two parts: 1) main(): contains option parsing, process privileges setting and daemonization 2) tcsd_main(): contains socket setup and the main loop where incoming connections are accepted.
This is useful in order to prepare the code to be ported on windows. This split is required because tcsd_main() will be the function executed when the tcsd windows service is started. Signed-off-by: Roberto Sassu <[email protected]> --- src/tcsd/svrside.c | 124 +++++++++++++++++++++++++++++---------------------- 1 files changed, 70 insertions(+), 54 deletions(-) diff --git a/src/tcsd/svrside.c b/src/tcsd/svrside.c index 6641800..4600e6d 100644 --- a/src/tcsd/svrside.c +++ b/src/tcsd/svrside.c @@ -43,7 +43,7 @@ struct tcsd_config tcsd_options; struct tpm_properties tpm_metrics; static volatile int hup = 0, term = 0; extern char *optarg; -int sd; +int sd = -1; static void tcsd_shutdown(void) @@ -63,6 +63,7 @@ tcsd_signal_term(int signal) { term = 1; close(sd); + sd = -1; } void @@ -210,47 +211,25 @@ reload_config(void) } -int -main(int argc, char **argv) +void +tcsd_main(int argc, char **argv) { struct sockaddr_in serv_addr, client_addr; TSS_RESULT result; - int newsd, c, option_index = 0; + int newsd, c; socklen_t client_len; char *hostname = NULL; - struct passwd *pwd; struct hostent *client_hostent = NULL; - struct option long_options[] = { - {"help", 0, NULL, 'h'}, - {"foreground", 0, NULL, 'f'}, - {0, 0, 0, 0} - }; - - unsetenv("TCSD_USE_TCP_DEVICE"); - while ((c = getopt_long(argc, argv, "fhe", long_options, &option_index)) != -1) { - switch (c) { - case 'f': - setenv("TCSD_FOREGROUND", "1", 1); - break; - case 'h': - /* fall through */ - case 'e': - setenv("TCSD_USE_TCP_DEVICE", "1", 1); - break; - default: - usage(); - return -1; - break; - } + + if ((result = tcsd_startup())) { + LogError("tcsd_startup failed: %d", result); + goto outlast; } - if ((result = tcsd_startup())) - return (int)result; - sd = socket(AF_INET, SOCK_STREAM, 0); if (sd < 0) { LogError("Failed socket: %s", strerror(errno)); - return -1; + goto out; } memset(&serv_addr, 0, sizeof (serv_addr)); @@ -268,34 +247,14 @@ main(int argc, char **argv) setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &c, sizeof(c)); if (bind(sd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) { LogError("Failed bind: %s", strerror(errno)); - return -1; - } -#ifndef SOLARIS - pwd = getpwnam(TSS_USER_NAME); - if (pwd == NULL) { - if (errno == 0) { - LogError("User \"%s\" not found, please add this user" - " manually.", TSS_USER_NAME); - } else { - LogError("getpwnam(%s): %s", TSS_USER_NAME, strerror(errno)); - } - return TCSERR(TSS_E_INTERNAL_ERROR); + goto out; } - setuid(pwd->pw_uid); -#endif + if (listen(sd, TCSD_MAX_SOCKETS_QUEUED) < 0) { LogError("Failed listen: %s", strerror(errno)); - return -1; + goto out; } client_len = (unsigned)sizeof(client_addr); - - if (getenv("TCSD_FOREGROUND") == NULL) { - if (daemon(0, 0) == -1) { - perror("daemon"); - tcsd_shutdown(); - return -1; - } - } LogInfo("%s: TCSD up and running.", PACKAGE_STRING); do { @@ -341,6 +300,63 @@ main(int argc, char **argv) } while (term ==0); /* To close correctly, we must receive a SIGTERM */ +out: + if (sd != -1) + close(sd); tcsd_shutdown(); +outlast: + return; +} + +int +main(int argc, char **argv) +{ + int c, option_index = 0; + struct passwd *pwd; + + struct option long_options[] = { + {"help", 0, NULL, 'h'}, + {"foreground", 0, NULL, 'f'}, + {0, 0, 0, 0} + }; + + unsetenv("TCSD_USE_TCP_DEVICE"); + while ((c = getopt_long(argc, argv, "fhe", long_options, &option_index)) != -1) { + switch (c) { + case 'f': + setenv("TCSD_FOREGROUND", "1", 1); + break; + case 'h': + /* fall through */ + case 'e': + setenv("TCSD_USE_TCP_DEVICE", "1", 1); + break; + default: + usage(); + return -1; + break; + } + } + +#ifndef SOLARIS + pwd = getpwnam(TSS_USER_NAME); + if (pwd == NULL) { + if (errno == 0) { + LogError("User \"%s\" not found, please add this user" + " manually.", TSS_USER_NAME); + } else { + LogError("getpwnam(%s): %s", TSS_USER_NAME, strerror(errno)); + } + return TCSERR(TSS_E_INTERNAL_ERROR); + } + setuid(pwd->pw_uid); +#endif + if (getenv("TCSD_FOREGROUND") == NULL) { + if (daemon(0, 0) == -1) { + perror("daemon"); + return -1; + } + } + tcsd_main(0, NULL); return 0; } -- 1.7.2.2 ------------------------------------------------------------------------------ This SF.net Dev2Dev email is sponsored by: Show off your parallel programming skills. Enter the Intel(R) Threading Challenge 2010. http://p.sf.net/sfu/intel-thread-sfd _______________________________________________ TrouSerS-tech mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/trousers-tech
