Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a0f19b3 authored by Mike McTernan's avatar Mike McTernan
Browse files

trusty:storageproxyd: try to exit gracefully on shutdown

Add a SIGTERM handler and exit when idle, rather than potentially in the
middle of a transaction.

Bug: 301591047
Test: com.android.storage-unittest.*
Test: VtsAidlKeyMintTargetTest "*TestSingleUseKeyAndRollbackResistance*default" repeatedly while calling reboot at different times.
Change-Id: Iffccf6190073f697c2a76288b5b5f293deef1a2a
parent 320aec9e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ int ipc_respond(struct storage_msg *msg, void *out, size_t out_size)

    msg->cmd |= STORAGE_RESP_BIT;

    rc = writev(tipc_fd, iovs, out ? 2 : 1);
    rc = TEMP_FAILURE_RETRY(writev(tipc_fd, iovs, out ? 2 : 1));
    if (rc < 0) {
        ALOGE("error sending response 0x%x: %s\n",
              msg->cmd, strerror(errno));
+25 −4
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
@@ -37,6 +38,8 @@
#define REQ_BUFFER_SIZE 4096
static uint8_t req_buffer[REQ_BUFFER_SIZE + 1];

static volatile sig_atomic_t terminate = false;

static const char* ss_data_root;
static const char* trusty_devname;
static const char* rpmb_devname;
@@ -120,6 +123,10 @@ static void show_usage_and_exit(int code) {
    exit(code);
}

static void handle_sigterm(int signum __attribute__((unused))) {
    terminate = true;
}

static int handle_req(struct storage_msg* msg, const void* req, size_t req_len) {
    int rc;

@@ -218,10 +225,14 @@ static int proxy_loop(void) {
    struct storage_msg msg;

    /* enter main message handling loop */
    while (true) {
    while (!terminate) {
        /* get incoming message */
        rc = ipc_get_msg(&msg, req_buffer, REQ_BUFFER_SIZE);
        if (rc < 0) return rc;
        if (rc == EINTR && terminate) {
            return 0;
        } else if (rc < 0) {
            return rc;
        }

        /* handle request */
        req_buffer[rc] = 0; /* force zero termination */
@@ -303,6 +314,12 @@ int main(int argc, char* argv[]) {
     */
    umask(S_IRWXG | S_IRWXO);

    /* catch SIGTERM for graceful shutdown */
    const struct sigaction sa = {
            .sa_handler = handle_sigterm,
    };
    sigaction(SIGTERM, &sa, NULL);

    /* parse arguments */
    parse_args(argc, argv);

@@ -332,10 +349,14 @@ int main(int argc, char* argv[]) {

    /* enter main loop */
    rc = proxy_loop();
    if (terminate) {
        ALOGI("proxy loop terminated with status (%d)\n", rc);
    } else {
        ALOGE("exiting proxy loop with status (%d)\n", rc);
    }

    ipc_disconnect();
    rpmb_close();

    return (rc < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
    _exit((rc < 0) ? EXIT_FAILURE : EXIT_SUCCESS);
}