mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-07-31 00:03:07 +03:00
test_server: Added an option to write PID to file
Using the added option it is possible to set a path to a file in which the server will write its PID. This can be used later to kill the server. Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
committed by
Andreas Schneider
parent
3aea2ad53f
commit
f529659f76
@ -61,6 +61,7 @@ struct arguments_st {
|
|||||||
|
|
||||||
char *config_file;
|
char *config_file;
|
||||||
bool with_global_config;
|
bool with_global_config;
|
||||||
|
char *pid_file;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void free_arguments(struct arguments_st *arguments)
|
static void free_arguments(struct arguments_st *arguments)
|
||||||
@ -85,6 +86,7 @@ static void free_arguments(struct arguments_st *arguments)
|
|||||||
SAFE_FREE(arguments->username);
|
SAFE_FREE(arguments->username);
|
||||||
SAFE_FREE(arguments->password);
|
SAFE_FREE(arguments->password);
|
||||||
SAFE_FREE(arguments->config_file);
|
SAFE_FREE(arguments->config_file);
|
||||||
|
SAFE_FREE(arguments->pid_file);
|
||||||
|
|
||||||
end:
|
end:
|
||||||
return;
|
return;
|
||||||
@ -401,6 +403,14 @@ static struct argp_option options[] = {
|
|||||||
.doc = "Set the pcap output file.",
|
.doc = "Set the pcap output file.",
|
||||||
.group = 0
|
.group = 0
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.name = "pid_file",
|
||||||
|
.key = 'i',
|
||||||
|
.arg = "FILE",
|
||||||
|
.flags = 0,
|
||||||
|
.doc = "The server will write its pid in this file, if provided.",
|
||||||
|
.group = 0
|
||||||
|
},
|
||||||
{
|
{
|
||||||
.name = "auth-methods",
|
.name = "auth-methods",
|
||||||
.key = 'a',
|
.key = 'a',
|
||||||
@ -500,6 +510,14 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state)
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'i':
|
||||||
|
arguments->pid_file = strdup(arg);
|
||||||
|
if (arguments->pid_file == NULL) {
|
||||||
|
fprintf(stderr, "Out of memory\n");
|
||||||
|
rc = ENOMEM;
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case 'k':
|
case 'k':
|
||||||
arguments->host_key = strdup(arg);
|
arguments->host_key = strdup(arg);
|
||||||
if (arguments->host_key == NULL) {
|
if (arguments->host_key == NULL) {
|
||||||
@ -598,6 +616,8 @@ static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};
|
|||||||
int main(UNUSED_PARAM(int argc), UNUSED_PARAM(char **argv))
|
int main(UNUSED_PARAM(int argc), UNUSED_PARAM(char **argv))
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
FILE *pid_file;
|
||||||
|
pid_t pid;
|
||||||
|
|
||||||
struct arguments_st arguments = {
|
struct arguments_st arguments = {
|
||||||
.address = NULL,
|
.address = NULL,
|
||||||
@ -611,6 +631,17 @@ int main(UNUSED_PARAM(int argc), UNUSED_PARAM(char **argv))
|
|||||||
argp_parse (&argp, argc, argv, 0, 0, &arguments);
|
argp_parse (&argp, argc, argv, 0, 0, &arguments);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (arguments.pid_file) {
|
||||||
|
pid_file = fopen(arguments.pid_file, "w");
|
||||||
|
if (pid_file == NULL) {
|
||||||
|
rc = -1;
|
||||||
|
goto free_arguments;
|
||||||
|
}
|
||||||
|
pid = getpid();
|
||||||
|
fprintf(pid_file, "%d\n", pid);
|
||||||
|
fclose(pid_file);
|
||||||
|
}
|
||||||
|
|
||||||
/* Initialize the state using default or given parameters */
|
/* Initialize the state using default or given parameters */
|
||||||
rc = init_server_state(&state, &arguments);
|
rc = init_server_state(&state, &arguments);
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
|
Reference in New Issue
Block a user