Skip to content

Commit 40df920

Browse files
committed
Log error cause when opcache cannot write to file cache
1 parent 1cd2d73 commit 40df920

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
echo "OK\n";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
File cache error 001
3+
--EXTENSIONS--
4+
opcache
5+
posix
6+
--INI--
7+
opcache.enable_cli=1
8+
opcache.file_cache={PWD}
9+
opcache.log_verbosity_level=2
10+
--FILE--
11+
<?php
12+
posix_setrlimit(POSIX_RLIMIT_FSIZE, 1, 1);
13+
require __DIR__.'/file_cache_error.inc';
14+
?>
15+
--EXPECTF--
16+
%sWarning opcache cannot write to file %s: %s
17+
18+
OK

ext/opcache/zend_file_cache.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,23 +1004,55 @@ static char *zend_file_cache_get_bin_file_path(zend_string *script_path)
10041004
/**
10051005
* Helper function for zend_file_cache_script_store().
10061006
*
1007-
* @return true on success, false on error
1007+
* @return true on success, false on error and errno is set to indicate the cause of the error
10081008
*/
10091009
static bool zend_file_cache_script_write(int fd, const zend_persistent_script *script, const zend_file_cache_metainfo *info, const void *buf, const zend_string *s)
10101010
{
1011+
ssize_t written;
1012+
10111013
#ifdef HAVE_SYS_UIO_H
10121014
const struct iovec vec[] = {
10131015
{ .iov_base = (void *)info, .iov_len = sizeof(*info) },
10141016
{ .iov_base = (void *)buf, .iov_len = script->size },
10151017
{ .iov_base = (void *)ZSTR_VAL(s), .iov_len = info->str_size },
10161018
};
10171019

1018-
return writev(fd, vec, sizeof(vec) / sizeof(vec[0])) == (ssize_t)(sizeof(*info) + script->size + info->str_size);
1020+
written = writev(fd, vec, sizeof(vec) / sizeof(vec[0]));
1021+
if (EXPECTED(written == (ssize_t)(sizeof(*info) + script->size + info->str_size))) {
1022+
return true;
1023+
}
1024+
1025+
errno = written == -1 ? errno : EAGAIN;
1026+
return false;
10191027
#else
1020-
return ZEND_LONG_MAX >= (zend_long)(sizeof(*info) + script->size + info->str_size) &&
1021-
write(fd, info, sizeof(*info)) == sizeof(*info) &&
1022-
write(fd, buf, script->size) == script->size &&
1023-
write(fd, ZSTR_VAL(s), info->str_size) == info->str_size;
1028+
if (UNEXPECTED(ZEND_LONG_MAX >= (zend_long)(sizeof(*info) + script->size + info->str_size))) {
1029+
# ifdef EFBIG
1030+
errno = EFBIG;
1031+
# else
1032+
errno = ERANGE;
1033+
# endif
1034+
return false;
1035+
}
1036+
1037+
written = write(fd, info, sizeof(*info));
1038+
if (UNEXPECTED(written != sizeof(*info))) {
1039+
errno = written == -1 ? errno : EAGAIN;
1040+
return false;
1041+
}
1042+
1043+
written = write(fd, buf, script->size);
1044+
if (UNEXPECTED(written != script->size)) {
1045+
errno = written == -1 ? errno : EAGAIN;
1046+
return false;
1047+
}
1048+
1049+
written = write(fd, ZSTR_VAL(s), info->str_size);
1050+
if (UNEXPECTED(writen != info->str_size)) {
1051+
errno = written == -1 ? errno : EAGAIN;
1052+
return false;
1053+
}
1054+
1055+
return true;
10241056
#endif
10251057
}
10261058

@@ -1095,7 +1127,7 @@ int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm)
10951127
#endif
10961128

10971129
if (!zend_file_cache_script_write(fd, script, &info, buf, s)) {
1098-
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot write to file '%s'\n", filename);
1130+
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot write to file '%s': %s\n", filename, strerror(errno));
10991131
zend_string_release_ex(s, 0);
11001132
close(fd);
11011133
efree(mem);
@@ -1107,7 +1139,7 @@ int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm)
11071139
zend_string_release_ex(s, 0);
11081140
efree(mem);
11091141
if (zend_file_cache_flock(fd, LOCK_UN) != 0) {
1110-
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s'\n", filename);
1142+
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s': %s\n", filename, strerror(errno));
11111143
}
11121144
close(fd);
11131145
efree(filename);

0 commit comments

Comments
 (0)