Created
April 1, 2026 02:20
-
-
Save echoechoin/5611bd71c8ffe546f548f753ff238a5c to your computer and use it in GitHub Desktop.
lastlog2-parallel.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* SPDX-License-Identifier: BSD-2-Clause | |
| Copyright (c) 2026 | |
| Redistribution and use in source and binary forms, with or without | |
| modification, are permitted provided that the following conditions are met: | |
| 1. Redistributions of source code must retain the above copyright notice, | |
| this list of conditions and the following disclaimer. | |
| 2. Redistributions in binary form must reproduce the above copyright | |
| notice, this list of conditions and the following disclaimer in the | |
| documentation and/or other materials provided with the distribution. | |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| #include <errno.h> | |
| #include <getopt.h> | |
| #include <inttypes.h> | |
| #include <signal.h> | |
| #include <stdbool.h> | |
| #include <stdint.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/types.h> | |
| #include <sys/wait.h> | |
| #include <time.h> | |
| #include <unistd.h> | |
| #include "c.h" | |
| #include "lastlog2.h" | |
| #include "nls.h" | |
| #include "strutils.h" | |
| struct child_result { | |
| uint64_t success; | |
| uint64_t failure; | |
| }; | |
| static void __attribute__((__noreturn__)) usage(void) | |
| { | |
| FILE *output = stdout; | |
| fputs(USAGE_HEADER, output); | |
| fprintf(output, _(" %s [options]\n"), program_invocation_short_name); | |
| fputs(USAGE_OPTIONS, output); | |
| fputs(_(" -d, --database FILE use FILE as lastlog2 database\n"), output); | |
| fputs(_(" -p, --processes NUM run NUM worker processes\n"), output); | |
| fputs(_(" -n, --rounds NUM execute NUM write rounds per process\n"), output); | |
| fputs(USAGE_SEPARATOR, output); | |
| fprintf(output, USAGE_HELP_OPTIONS(25)); | |
| fprintf(output, USAGE_MAN_TAIL("lastlog2-parallel(8)")); | |
| exit(EXIT_SUCCESS); | |
| } | |
| static int | |
| wait_for_start(int fd) | |
| { | |
| char token; | |
| ssize_t rc; | |
| do { | |
| rc = read(fd, &token, 1); | |
| } while (rc < 0 && errno == EINTR); | |
| return rc == 1 ? 0 : -1; | |
| } | |
| static int | |
| write_full(int fd, const void *buf, size_t size) | |
| { | |
| const char *ptr = buf; | |
| while (size > 0) { | |
| ssize_t rc = write(fd, ptr, size); | |
| if (rc < 0) { | |
| if (errno == EINTR) | |
| continue; | |
| return -1; | |
| } | |
| ptr += rc; | |
| size -= rc; | |
| } | |
| return 0; | |
| } | |
| static int | |
| read_full(int fd, void *buf, size_t size) | |
| { | |
| char *ptr = buf; | |
| while (size > 0) { | |
| ssize_t rc = read(fd, ptr, size); | |
| if (rc < 0) { | |
| if (errno == EINTR) | |
| continue; | |
| return -1; | |
| } | |
| if (rc == 0) | |
| return -1; | |
| ptr += rc; | |
| size -= rc; | |
| } | |
| return 0; | |
| } | |
| static int | |
| run_child(const char *db_path, unsigned long child_idx, unsigned long rounds, | |
| int start_fd, int result_fd) | |
| { | |
| struct ll2_context *context; | |
| struct child_result result = { 0, 0 }; | |
| char user[96]; | |
| char tty[32]; | |
| char rhost[32]; | |
| char service[32]; | |
| char *error = NULL; | |
| unsigned long round; | |
| int rc = EXIT_FAILURE; | |
| context = ll2_new_context(db_path); | |
| if (context == NULL) { | |
| warnx(_("failed to create lastlog2 context")); | |
| goto out; | |
| } | |
| if (wait_for_start(start_fd) != 0) { | |
| warn(_("failed to wait for start signal")); | |
| goto out_ctx; | |
| } | |
| snprintf(tty, sizeof(tty), "pts/%lu", child_idx); | |
| snprintf(rhost, sizeof(rhost), "host-%lu", child_idx); | |
| for (round = 0; round < rounds; round++) { | |
| int64_t login_time; | |
| login_time = (int64_t) time(NULL) + child_idx * rounds + round; | |
| snprintf(user, sizeof(user), "parallel-user-%lu-%lu", child_idx, round); | |
| snprintf(service, sizeof(service), "round-%lu", round); | |
| if (ll2_write_entry(context, user, login_time, tty, rhost, service, &error) == 0) { | |
| result.success++; | |
| continue; | |
| } | |
| result.failure++; | |
| if (error != NULL) { | |
| warnx(_("write failed for %s: %s"), user, error); | |
| free(error); | |
| error = NULL; | |
| } | |
| } | |
| if (write_full(result_fd, &result, sizeof(result)) != 0) | |
| warn(_("failed to report child result")); | |
| else | |
| rc = result.failure == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
| out_ctx: | |
| ll2_unref_context(context); | |
| out: | |
| close(start_fd); | |
| close(result_fd); | |
| return rc; | |
| } | |
| int | |
| main(int argc, char **argv) | |
| { | |
| static const struct option longopts[] = { | |
| { "database", required_argument, NULL, 'd' }, | |
| { "help", no_argument, NULL, 'h' }, | |
| { "processes", required_argument, NULL, 'p' }, | |
| { "rounds", required_argument, NULL, 'n' }, | |
| { "version", no_argument, NULL, 'V' }, | |
| { NULL, 0, NULL, '\0' } | |
| }; | |
| const char *db_path = "tst-parallel-write.db"; | |
| unsigned long processes = 8; | |
| unsigned long rounds = 16; | |
| pid_t *children; | |
| int (*start_pipes)[2]; | |
| int (*result_pipes)[2]; | |
| uint64_t total_success = 0; | |
| uint64_t total_failure = 0; | |
| int c; | |
| unsigned long idx; | |
| int rc = EXIT_SUCCESS; | |
| while ((c = getopt_long(argc, argv, "d:hn:p:V", longopts, NULL)) != -1) { | |
| switch (c) { | |
| case 'd': | |
| db_path = optarg; | |
| break; | |
| case 'h': | |
| usage(); | |
| break; | |
| case 'n': | |
| rounds = strtoul_or_err(optarg, _("invalid rounds")); | |
| break; | |
| case 'p': | |
| processes = strtoul_or_err(optarg, _("invalid processes")); | |
| break; | |
| case 'V': | |
| print_version(EXIT_SUCCESS); | |
| break; | |
| default: | |
| errtryhelp(EXIT_FAILURE); | |
| } | |
| } | |
| if (processes == 0) | |
| errx(EXIT_FAILURE, _("processes must be greater than zero")); | |
| if (rounds == 0) | |
| errx(EXIT_FAILURE, _("rounds must be greater than zero")); | |
| children = calloc(processes, sizeof(*children)); | |
| start_pipes = calloc(processes, sizeof(*start_pipes)); | |
| result_pipes = calloc(processes, sizeof(*result_pipes)); | |
| if (children == NULL || start_pipes == NULL || result_pipes == NULL) | |
| err(EXIT_FAILURE, _("memory allocation failed")); | |
| for (idx = 0; idx < processes; idx++) { | |
| children[idx] = -1; | |
| start_pipes[idx][0] = -1; | |
| start_pipes[idx][1] = -1; | |
| result_pipes[idx][0] = -1; | |
| result_pipes[idx][1] = -1; | |
| } | |
| for (idx = 0; idx < processes; idx++) { | |
| if (pipe(start_pipes[idx]) != 0) | |
| err(EXIT_FAILURE, _("pipe failed")); | |
| if (pipe(result_pipes[idx]) != 0) | |
| err(EXIT_FAILURE, _("pipe failed")); | |
| children[idx] = fork(); | |
| if (children[idx] < 0) | |
| err(EXIT_FAILURE, _("fork failed")); | |
| if (children[idx] == 0) { | |
| close(start_pipes[idx][1]); | |
| close(result_pipes[idx][0]); | |
| _exit(run_child(db_path, idx, rounds, start_pipes[idx][0], result_pipes[idx][1])); | |
| } | |
| close(start_pipes[idx][0]); | |
| start_pipes[idx][0] = -1; | |
| close(result_pipes[idx][1]); | |
| result_pipes[idx][1] = -1; | |
| } | |
| for (idx = 0; idx < processes; idx++) { | |
| if (write_full(start_pipes[idx][1], "", 1) != 0) | |
| err(EXIT_FAILURE, _("failed to release child")); | |
| close(start_pipes[idx][1]); | |
| start_pipes[idx][1] = -1; | |
| } | |
| for (idx = 0; idx < processes; idx++) { | |
| struct child_result result; | |
| int status; | |
| if (read_full(result_pipes[idx][0], &result, sizeof(result)) != 0) | |
| err(EXIT_FAILURE, _("failed to collect child result")); | |
| close(result_pipes[idx][0]); | |
| result_pipes[idx][0] = -1; | |
| total_success += result.success; | |
| total_failure += result.failure; | |
| printf("process[%lu] success=%" PRIu64 " failure=%" PRIu64 "\n", | |
| idx, result.success, result.failure); | |
| if (waitpid(children[idx], &status, 0) < 0) | |
| err(EXIT_FAILURE, _("waitpid failed")); | |
| children[idx] = -1; | |
| if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) | |
| rc = EXIT_FAILURE; | |
| } | |
| printf("success=%" PRIu64 " failure=%" PRIu64 "\n", | |
| total_success, total_failure); | |
| if (total_failure > 0) | |
| rc = EXIT_FAILURE; | |
| free(result_pipes); | |
| free(start_pipes); | |
| free(children); | |
| return rc; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment