From patchwork Tue Jan 9 08:30:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 10151165 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9764860223 for ; Tue, 9 Jan 2018 08:33:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 86CBC287FF for ; Tue, 9 Jan 2018 08:33:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B63D2889E; Tue, 9 Jan 2018 08:33:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1ABD2287FF for ; Tue, 9 Jan 2018 08:33:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751342AbeAIIcA (ORCPT ); Tue, 9 Jan 2018 03:32:00 -0500 Received: from conuserg-10.nifty.com ([210.131.2.77]:27299 "EHLO conuserg-10.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751127AbeAIIb7 (ORCPT ); Tue, 9 Jan 2018 03:31:59 -0500 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-10.nifty.com with ESMTP id w098Uugs024960; Tue, 9 Jan 2018 17:30:56 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com w098Uugs024960 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1515486656; bh=eII+qy4k8i367HbCbA4Q0X4SPtggcoLkPrjaGg5QK9M=; h=From:To:Cc:Subject:Date:From; b=UNKteju4bWDu9YGtQhjHM7qI/FGGF2ZDBT9ZiPS/+S7t3oHjwko73VC2tMWyHzGnV SRi2dYjq5VL9kx4kaz6gykYEx+nO4fUrzja9OL/isVDDFI1qj7HbrI11Dqih5xJlNU 5zy4xrF/6gO+WSSe5QBp/QgIvSv/shk594CoQ1LdGSoctlpGR4aE4Nj+TzMdt+W7Mn FaNWPKYysljlu66LQs5ulXJOB/1nMtqtUNL2fS39kLvGGLd7pbPoRrkhPIOgP7LOyR p38hout0wI3M/QV3toodtLxVKsCHpnJDJehOb2sEZm1IpblPawPB7Krd0WyU9NATEJ G8HVDxsvQZo/A== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Sam Ravnborg , Michal Marek , Lukas Bulwahn , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH 1/6] fixdep: use malloc() and read() to load dep_file to buffer Date: Tue, 9 Jan 2018 17:30:45 +0900 Message-Id: <1515486650-1141-1-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Commit dee81e988674 ("fixdep: faster CONFIG_ search") changed how to load files in which CONFIG options are searched. It used malloc() and read() instead of mmap() because it needed to zero-terminate the buffer to use strstr(). print_deps() was left untouched since there was no reason to change it. Now, I have two motivations to change it in the same way. - do_config_file() and print_deps() do quite similar things; they open a file, map it onto memory, and pass it to a parser function. If we use malloc() and read() for print_deps() too, we can factor out the common code. (I will do this in the next commit.) - parse_dep_file() copies each token to a temporary buffer because it needs to zero-terminate it to be passed to printf(). It is not possible to modify the buffer directly because it is mmap'ed with O_RDONLY. If we load the file content into a malloc'ed buffer, we can insert '\0' after each token, and save memcpy(). (I will do this in the commit after next.) Signed-off-by: Masahiro Yamada --- scripts/basic/fixdep.c | 55 +++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c index 86a61d6..7efbeb7 100644 --- a/scripts/basic/fixdep.c +++ b/scripts/basic/fixdep.c @@ -309,24 +309,27 @@ static void do_config_file(const char *filename) * assignments are parsed not only by make, but also by the rather simple * parser in scripts/mod/sumversion.c. */ -static void parse_dep_file(void *map, size_t len) +static void parse_dep_file(char *m) { - char *m = map; - char *end = m + len; char *p; char s[PATH_MAX]; - int is_target; + int is_last, is_target; int saw_any_target = 0; int is_first_dep = 0; - while (m < end) { + while (1) { /* Skip any "white space" */ - while (m < end && (*m == ' ' || *m == '\\' || *m == '\n')) + while (*m == ' ' || *m == '\\' || *m == '\n') m++; + + if (!*m) + break; + /* Find next "white space" */ p = m; - while (p < end && *p != ' ' && *p != '\\' && *p != '\n') + while (*p && *p != ' ' && *p != '\\' && *p != '\n') p++; + is_last = (*p == '\0'); /* Is the token we found a target name? */ is_target = (*(p-1) == ':'); /* Don't write any target names into the dependency file */ @@ -374,6 +377,10 @@ static void parse_dep_file(void *map, size_t len) do_config_file(s); } } + + if (is_last) + break; + /* * Start searching for next token immediately after the first * "whitespace" character that follows this token. @@ -392,40 +399,42 @@ static void parse_dep_file(void *map, size_t len) printf("$(deps_%s):\n", target); } -static void print_deps(void) +static void print_deps(const char *filename) { struct stat st; int fd; - void *map; + char *buf; - fd = open(depfile, O_RDONLY); + fd = open(filename, O_RDONLY); if (fd < 0) { fprintf(stderr, "fixdep: error opening depfile: "); - perror(depfile); + perror(filename); exit(2); } if (fstat(fd, &st) < 0) { fprintf(stderr, "fixdep: error fstat'ing depfile: "); - perror(depfile); + perror(filename); exit(2); } if (st.st_size == 0) { - fprintf(stderr,"fixdep: %s is empty\n",depfile); close(fd); return; } - map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if ((long) map == -1) { - perror("fixdep: mmap"); - close(fd); - return; + buf = malloc(st.st_size + 1); + if (!buf) { + perror("fixdep: malloc"); + exit(2); } + if (read(fd, buf, st.st_size) != st.st_size) { + perror("fixdep: read"); + exit(2); + } + buf[st.st_size] = '\0'; + close(fd); - parse_dep_file(map, st.st_size); - - munmap(map, st.st_size); + parse_dep_file(buf); - close(fd); + free(buf); } int main(int argc, char *argv[]) @@ -441,7 +450,7 @@ int main(int argc, char *argv[]) cmdline = argv[3]; print_cmdline(); - print_deps(); + print_deps(depfile); return 0; }