Repository: valkey-io/valkey
File: src/aof.c (C)

Code before:
        server.aof_rewrite_time_start = time(NULL);
        server.aof_rewrite_use_rdb_preamble = server.aof_use_rdb_preamble;
        return C_OK;
    }
    return C_OK; /* unreached */
}

void bgrewriteaofCommand(client *c) {
    if (server.child_type == CHILD_TYPE_AOF) {
        addReplyError(c, "Background append only file rewriting already in progress");
    } else if (hasActiveChildProcess() || server.in_exec) {
        server.aof_rewrite_scheduled = 1;
        /* When manually triggering AOFRW we reset the count
         * so that it can be executed immediately. */
        server.stat_aofrw_consecutive_failures = 0;
        serverLog(LL_NOTICE, "Background append only file rewriting scheduled.");
        addReplyStatus(c, "Background append only file rewriting scheduled");
    } else if (rewriteAppendOnlyFileBackground() == C_OK) {
        addReplyStatus(c, "Background append only file rewriting started");
    } else {
        addReplyError(c, "Can't execute an AOF background rewriting. "
                         "Please check the server logs for more information.");
    }
}

/* Note that we may call this function in signal handle 'sigShutdownHandler',
 * so we need guarantee all functions we call are async-signal-safe.
 * If we call this function from signal handle, we won't call bg_unlink that
 * is not async-signal-safe. */
void aofRemoveTempFile(pid_t childpid, int from_signal) {
    char tmpfile[256];
    char tmpfile2[256];
    char pid[32];

    /* Generate temp aof file name using aync-signal safe functions. */
    ll2string(pid, sizeof(pid), childpid);
    valkey_strlcpy(tmpfile, "temp-rewriteaof-bg-", sizeof(tmpfile));
    valkey_strlcat(tmpfile, pid, sizeof(tmpfile));
    valkey_strlcat(tmpfile, ".aof", sizeof(tmpfile));
    valkey_strlcpy(tmpfile2, "temp-rewriteaof-", sizeof(tmpfile2));
    valkey_strlcat(tmpfile2, pid, sizeof(tmpfile2));
    valkey_strlcat(tmpfile2, ".aof", sizeof(tmpfile2));

    if (from_signal) {
        /* bg_unlink is not async-signal-safe, but in this case we don't really
         * need to close the fd, it'll be released when the process exists. */
        int fd = open(tmpfile, O_RDONLY | O_NONBLOCK);
        int fd2 = open(tmpfile2, O_RDONLY | O_NONBLOCK);
        UNUSED(fd);
        UNUSED(fd2);
        unlink(tmpfile);

Review comment (suggestion):
```suggestion
/* Note that we may call this function in signal handler 'sigShutdownHandler',
```

Code after:
        server.aof_rewrite_time_start = time(NULL);
        server.aof_rewrite_use_rdb_preamble = server.aof_use_rdb_preamble;
        return C_OK;
    }
    return C_OK; /* unreached */
}

void bgrewriteaofCommand(client *c) {
    if (server.child_type == CHILD_TYPE_AOF) {
        addReplyError(c, "Background append only file rewriting already in progress");
    } else if (hasActiveChildProcess() || server.in_exec) {
        server.aof_rewrite_scheduled = 1;
        /* When manually triggering AOFRW we reset the count
         * so that it can be executed immediately. */
        server.stat_aofrw_consecutive_failures = 0;
        serverLog(LL_NOTICE, "Background append only file rewriting scheduled.");
        addReplyStatus(c, "Background append only file rewriting scheduled");
    } else if (rewriteAppendOnlyFileBackground() == C_OK) {
        addReplyStatus(c, "Background append only file rewriting started");
    } else {
        addReplyError(c, "Can't execute an AOF background rewriting. "
                         "Please check the server logs for more information.");
    }
}

/* Note that we may call this function in signal handler 'sigShutdownHandler',
 * so we need guarantee all functions we call are async-signal-safe.
 * If we call this function from signal handle, we won't call bg_unlink that
 * is not async-signal-safe. */
void aofRemoveTempFile(pid_t childpid, int from_signal) {
    char tmpfile[256];
    char tmpfile2[256];
    char pid[32];

    /* Generate temp aof file name using async-signal safe functions. */
    ll2string(pid, sizeof(pid), childpid);
    valkey_strlcpy(tmpfile, "temp-rewriteaof-bg-", sizeof(tmpfile));
    valkey_strlcat(tmpfile, pid, sizeof(tmpfile));
    valkey_strlcat(tmpfile, ".aof", sizeof(tmpfile));
    valkey_strlcpy(tmpfile2, "temp-rewriteaof-", sizeof(tmpfile2));
    valkey_strlcat(tmpfile2, pid, sizeof(tmpfile2));
    valkey_strlcat(tmpfile2, ".aof", sizeof(tmpfile2));

    if (from_signal) {
        /* bg_unlink is not async-signal-safe, but in this case we don't really
         * need to close the fd, it'll be released when the process exists. */
        int fd = open(tmpfile, O_RDONLY | O_NONBLOCK);
        int fd2 = open(tmpfile2, O_RDONLY | O_NONBLOCK);
        UNUSED(fd);
        UNUSED(fd2);
        unlink(tmpfile);