From: Baptiste Daroussin Date: Wed, 28 Dec 2022 15:42:12 +0000 (+0100) Subject: Rewrite find_subscriber using getline(3) instead of mmap X-Git-Tag: RELEASE_1_4_0_a2~48 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c0d96e51db53a0cc18d0e1d9bdffbccfc5c5b9da;p=thirdparty%2Fmlmmj.git Rewrite find_subscriber using getline(3) instead of mmap It simplifies greatly the code --- diff --git a/include/subscriberfuncs.h b/include/subscriberfuncs.h index a81b5a69..ef7f5d04 100644 --- a/include/subscriberfuncs.h +++ b/include/subscriberfuncs.h @@ -26,7 +26,7 @@ #include -off_t find_subscriber(int fd, const char *address); +bool find_subscriber(int fd, const char *address); int is_subbed_in(int fd, const char *subddirname, const char *address); enum subtype is_subbed(int listfd, const char *address, bool both); diff --git a/src/subscriberfuncs.c b/src/subscriberfuncs.c index 798960b3..13a9d09b 100644 --- a/src/subscriberfuncs.c +++ b/src/subscriberfuncs.c @@ -1,6 +1,6 @@ -/* Copyright (C) 2003 Mads Martin Joergensen - * - * $Id$ +/* + * Copyright (C) 2003 Mads Martin Joergensen + * Copyright (C) 2022 Baptiste Daroussin * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to @@ -22,21 +22,14 @@ */ #include -#include -#include #include -#include -#include -#include -#include #include #include #include "mlmmj.h" #include "subscriberfuncs.h" -#include "mygetline.h" #include "log_error.h" -#include "wrappers.h" +#include "chomp.h" char *subtype_strs[] = { "normal", @@ -57,64 +50,35 @@ char * subreason_strs[] = { "switch" }; -off_t find_subscriber(int fd, const char *address) +bool +find_subscriber(int fd, const char *address) { - char *start, *cur, *next; - struct stat st; - size_t len; - - if(fstat(fd, &st) < 0) { - log_error(LOG_ARGS, "Could not stat fd"); - return (off_t)-1; - } - - /* No need to check in 0-size file */ - if(st.st_size == 0) - return (off_t)-1; - - if(!S_ISREG(st.st_mode)) { - log_error(LOG_ARGS, "Non regular file in subscribers.d/"); - return (off_t)-1; - } - - if((start = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == - MAP_FAILED) { - log_error(LOG_ARGS, "Could not mmap fd"); - return (off_t)-1; - } - - for(next = cur = start; next < start + st.st_size; next++) { - if(*next == '\n') { - len = next - cur; - if((strlen(address) == len) && - (strncasecmp(address, cur, len) == 0)) { - munmap(start, st.st_size); - return (off_t)(cur - start); - } - cur = next + 1; - } - } - - if(next > cur) { - len = next - cur; - if((strlen(address) == len) && - (strncasecmp(address, cur, len) == 0)) { - munmap(start, st.st_size); - return (off_t)(cur - start); + FILE *f; + char *line = NULL; + size_t linecap = 0; + ssize_t linelen; + bool ret = false; + + f = fdopen(fd, "r"); + while ((linelen = getline(&line, &linecap, f)) > 0) { + chomp(line); + if (strcasecmp(address, line) == 0) { + ret = true; + break; } } - - munmap(start, st.st_size); - return (off_t)-1; + free(line); + fclose(f); + return (ret);; } int is_subbed_in(int dirfd, const char *subdirname, const char *address) { - int retval = 0, subread; - off_t suboff; + int subread; DIR *subddir; struct dirent *dp; + bool ret = false; if((subddir = fdopendir(dirfd)) == NULL) { log_error(LOG_ARGS, "Could not opendir(%s)", subdirname); @@ -134,19 +98,13 @@ is_subbed_in(int dirfd, const char *subdirname, const char *address) continue; } - suboff = find_subscriber(subread, address); - close(subread); - - if(suboff == -1) { - continue; - } else { - retval = 1; + ret = find_subscriber(subread, address); + if (ret) break; - } } closedir(subddir); - return retval; + return ret; } enum subtype diff --git a/tests/mlmmj.c b/tests/mlmmj.c index 8d4b9d57..3ffe42f1 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -53,6 +53,7 @@ #include "getlistdelim.h" #include "getlistaddr.h" #include "statctrl.h" +#include "subscriberfuncs.h" ATF_TC_WITHOUT_HEAD(random_int); ATF_TC_WITHOUT_HEAD(chomp); @@ -103,6 +104,7 @@ ATF_TC_WITHOUT_HEAD(send_mail_8); ATF_TC_WITHOUT_HEAD(getlistdelim); ATF_TC_WITHOUT_HEAD(getlistaddr); ATF_TC_WITHOUT_HEAD(statctrl); +ATF_TC_WITHOUT_HEAD(is_subbed_in); #ifndef NELEM #define NELEM(array) (sizeof(array) / sizeof((array)[0])) @@ -1620,6 +1622,17 @@ ATF_TC_BODY(statctrl, tc) ATF_REQUIRE_EQ(statctrl(fd, "test"), true); } +ATF_TC_BODY(is_subbed_in, tc) +{ + pid_t p; + + p = atf_utils_fork(); + if (p == 0) { + is_subbed_in(-1, "plop", "meh"); + } + atf_utils_wait(p, EXIT_FAILURE, "", ""); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, random_int); @@ -1671,6 +1684,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, getlistdelim); ATF_TP_ADD_TC(tp, getlistaddr); ATF_TP_ADD_TC(tp, statctrl); + ATF_TP_ADD_TC(tp, is_subbed_in); return (atf_no_error()); }