Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67368 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 45648 invoked from network); 8 May 2013 00:26:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 May 2013 00:26:55 -0000 Authentication-Results: pb1.pair.com smtp.mail=martin.pelikan@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=martin.pelikan@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.83.52 as permitted sender) X-PHP-List-Original-Sender: martin.pelikan@gmail.com X-Host-Fingerprint: 74.125.83.52 mail-ee0-f52.google.com Received: from [74.125.83.52] ([74.125.83.52:36495] helo=mail-ee0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 23/5E-06696-ECB99815 for ; Tue, 07 May 2013 20:26:54 -0400 Received: by mail-ee0-f52.google.com with SMTP id d41so618484eek.11 for ; Tue, 07 May 2013 17:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:date:from:to:subject:message-id:mime-version :content-type:content-disposition:user-agent; bh=edL2TWuNEmHGu4oUIebN2S7xQt17kLvBLuyb5IS2SiY=; b=OLNOLC0i70j4bMOeQxTjB0L2swr8l5dSCpAPdOhI/m3EQJ0i1VWyjLXCWBfn8LIe8g rxK3dosMCoLxu98PwwP3czjABVolbZ4btFqFtUspEPz3foTZv4y/wVcRIrj8D87XpHPY /vp/T2AHFWdvgd9dfNLRm7JFYlwZ8mkCdluKiHQI9MkwTvTOkuuPaUxKQCrbghXRY+pF BQJ9m8JIZSZy5Hg75ItTbob3VsqJaTfCTCXYU19rkc+8p49ce6UThY+NpeYpCxtVDrWw Tbu6gCpndbxY2XCRh7NWdJ6EcKpKi3+XUbNHqBYY8tzhsoBI0U3xdBdnq7e5RHsAfcCC ccOQ== X-Received: by 10.14.208.132 with SMTP id q4mr10795351eeo.35.1367972811748; Tue, 07 May 2013 17:26:51 -0700 (PDT) Received: from methuselah.storkhole.cz ([2a00:1028:201:201:21b:21ff:fe56:9a37]) by mx.google.com with ESMTPSA id r10sm42197639eez.10.2013.05.07.17.26.49 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 07 May 2013 17:26:50 -0700 (PDT) Date: Wed, 8 May 2013 02:26:45 +0200 To: internals@lists.php.net Message-ID: <20130508002645.GJ28723@methuselah.storkhole.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Subject: [PATCH] faster and safer fpm config reading From: martin.pelikan@gmail.com (Martin Pelikan) commit edae18ccf5c51ae0bcc0e9655ead7540fd42dd9f Author: Martin Pelikan Date: Wed May 8 02:06:09 2013 +0200 Faster and safer parsing of fpm configuration. The current fpm config parser did a read(2) syscall per character, which obfuscated debugging strace/ktrace output somewhat. This also makes the 1024 characters per line limit more explicit, so the users will know what were they hitting. It might improve performance during php-fpm load (.03 vs .00 seconds on my laptop) but primarily serves to help observing fpm system call traces during deployment (in chroot for example). Signed-off-by: Martin Pelikan diff --git a/sapi/fpm/fpm/fpm_conf.c b/sapi/fpm/fpm/fpm_conf.c index 0a8a0e3..e004934 100644 --- a/sapi/fpm/fpm/fpm_conf.c +++ b/sapi/fpm/fpm/fpm_conf.c @@ -1447,11 +1447,11 @@ static void fpm_conf_ini_parser(zval *arg1, zval *arg2, zval *arg3, int callback int fpm_conf_load_ini_file(char *filename TSRMLS_DC) /* {{{ */ { + const int bufsize = 1024; int error = 0; - char buf[1024+1]; - int fd, n; + char buf[bufsize], *newl; + int fd, pos = 0; int nb_read = 1; - char c = '*'; int ret = 1; @@ -1473,36 +1473,48 @@ int fpm_conf_load_ini_file(char *filename TSRMLS_DC) /* {{{ */ } ini_lineno = 0; + memset(buf, 0, sizeof buf); while (nb_read > 0) { int tmp; - memset(buf, 0, sizeof(char) * (1024 + 1)); - for (n = 0; n < 1024 && (nb_read = read(fd, &c, sizeof(char))) == sizeof(char) && c != '\n'; n++) { - buf[n] = c; - } - buf[n++] = '\n'; - ini_lineno++; - ini_filename = filename; - tmp = zend_parse_ini_string(buf, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t)fpm_conf_ini_parser, &error TSRMLS_CC); - ini_filename = filename; - if (error || tmp == FAILURE) { - if (ini_include) free(ini_include); - ini_recursion--; - close(fd); - return -1; - } - if (ini_include) { - char *tmp = ini_include; - ini_include = NULL; - fpm_evaluate_full_path(&tmp, NULL, NULL, 0); - fpm_conf_ini_parser_include(tmp, &error TSRMLS_CC); - if (error) { - free(tmp); + + nb_read = read(fd, buf + pos, sizeof buf - pos); + pos = 0; + + while ((newl = strchr(buf + pos, '\n')) != NULL) { + newl[0] = '\0'; + ini_lineno++; + ini_filename = filename; + tmp = zend_parse_ini_string(buf + pos, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t)fpm_conf_ini_parser, &error TSRMLS_CC); + ini_filename = filename; + if (error || tmp == FAILURE) { + if (ini_include) free(ini_include); ini_recursion--; close(fd); return -1; } - free(tmp); + if (ini_include) { + char *tmp = ini_include; + ini_include = NULL; + fpm_evaluate_full_path(&tmp, NULL, NULL, 0); + fpm_conf_ini_parser_include(tmp, &error TSRMLS_CC); + if (error) { + free(tmp); + ini_recursion--; + close(fd); + return -1; + } + free(tmp); + } + + pos = newl - buf + 1; + } + if (nb_read > 0 && pos == 0) { + zlog(ZLOG_ERROR, "line %u too long", ini_lineno + 1); + close(fd); + return -1; } + memmove(buf, buf + pos, sizeof buf - pos); + pos = sizeof buf - pos; } ini_recursion--;