diff --git a/sesman/Makefile.am b/sesman/Makefile.am index 8d9d31022c..8744405baa 100644 --- a/sesman/Makefile.am +++ b/sesman/Makefile.am @@ -32,6 +32,8 @@ xrdp_sesman_SOURCES = \ sesexec_control.h \ session_list.c \ session_list.h \ + sesman_restart.c \ + sesman_restart.h \ sig.c \ sig.h diff --git a/sesman/sesman.c b/sesman/sesman.c index 418429004a..bb812d5f02 100644 --- a/sesman/sesman.c +++ b/sesman/sesman.c @@ -46,6 +46,7 @@ #include "scp.h" #include "scp_process.h" #include "sesexec_control.h" +#include "sesman_restart.h" #include "sig.h" #include "string_calls.h" #include "trans.h" @@ -940,7 +941,8 @@ main(int argc, char **argv) } if ((error = pre_session_list_init(MAX_PRE_SESSION_ITEMS)) == 0 && - (error = session_list_init()) == 0) + (error = session_list_init()) == 0 && + (error = sesman_restart_discover_sessions()) == 0) { error = sesman_main_loop(); } diff --git a/sesman/sesman_restart.c b/sesman/sesman_restart.c new file mode 100644 index 0000000000..78ee8f24e9 --- /dev/null +++ b/sesman/sesman_restart.c @@ -0,0 +1,91 @@ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Matt Burt 2024 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * + * @file sesman_restart.c + * @brief Sesman restart definitions + * @author Matt Burt + * + */ + +#if defined(HAVE_CONFIG_H) +#include "config_ac.h" +#endif + +#include + +#include "os_calls.h" +#include "sesman.h" +#include "sesman_config.h" + +#include "sesman_restart.h" +#include "xrdp_sockets.h" + +/******************************************************************************/ +static int +create_restart_directory(const char *listen_port) +{ + int rv = 1; + // The restart directory contains Unix Domain sockets, so can't + // exceed XRDP_SOCKETS_MAXPATH in length + char restart_dir[XRDP_SOCKETS_MAXPATH]; + // Allow some overhead for the names of the directory entries +#define MAX_LISTEN_PORT_LEN (XRDP_SOCKETS_MAXPATH - 10) + + if (strlen(listen_port) > MAX_LISTEN_PORT_LEN) + { + LOG(LOG_LEVEL_ERROR, "Listen port string length must be <= %d", + MAX_LISTEN_PORT_LEN); + } + else + { + snprintf(restart_dir, sizeof(restart_dir), "%s.r", listen_port); + (void)g_mkdir(restart_dir); // Directory may already exist + if (!g_directory_exist(restart_dir)) + { + LOG(LOG_LEVEL_ERROR, "Can't create restart directory %s", + restart_dir); + } + else if (g_chown(restart_dir, g_getuid(), g_getuid()) != 0) + { + LOG(LOG_LEVEL_ERROR, "Can't set ownership of '%s' [%s]", + restart_dir, g_get_strerror()); + } + else if ((rv = g_chmod_hex(restart_dir, 0x700)) != 0) + { + LOG(LOG_LEVEL_ERROR, "%s: Can't set permissions on '%s' [%s]", + __func__, restart_dir, g_get_strerror()); + } + else + { + rv = 0; + } + } + + return rv; +#undef MAX_LISTEN_PORT_LEN +} + +/******************************************************************************/ +int +sesman_restart_discover_sessions(void) +{ + // Needs lots of work! + return create_restart_directory(g_cfg->listen_port); +} diff --git a/sesman/sesman_restart.h b/sesman/sesman_restart.h new file mode 100644 index 0000000000..bbfa11b49c --- /dev/null +++ b/sesman/sesman_restart.h @@ -0,0 +1,40 @@ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Matt Burt 2024 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * + * @file sesman_restart.h + * @brief Sesman restart declarations + * @author Matt Burt + * + */ + + +#ifndef SESMAN_RESTART_H +#define SESMAN_RESTART_H + +/** + * Discover sessions from a previous sesman run + * @return 0 for success + * + * Errors are logged + */ +int +sesman_restart_discover_sessions(void); + +#endif // SESMAN_RESTART_H