From 66db6e7063af6f44e5ffabae529655db2bbe6530 Mon Sep 17 00:00:00 2001 From: Simo Piiroinen Date: Tue, 13 Apr 2021 10:50:31 +0300 Subject: [PATCH] [mapplauncherd] Cleanup compilation warnings Comparing signed vs unsigned integers. Unused static data. Questionable variable declarations. Const correctness issues. Unchecked socket and pipe i/o. Unchecked chdir() call. String sender that silently skips null strings while protocol does not make it possible for receiver to detect such omissions. Signed-off-by: Simo Piiroinen --- src/common/report.h | 6 ++- src/invoker/invokelib.c | 38 +++++++++------ src/invoker/invokelib.h | 6 ++- src/invoker/invoker.c | 15 +++--- src/launcherlib/booster.cpp | 17 +++++-- src/launcherlib/connection.cpp | 7 +-- src/launcherlib/daemon.cpp | 88 +++++++++++++++++----------------- src/launcherlib/daemon.h | 9 ++-- src/launcherlib/logger.cpp | 12 +++-- 9 files changed, 112 insertions(+), 86 deletions(-) diff --git a/src/common/report.h b/src/common/report.h index 08858f4..85b3a2f 100644 --- a/src/common/report.h +++ b/src/common/report.h @@ -1,6 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2021 Open Mobile Platform LLC. +** Copyright (c) 2021 Jolla Ltd. ** All rights reserved. ** Contact: Nokia Corporation (directui@nokia.com) ** @@ -46,7 +48,7 @@ extern void report(enum report_type type, const char *msg, ...); #ifndef DEBUG_LOGGING_DISABLED #define debug(msg, ...) report(report_debug, msg, ##__VA_ARGS__) #else -#define debug(...) +#define debug(...) do {} while (0) #endif #define info(msg, ...) report(report_info, msg, ##__VA_ARGS__) diff --git a/src/invoker/invokelib.c b/src/invoker/invokelib.c index fb516c7..2de5013 100644 --- a/src/invoker/invokelib.c +++ b/src/invoker/invokelib.c @@ -1,6 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2021 Open Mobile Platform LLC. +** Copyright (c) 2021 Jolla Ltd. ** All rights reserved. ** Contact: Nokia Corporation (directui@nokia.com) ** @@ -26,10 +28,21 @@ #include "report.h" #include "invokelib.h" +static void invoke_send_or_die(int fd, const void *data, size_t size) +{ + if (write(fd, data, size) != (ssize_t)size) { + const char m[] = "*** socket write failure, terminating\n"; + if (write(STDERR_FILENO, m, sizeof m - 1) == -1) { + // dontcare + } + _exit(EXIT_FAILURE); + } +} + void invoke_send_msg(int fd, uint32_t msg) { debug("%s: %08x\n", __FUNCTION__, msg); - write(fd, &msg, sizeof(msg)); + invoke_send_or_die(fd, &msg, sizeof(msg)); } bool invoke_recv_msg(int fd, uint32_t *msg) @@ -58,20 +71,17 @@ bool invoke_recv_msg(int fd, uint32_t *msg) } } -void invoke_send_str(int fd, char *str) +void invoke_send_str(int fd, const char *str) { - if (str) - { - uint32_t size; + if (!str) + str = ""; + uint32_t size = strlen(str) + 1; - /* Send size. */ - size = strlen(str) + 1; - invoke_send_msg(fd, size); + /* Send size. */ + invoke_send_msg(fd, size); - debug("%s: '%s'\n", __FUNCTION__, str); + debug("%s: '%s'\n", __FUNCTION__, str); - /* Send the string. */ - write(fd, str, size); - } + /* Send the string. */ + invoke_send_or_die(fd, str, size); } - diff --git a/src/invoker/invokelib.h b/src/invoker/invokelib.h index 615799a..eb1e429 100644 --- a/src/invoker/invokelib.h +++ b/src/invoker/invokelib.h @@ -1,6 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2012 - 2021 Jolla Ltd. +** Copyright (c) 2021 Open Mobile Platform LLC. ** All rights reserved. ** Contact: Nokia Corporation (directui@nokia.com) ** @@ -25,7 +27,7 @@ void invoke_send_msg(int fd, uint32_t msg); bool invoke_recv_msg(int fd, uint32_t *msg); -void invoke_send_str(int fd, char *str); +void invoke_send_str(int fd, const char *str); // Existence of the test mode control file is checked // to enable test mode. diff --git a/src/invoker/invoker.c b/src/invoker/invoker.c index 873a082..67b9de6 100644 --- a/src/invoker/invoker.c +++ b/src/invoker/invoker.c @@ -1,6 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2012 - 2021 Jolla Ltd. +** Copyright (c) 2021 Open Mobile Platform LLC. ** All rights reserved. ** Contact: Nokia Corporation (directui@nokia.com) ** @@ -55,7 +57,6 @@ static const unsigned int RESPAWN_DELAY = 1; static const unsigned int MIN_RESPAWN_DELAY = 0; static const unsigned int MAX_RESPAWN_DELAY = 10; -static const unsigned char EXIT_STATUS_APPLICATION_CONNECTION_LOST = 0xfa; static const unsigned char EXIT_STATUS_APPLICATION_NOT_FOUND = 0x7f; // Environment @@ -99,7 +100,8 @@ static void sig_forwarder(int sig) // Write signal number to the self-pipe char signal_id = (char) sig; - write(g_signal_pipe[1], &signal_id, 1); + if (write(g_signal_pipe[1], &signal_id, 1) != 1) + _exit(EXIT_FAILURE); // Send the signal to itself using the default handler raise(sig); @@ -208,7 +210,7 @@ static int invoker_init(const char *app_type) strcat(sun.sun_path, subpath); maxSize -= strlen(sun.sun_path); - if (maxSize < strlen(app_type) || strchr(app_type, '/')) + if (maxSize < (int)strlen(app_type) || strchr(app_type, '/')) die(1, "Invalid type of application: %s\n", app_type); strcat(sun.sun_path, app_type); @@ -274,7 +276,7 @@ static void invoker_send_magic(int fd, uint32_t options) } // Sends the process name to be invoked. -static void invoker_send_name(int fd, char *name) +static void invoker_send_name(int fd, const char *name) { invoke_send_msg(fd, INVOKER_MSG_NAME); invoke_send_str(fd, name); @@ -546,7 +548,8 @@ static int wait_for_launched_process_to_exit(int socket_fd, bool wait_term) { // Clean up the pipe char signal_id; - read(g_signal_pipe[0], &signal_id, sizeof(signal_id)); + if (read(g_signal_pipe[0], &signal_id, 1) != 1) + exit(EXIT_FAILURE); // Set signals forwarding to the invoked process again // (they were reset by the signal forwarder). diff --git a/src/launcherlib/booster.cpp b/src/launcherlib/booster.cpp index aa049fe..c71c330 100644 --- a/src/launcherlib/booster.cpp +++ b/src/launcherlib/booster.cpp @@ -1,8 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2013 - 2021 Jolla Ltd. -** Copyright (C) 2018 - 2020 Open Mobile Platform LLC. +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2013 - 2021 Jolla Ltd. +** Copyright (c) 2018 - 2021 Open Mobile Platform LLC. ** All rights reserved. ** Contact: Nokia Corporation (directui@nokia.com) ** @@ -478,7 +478,16 @@ void Booster::setEnvironmentBeforeLaunch() // Set PWD const char * pwd = getenv("PWD"); - if (pwd) chdir(pwd); + if (pwd) { + if (chdir(pwd) == -1) { + Logger::logWarning("Booster: chdir(\"%s\") failed: %m", pwd); + pwd = "/"; + if (chdir(pwd) == -1) { + Logger::logWarning("Booster: chdir(\"%s\") failed: %m", pwd); + exit(EXIT_FAILURE); + } + } + } Logger::logDebug("Booster: launching process: '%s' ", m_appData->fileName().c_str()); } diff --git a/src/launcherlib/connection.cpp b/src/launcherlib/connection.cpp index 7f08c03..a883885 100644 --- a/src/launcherlib/connection.cpp +++ b/src/launcherlib/connection.cpp @@ -1,7 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2021 Jolla Ltd. +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2021 Jolla Ltd. +** Copyright (c) 2021 Open Mobile Platform LLC. ** All rights reserved. ** Contact: Nokia Corporation (directui@nokia.com) ** @@ -620,7 +621,7 @@ std::string Connection::getExecutablePath(pid_t pid) static_assert(sizeof(INVOKER_PATH) < sizeof(exe)); path << "/proc/" << pid << "/exe"; len = readlink(path.str().c_str(), exe, sizeof(exe)); - if (len < 0 || len >= sizeof(exe)) + if (len < 0 || (size_t)len >= sizeof(exe)) { Logger::logError("Connection: %s: readlink failed for pid %d: %s", __FUNCTION__, pid, strerror(errno)); return std::string(); diff --git a/src/launcherlib/daemon.cpp b/src/launcherlib/daemon.cpp index 13fefdf..b6c021f 100644 --- a/src/launcherlib/daemon.cpp +++ b/src/launcherlib/daemon.cpp @@ -1,8 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2013 - 2021 Jolla Ltd. -** Copyright (C) 2020 Open Mobile Platform LLC. +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2013 - 2021 Jolla Ltd. +** Copyright (c) 2020 - 2021 Open Mobile Platform LLC. ** All rights reserved. ** Contact: Nokia Corporation (directui@nokia.com) ** @@ -53,43 +53,39 @@ extern char ** environ; Daemon * Daemon::m_instance = NULL; const int Daemon::m_boosterSleepTime = 2; -const std::string Daemon::m_stateDir = std::string(getenv("XDG_RUNTIME_DIR"))+"/applauncherd"; -const std::string Daemon::m_stateFile = Daemon::m_stateDir + "/saved-state"; - -static void sigChldHandler(int) -{ - char v = SIGCHLD; - write(Daemon::instance()->sigPipeFd(), &v, 1); -} - -static void sigTermHandler(int) +static void write_dontcare(int fd, const void *data, size_t size) { - char v = SIGTERM; - write(Daemon::instance()->sigPipeFd(), &v, 1); + ssize_t rc = write(fd, data, size); + if (rc == -1) + Logger::logWarning("write to fd=%d failed: %m", fd); + else if ((size_t)rc != size) + Logger::logWarning("write to fd=%d failed", fd); } -static void sigUsr1Handler(int) +static void write_to_signal_pipe(int sig) { - char v = SIGUSR1; - write(Daemon::instance()->sigPipeFd(), &v, 1); -} - -static void sigUsr2Handler(int) -{ - char v = SIGUSR2; - write(Daemon::instance()->sigPipeFd(), &v, 1); -} - -static void sigPipeHandler(int) -{ - char v = SIGPIPE; - write(Daemon::instance()->sigPipeFd(), &v, 1); + char v = (char)sig; + if (write(Daemon::instance()->sigPipeFd(), &v, 1) != 1) { + /* If we can't write to internal signal forwarding + * pipe, we might as well quit */ + const char m[] = "*** signal pipe write failure - terminating\n"; + if (write(STDERR_FILENO, m, sizeof m - 1) == -1) { + // dontcare + } + _exit(EXIT_FAILURE); + } } -static void sigHupHandler(int) +static int read_from_signal_pipe(int fd) { - char v = SIGHUP; - write(Daemon::instance()->sigPipeFd(), &v, 1); + char sig = 0; + if (read(fd, &sig, 1) != 1) { + /* If we can't read from internal signal forwarding + * pipe, we might as well quit */ + Logger::logError("signal pipe read failure - terminating\n"); + exit(EXIT_FAILURE); + } + return sig; } Daemon::Daemon(int & argc, char * argv[]) : @@ -108,12 +104,12 @@ Daemon::Daemon(int & argc, char * argv[]) : // Install signal handlers. The original handlers are saved // in the daemon instance so that they can be restored in boosters. - setUnixSignalHandler(SIGCHLD, sigChldHandler); // reap zombies - setUnixSignalHandler(SIGTERM, sigTermHandler); // exit launcher - setUnixSignalHandler(SIGUSR1, sigUsr1Handler); // enter normal mode from boot mode - setUnixSignalHandler(SIGUSR2, sigUsr2Handler); // enter boot mode (same as --boot-mode) - setUnixSignalHandler(SIGPIPE, sigPipeHandler); // broken invoker's pipe - setUnixSignalHandler(SIGHUP, sigHupHandler); // re-exec + setUnixSignalHandler(SIGCHLD, write_to_signal_pipe); // reap zombies + setUnixSignalHandler(SIGTERM, write_to_signal_pipe); // exit launcher + setUnixSignalHandler(SIGUSR1, write_to_signal_pipe); // enter normal mode from boot mode + setUnixSignalHandler(SIGUSR2, write_to_signal_pipe); // enter boot mode (same as --boot-mode) + setUnixSignalHandler(SIGPIPE, write_to_signal_pipe); // broken invoker's pipe + setUnixSignalHandler(SIGHUP, write_to_signal_pipe); // re-exec if (!Daemon::m_instance) { @@ -210,8 +206,7 @@ void Daemon::run(Booster *booster) if (FD_ISSET(m_sigPipeFd[0], &rfds)) { Logger::logDebug("Daemon: FD_ISSET(m_sigPipeFd[0])"); - char dataReceived; - read(m_sigPipeFd[0], &dataReceived, 1); + int dataReceived = read_from_signal_pipe(m_sigPipeFd[0]); switch (dataReceived) { @@ -380,6 +375,10 @@ void Daemon::forkBooster(int sleepTime) if (newPid == 0) /* Child process */ { + // Will be reopened with new identity when/if + // there is something to report + Logger::closeLog(); + // Restore used signal handlers restoreUnixSignalHandlers(); @@ -483,9 +482,10 @@ void Daemon::reapZombies() FdMap::iterator fd = m_boosterPidToInvokerFd.find(pid); if (fd != m_boosterPidToInvokerFd.end()) { - write((*fd).second, &INVOKER_MSG_EXIT, sizeof(uint32_t)); - int exitStatus = WEXITSTATUS(status); - write((*fd).second, &exitStatus, sizeof(int)); + uint32_t command = INVOKER_MSG_EXIT; + uint32_t argument = WEXITSTATUS(status); + write_dontcare((*fd).second, &command, sizeof command); + write_dontcare((*fd).second, &argument, sizeof argument); close((*fd).second); m_boosterPidToInvokerFd.erase(fd); } diff --git a/src/launcherlib/daemon.h b/src/launcherlib/daemon.h index 0f7c4db..d0838ec 100644 --- a/src/launcherlib/daemon.h +++ b/src/launcherlib/daemon.h @@ -1,7 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** Copyright (C) 2013 - 2021 Jolla Ltd. +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2013 - 2021 Jolla Ltd. +** Copyright (c) 2021 Open Mobile Platform LLC. ** All rights reserved. ** Contact: Nokia Corporation (directui@nokia.com) ** @@ -212,10 +213,6 @@ private: //! Booster instance Booster * m_booster; - //! Name of the state saving directory and file - static const std::string m_stateDir; - static const std::string m_stateFile; - #ifdef UNIT_TEST friend class Ut_Daemon; #endif diff --git a/src/launcherlib/logger.cpp b/src/launcherlib/logger.cpp index 1b8cc67..f2f6aa9 100644 --- a/src/launcherlib/logger.cpp +++ b/src/launcherlib/logger.cpp @@ -1,6 +1,8 @@ /*************************************************************************** ** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (c) 2013 - 2021 Jolla Ltd. +** Copyright (c) 2021 Open Mobile Platform LLC. ** All rights reserved. ** Contact: Nokia Corporation (directui@nokia.com) ** @@ -73,7 +75,7 @@ void Logger::logDebug(const char * format, ...) { if (m_debugMode) { - va_list(ap); + va_list ap; va_start(ap, format); writeLog(LOG_DEBUG, format, ap); va_end(ap); @@ -82,7 +84,7 @@ void Logger::logDebug(const char * format, ...) void Logger::logInfo(const char * format, ...) { - va_list(ap); + va_list ap; va_start(ap, format); writeLog(LOG_INFO, format, ap); va_end(ap); @@ -92,7 +94,7 @@ void Logger::logInfo(const char * format, ...) void Logger::logWarning(const char * format, ...) { - va_list(ap); + va_list ap; va_start(ap, format); writeLog(LOG_WARNING, format, ap); va_end(ap); @@ -100,7 +102,7 @@ void Logger::logWarning(const char * format, ...) void Logger::logError(const char * format, ...) { - va_list(ap); + va_list ap; va_start(ap, format); writeLog(LOG_ERR, format, ap); va_end(ap);