open file with flag O_CLOEXEC

replication_quorum
YuQing 2022-06-24 18:52:39 +08:00
parent aa5506191f
commit e254b8e1d3
12 changed files with 44 additions and 33 deletions

View File

@ -1,4 +1,7 @@
Version 1.59 2022-06-24
* open file with flag O_CLOEXEC
Version 1.58 2022-06-04
* add function conn_pool_connect_server_ex1 to support service name
* add function conn_pool_get_connection_ex to support service name

View File

@ -1237,8 +1237,8 @@ static PHPFileContext *fetch_file_context(const char *filename)
static int fc_open_file(PHPFileContext *ctx)
{
if ((ctx->fd = open(ctx->filename, O_WRONLY |
O_CREAT | O_APPEND, 0644)) < 0)
if ((ctx->fd = open(ctx->filename, O_WRONLY | O_CREAT |
O_APPEND | O_CLOEXEC, 0644)) < 0)
{
logError("file: "__FILE__", line: %d, "
"open file \"%s\" to write fail, "

View File

@ -49,7 +49,8 @@ int buffered_file_writer_open_ex(BufferedFileWriter *writer,
}
snprintf(writer->filename, sizeof(writer->filename), "%s", filename);
writer->fd = open(writer->filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
writer->fd = open(writer->filename, O_WRONLY |
O_CREAT | O_TRUNC | O_CLOEXEC, mode);
if (writer->fd < 0)
{
result = errno != 0 ? errno : EIO;

View File

@ -125,7 +125,7 @@ int conn_pool_connect_server_ex1(ConnectionInfo *conn,
}
if ((conn->sock=socketCreateEx2(conn->socket_domain, conn->ip_addr,
O_NONBLOCK, bind_ipaddr, &result)) < 0)
O_NONBLOCK | O_CLOEXEC, bind_ipaddr, &result)) < 0)
{
return result;
}
@ -160,8 +160,8 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn,
close(conn->sock);
}
if ((conn->sock=socketCreateEx2(conn->socket_domain,
conn->ip_addr, O_NONBLOCK, bind_ipaddr,
if ((conn->sock=socketCreateEx2(conn->socket_domain, conn->ip_addr,
O_NONBLOCK | O_CLOEXEC, bind_ipaddr,
&result)) < 0)
{
return result;
@ -181,7 +181,8 @@ int conn_pool_async_connect_server_ex(ConnectionInfo *conn,
return result;
}
static inline void conn_pool_get_key(const ConnectionInfo *conn, char *key, int *key_len)
static inline void conn_pool_get_key(const ConnectionInfo *conn,
char *key, int *key_len)
{
*key_len = sprintf(key, "%s_%u", conn->ip_addr, conn->port);
}

View File

@ -123,13 +123,14 @@ int id_generator_init_extra_ex(struct idg_context *context, const char *filename
mid = ntohl(ip_addr.s_addr) & ((1 << mid_bits) - 1);
}
if ((context->fd = open(filename, O_RDWR)) < 0)
if ((context->fd = open(filename, O_RDWR | O_CLOEXEC)) < 0)
{
if (errno == ENOENT)
{
mode_t old_mode;
old_mode = umask(0);
if ((context->fd=open(filename, O_RDWR | O_CREAT, mode)) < 0)
if ((context->fd=open(filename, O_RDWR | O_CREAT |
O_CLOEXEC, mode)) < 0)
{
result = errno != 0 ? errno : EACCES;
}

View File

@ -158,8 +158,8 @@ static int log_print_header(LogContext *pContext)
static int log_open(LogContext *pContext)
{
int result;
if ((pContext->log_fd = open(pContext->log_filename, O_WRONLY | \
O_CREAT | O_APPEND | pContext->fd_flags, 0644)) < 0)
if ((pContext->log_fd = open(pContext->log_filename, O_WRONLY | O_CREAT |
O_APPEND | O_CLOEXEC | pContext->fd_flags, 0644)) < 0)
{
fprintf(stderr, "open log file \"%s\" to write fail, " \
"errno: %d, error info: %s\n", \

View File

@ -114,9 +114,9 @@ static inline void fc_timedwait_sec(pthread_mutex_t *lock,
{
struct timespec ts;
PTHREAD_MUTEX_LOCK(lock);
ts.tv_sec = get_current_time() + timeout;
ts.tv_nsec = 0;
PTHREAD_MUTEX_LOCK(lock);
pthread_cond_timedwait(cond, lock, &ts);
PTHREAD_MUTEX_UNLOCK(lock);
}
@ -128,9 +128,9 @@ static inline void fc_timedwait_ms(pthread_mutex_t *lock,
struct timespec ts;
expires_ms = get_current_time_ms() + timeout_ms;
PTHREAD_MUTEX_LOCK(lock);
ts.tv_sec = expires_ms / 1000;
ts.tv_nsec = (expires_ms % 1000) * (1000 * 1000);
PTHREAD_MUTEX_LOCK(lock);
pthread_cond_timedwait(cond, lock, &ts);
PTHREAD_MUTEX_UNLOCK(lock);
}

View File

@ -127,9 +127,9 @@ static int sched_init_entries(ScheduleEntry *entries, const int count)
if (pEntry->interval <= 0)
{
logError("file: "__FILE__", line: %d, " \
"shedule interval %d <= 0", \
__LINE__, pEntry->interval);
logError("file: "__FILE__", line: %d, "
"shedule id: %d, interval %d <= 0",
__LINE__, pEntry->id, pEntry->interval);
return EINVAL;
}

View File

@ -335,7 +335,7 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
if ((bAllOwners || (statbuf.st_uid == myuid)) && S_ISDIR(statbuf.st_mode))
{
sprintf(filepath, "%s/cmdline", fullpath);
if ((fd = open(filepath, O_RDONLY))<0)
if ((fd=open(filepath, O_RDONLY | O_CLOEXEC))<0)
{
continue;
}
@ -734,7 +734,7 @@ int fc_get_file_line_count_ex(const char *filename,
return ENOMEM;
}
fd = open(filename, O_RDONLY);
fd = open(filename, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
result = errno != 0 ? errno : EACCES;
logError("file: "__FILE__", line: %d, "
@ -1314,7 +1314,7 @@ int getFileContent(const char *filename, char **buff, int64_t *file_size)
}
}
fd = open(filename, O_RDONLY);
fd = open(filename, O_RDONLY | O_CLOEXEC);
if (fd < 0)
{
*buff = NULL;
@ -1368,7 +1368,7 @@ int getFileContentEx(const char *filename, char *buff,
return EINVAL;
}
fd = open(filename, O_RDONLY);
fd = open(filename, O_RDONLY | O_CLOEXEC);
if (fd < 0)
{
*size = 0;
@ -1406,7 +1406,7 @@ int writeToFile(const char *filename, const char *buff, const int file_size)
int fd;
int result;
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd < 0)
{
result = errno != 0 ? errno : EIO;
@ -1480,7 +1480,7 @@ int fc_copy_file(const char *src_filename, const char *dest_filename)
int bytes;
char buff[16 * 1024];
src_fd = open(src_filename, O_RDONLY);
src_fd = open(src_filename, O_RDONLY | O_CLOEXEC);
if (src_fd < 0)
{
result = errno != 0 ? errno : ENOENT;
@ -1490,7 +1490,8 @@ int fc_copy_file(const char *src_filename, const char *dest_filename)
return result;
}
dest_fd = open(dest_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
dest_fd = open(dest_filename, O_WRONLY | O_CREAT |
O_TRUNC | O_CLOEXEC, 0644);
if (dest_fd < 0)
{
result = errno != 0 ? errno : EIO;

View File

@ -1198,7 +1198,7 @@ int fc_safe_write_file_init(SafeWriteFileInfo *fi,
static inline int fc_safe_write_file_open(SafeWriteFileInfo *fi)
{
const int flags = O_WRONLY | O_CREAT | O_TRUNC;
const int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC;
int result;
if ((fi->fd=open(fi->tmp_filename, flags, 0644)) < 0) {

View File

@ -1476,7 +1476,8 @@ int tcprecvfile(int sock, const char *filename, const int64_t file_bytes, \
recv_func = tcprecvdata_ex;
}
write_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
write_fd = open(filename, O_WRONLY | O_CREAT |
O_TRUNC | O_CLOEXEC, 0644);
if (write_fd < 0)
{
return errno != 0 ? errno : EACCES;
@ -1544,7 +1545,7 @@ int tcprecvfile(int sock, const char *filename, const int64_t file_bytes, \
break;
}
read_fd = open(filename, O_RDONLY);
read_fd = open(filename, O_RDONLY | O_CLOEXEC);
if (read_fd < 0)
{
return errno != 0 ? errno : EACCES;
@ -1627,7 +1628,8 @@ int tcprecvfile_ex(int sock, const char *filename, const int64_t file_bytes, \
recv_func = tcprecvdata_ex;
}
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
fd = open(filename, O_WRONLY | O_CREAT |
O_TRUNC | O_CLOEXEC, 0644);
if (fd < 0)
{
return errno != 0 ? errno : EACCES;
@ -1761,7 +1763,7 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
int64_t remain_bytes;
#endif
fd = open(filename, O_RDONLY);
fd = open(filename, O_RDONLY | O_CLOEXEC);
if (fd < 0)
{
*total_send_bytes = 0;
@ -1779,7 +1781,7 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
if (flags & O_NONBLOCK)
{
if (fcntl(sock, F_SETFL, flags & ~O_NONBLOCK) == -1)
if (fcntl(sock, F_SETFL, flags & ~O_NONBLOCK) < 0)
{
*total_send_bytes = 0;
return errno != 0 ? errno : EACCES;
@ -1873,7 +1875,7 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
if (flags & O_NONBLOCK) //restore
{
if (fcntl(sock, F_SETFL, flags) == -1)
if (fcntl(sock, F_SETFL, flags) < 0)
{
result = errno != 0 ? errno : EACCES;
}
@ -2124,7 +2126,7 @@ int tcpsetnonblockopt(int fd)
return errno != 0 ? errno : EACCES;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
if (fcntl(fd, F_SETFL, flags | (O_NONBLOCK | O_CLOEXEC)) < 0)
{
logError("file: "__FILE__", line: %d, " \
"fcntl failed, errno: %d, error info: %s.", \

View File

@ -843,7 +843,7 @@ int get_device_block_size(const char *device, int *block_size)
int fd;
size_t bs;
if ((fd=open(device, O_RDONLY)) < 0) {
if ((fd=open(device, O_RDONLY | O_CLOEXEC)) < 0) {
result = errno != 0 ? errno : ENOENT;
logError("file: "__FILE__", line: %d, "
"open device %s fail, errno: %d, error info: %s",
@ -885,7 +885,9 @@ static int get_block_size_by_write(const char *path, int *block_size)
snprintf(tmp_filename, sizeof(tmp_filename),
"%s/.blksize-test.tmp", path);
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT | O_DIRECT, 0755)) < 0) {
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT |
O_DIRECT | O_CLOEXEC, 0755)) < 0)
{
result = errno != 0 ? errno : ENOENT;
logError("file: "__FILE__", line: %d, "
"open file %s fail, errno: %d, error info: %s",