website with slop placeholders
This commit is contained in:
+18
-12
@@ -229,9 +229,13 @@ String compiler_registry_lock_file_name(Request* context)
|
||||
return(compiler_registry_file_name(context) + ".lock");
|
||||
}
|
||||
|
||||
int compiler_open_lock_file(String file_name)
|
||||
int compiler_open_lock_file(String file_name, String purpose)
|
||||
{
|
||||
int fdlock = open(file_name.c_str(), O_RDWR | O_CREAT, 0666);
|
||||
(void)purpose;
|
||||
auto lock_dir = dirname(file_name);
|
||||
if(lock_dir != "")
|
||||
mkdir(lock_dir);
|
||||
int fdlock = file_open_locked(file_name, O_RDWR | O_CREAT, LOCK_EX, 0666);
|
||||
if(fdlock == -1)
|
||||
printf("(!) Could not open lock file %s\n", file_name.c_str());
|
||||
return(fdlock);
|
||||
@@ -239,10 +243,7 @@ int compiler_open_lock_file(String file_name)
|
||||
|
||||
void compiler_close_lock_file(int fdlock)
|
||||
{
|
||||
if(fdlock == -1)
|
||||
return;
|
||||
flock(fdlock, LOCK_UN);
|
||||
close(fdlock);
|
||||
file_close_locked(fdlock);
|
||||
}
|
||||
|
||||
String compiler_normalize_unit_path(Request* context, String file_name)
|
||||
@@ -297,9 +298,7 @@ template <typename TCallback>
|
||||
auto compiler_with_registry_lock(Request* context, TCallback callback) -> decltype(callback())
|
||||
{
|
||||
auto lock_file_name = compiler_registry_lock_file_name(context);
|
||||
int fdlock = compiler_open_lock_file(lock_file_name);
|
||||
if(fdlock != -1)
|
||||
flock(fdlock, LOCK_EX);
|
||||
int fdlock = compiler_open_lock_file(lock_file_name, "compiler-registry");
|
||||
auto result = callback();
|
||||
compiler_close_lock_file(fdlock);
|
||||
return(result);
|
||||
@@ -794,9 +793,16 @@ SharedUnit* compiler_get_shared_unit_internal(Request* context, String file_name
|
||||
setup_unit_paths(context, su, file_name);
|
||||
su->opt_so_optional = opt_so_optional;
|
||||
|
||||
int fdlock = compiler_open_lock_file(su->so_name + ".lock");
|
||||
if(fdlock != -1)
|
||||
flock(fdlock, LOCK_EX);
|
||||
int fdlock = compiler_open_lock_file(su->so_name + ".lock", "shared-unit:" + file_name);
|
||||
if(fdlock == -1)
|
||||
{
|
||||
su->compiler_messages = "could not open compile lock";
|
||||
su->compile_status = "lock_error";
|
||||
su->compile_error_status = su->compiler_messages;
|
||||
su->last_error = time();
|
||||
context->server->units[file_name] = su;
|
||||
return(su);
|
||||
}
|
||||
|
||||
cached = compiler_reusable_cached_unit(context, file_name, opt_so_optional, force_recompile);
|
||||
if(cached)
|
||||
|
||||
+25
-8
@@ -1,4 +1,3 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
@@ -10,6 +9,12 @@
|
||||
#include <sys/file.h>
|
||||
#include "sys.h"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr f64 FILE_LOCK_WAIT_TIMEOUT_SECONDS = 3.0;
|
||||
|
||||
}
|
||||
|
||||
String capture_backtrace_string(u32 max_frames, u32 skip_frames)
|
||||
{
|
||||
if(max_frames == 0)
|
||||
@@ -188,12 +193,15 @@ bool file_exists(String path)
|
||||
return(std::filesystem::exists(fp));
|
||||
}
|
||||
|
||||
int file_open_locked(String file_name, int open_flags, int lock_type, int create_mode)
|
||||
int file_open_locked(String file_name, int open_flags, int lock_type, int create_mode, f64 wait_timeout_seconds, String purpose)
|
||||
{
|
||||
(void)wait_timeout_seconds;
|
||||
(void)purpose;
|
||||
int fd = open(file_name.c_str(), open_flags, create_mode);
|
||||
if(fd == -1)
|
||||
return(-1);
|
||||
if(flock(fd, lock_type) == -1)
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
if(flock(fd, lock_type) != 0)
|
||||
{
|
||||
close(fd);
|
||||
return(-1);
|
||||
@@ -209,6 +217,11 @@ void file_close_locked(int fd)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void file_release_process_locks(String reason)
|
||||
{
|
||||
(void)reason;
|
||||
}
|
||||
|
||||
String file_get_contents_locked_fd(int fd)
|
||||
{
|
||||
if(fd == -1)
|
||||
@@ -254,7 +267,7 @@ bool file_put_contents_locked_fd(int fd, String content)
|
||||
|
||||
String file_get_contents(String file_name)
|
||||
{
|
||||
s32 fd = file_open_locked(file_name, O_RDONLY, LOCK_SH);
|
||||
s32 fd = file_open_locked(file_name, O_RDONLY, LOCK_SH, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "file_get_contents:" + file_name);
|
||||
if(fd == -1)
|
||||
{
|
||||
printf("(!) Could not read %s\n", file_name.c_str());
|
||||
@@ -267,7 +280,7 @@ String file_get_contents(String file_name)
|
||||
|
||||
bool file_put_contents(String file_name, String content)
|
||||
{
|
||||
s32 fd = file_open_locked(file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
s32 fd = file_open_locked(file_name, O_RDWR | O_CREAT, LOCK_EX, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "file_put_contents:" + file_name);
|
||||
if(fd == -1)
|
||||
{
|
||||
printf("(!) Could not write %s\n", file_name.c_str());
|
||||
@@ -285,7 +298,7 @@ bool file_put_contents(String file_name, String content)
|
||||
|
||||
bool file_append_contents(String file_name, String content)
|
||||
{
|
||||
s32 fd = file_open_locked(file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
s32 fd = file_open_locked(file_name, O_RDWR | O_CREAT, LOCK_EX, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "file_append:" + file_name);
|
||||
if(fd == -1)
|
||||
{
|
||||
printf("(!) Could not append %s\n", file_name.c_str());
|
||||
@@ -583,6 +596,7 @@ pid_t spawn_subprocess(std::function<void()> exec_after_spawn)
|
||||
p = fork();
|
||||
if(p == 0)
|
||||
{
|
||||
file_release_process_locks("fork child startup");
|
||||
my_pid = getpid();
|
||||
//printf("(C) child procress started, PID:%i\n", my_pid);
|
||||
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
||||
@@ -629,7 +643,9 @@ pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
{
|
||||
String status_file_name = context->server->config["BIN_DIRECTORY"] + "/task-" + key;
|
||||
String lock_file_name = status_file_name + ".lock";
|
||||
int lock_fd = file_open_locked(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
int lock_fd = file_open_locked(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "task:" + key);
|
||||
if(lock_fd == -1)
|
||||
return(0);
|
||||
String status_file = file_get_contents(status_file_name);
|
||||
pid_t p;
|
||||
if(status_file != "")
|
||||
@@ -647,6 +663,7 @@ pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
p = fork();
|
||||
if(p == 0)
|
||||
{
|
||||
file_release_process_locks("task child startup");
|
||||
file_close_locked(lock_fd);
|
||||
my_pid = getpid();
|
||||
|
||||
@@ -655,7 +672,7 @@ pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
|
||||
//printf("(C) child procress started, PID:%i\n", my_pid);
|
||||
//prctl(PR_SET_PDEATHSIG, SIGHUP);
|
||||
exec_after_spawn();
|
||||
int exit_lock_fd = file_open_locked(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
int exit_lock_fd = file_open_locked(lock_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644, FILE_LOCK_WAIT_TIMEOUT_SECONDS, "task-exit:" + key);
|
||||
file_unlink(status_file_name);
|
||||
file_close_locked(exit_lock_fd);
|
||||
printf("(P) worker process '%s' terminated: PID %i\n", key.c_str(), my_pid);
|
||||
|
||||
+2
-1
@@ -11,8 +11,9 @@ String dirname(String fn);
|
||||
String path_join(String base, String child);
|
||||
bool mkdir(String path);
|
||||
bool file_exists(String path);
|
||||
int file_open_locked(String file_name, int open_flags, int lock_type = LOCK_SH, int create_mode = 0644);
|
||||
int file_open_locked(String file_name, int open_flags, int lock_type = LOCK_SH, int create_mode = 0644, f64 wait_timeout_seconds = 3.0, String purpose = "");
|
||||
void file_close_locked(int fd);
|
||||
void file_release_process_locks(String reason = "");
|
||||
String file_get_contents_locked_fd(int fd);
|
||||
bool file_put_contents_locked_fd(int fd, String content);
|
||||
String file_get_contents(String file_name);
|
||||
|
||||
@@ -98,11 +98,6 @@ Request::~Request()
|
||||
delete stream;
|
||||
ob_stack.clear();
|
||||
ob = 0;
|
||||
if(session_lock_fd != -1)
|
||||
{
|
||||
flock(session_lock_fd, LOCK_UN);
|
||||
close(session_lock_fd);
|
||||
}
|
||||
for(auto& sockfd : resources.sockets)
|
||||
close(sockfd);
|
||||
}
|
||||
|
||||
+4
-3
@@ -155,9 +155,7 @@ struct Request {
|
||||
StringMap post;
|
||||
StringMap cookies;
|
||||
StringMap session;
|
||||
String session_file_name = "";
|
||||
String session_serialized = "";
|
||||
int session_lock_fd = -1;
|
||||
String session_loaded_hash = "";
|
||||
|
||||
DTree var;
|
||||
DTree cfg;
|
||||
@@ -211,6 +209,9 @@ struct Request {
|
||||
String websocket_connection_id = "";
|
||||
String websocket_scope = "";
|
||||
DTree* websocket_connection_state = 0;
|
||||
StringList websocket_scope_connection_ids;
|
||||
DTree websocket_dispatch_commands;
|
||||
bool websocket_dispatch_capture = false;
|
||||
u8 websocket_opcode = 0;
|
||||
bool websocket_is_binary = false;
|
||||
bool websocket_is_text = false;
|
||||
|
||||
+29
-40
@@ -544,15 +544,21 @@ String session_file_path(String session_id)
|
||||
|
||||
namespace {
|
||||
|
||||
void session_release_lock(Request* request)
|
||||
String session_serialize(const StringMap& data)
|
||||
{
|
||||
if(!request)
|
||||
return;
|
||||
if(request->session_lock_fd != -1)
|
||||
file_close_locked(request->session_lock_fd);
|
||||
request->session_lock_fd = -1;
|
||||
request->session_file_name = "";
|
||||
request->session_serialized = "";
|
||||
return(encode_query(data));
|
||||
}
|
||||
|
||||
String session_hash_serialized(String serialized)
|
||||
{
|
||||
return(gen_sha1(serialized));
|
||||
}
|
||||
|
||||
String session_load_serialized(String session_path)
|
||||
{
|
||||
if(session_path == "" || !file_exists(session_path))
|
||||
return("");
|
||||
return(file_get_contents(session_path));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -562,47 +568,36 @@ StringMap load_session_data(String session_id)
|
||||
String session_path = session_file_path(session_id);
|
||||
if(session_path == "")
|
||||
return(StringMap());
|
||||
if(context && context->session_lock_fd != -1 && context->session_file_name == session_path)
|
||||
return(parse_query(file_get_contents_locked_fd(context->session_lock_fd)));
|
||||
return(parse_query(file_get_contents(session_path)));
|
||||
return(parse_query(session_load_serialized(session_path)));
|
||||
}
|
||||
|
||||
void save_session_data(String session_id, StringMap data)
|
||||
{
|
||||
String session_path = session_file_path(session_id);
|
||||
String encoded = encode_query(data);
|
||||
String encoded = session_serialize(data);
|
||||
String encoded_hash = session_hash_serialized(encoded);
|
||||
if(session_path == "")
|
||||
{
|
||||
printf("(!) Refusing to save invalid session id\n");
|
||||
return;
|
||||
}
|
||||
if(context && context->session_lock_fd != -1 && context->session_file_name == session_path)
|
||||
if(context && encoded_hash == context->session_loaded_hash)
|
||||
return;
|
||||
if(!file_put_contents(session_path, encoded))
|
||||
{
|
||||
if(encoded == context->session_serialized)
|
||||
return;
|
||||
if(file_put_contents_locked_fd(context->session_lock_fd, encoded))
|
||||
context->session_serialized = encoded;
|
||||
printf("(!) Refusing to save session file %s\n", session_path.c_str());
|
||||
return;
|
||||
}
|
||||
int fd = file_open_locked(session_path, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
if(fd == -1)
|
||||
{
|
||||
printf("(!) Refusing to save unreadable session file %s\n", session_path.c_str());
|
||||
return;
|
||||
}
|
||||
if(file_get_contents_locked_fd(fd) != encoded)
|
||||
file_put_contents_locked_fd(fd, encoded);
|
||||
file_close_locked(fd);
|
||||
if(context)
|
||||
context->session_loaded_hash = encoded_hash;
|
||||
}
|
||||
|
||||
String session_start(String session_name)
|
||||
{
|
||||
if(context->session_lock_fd != -1 && context->session_name == session_name && context->session_id != "")
|
||||
if(context->session_name == session_name && context->session_id != "")
|
||||
return(context->session_id);
|
||||
session_release_lock(context);
|
||||
context->session.clear();
|
||||
context->session_serialized = "";
|
||||
context->session_file_name = "";
|
||||
context->session_loaded_hash = "";
|
||||
context->session_id = "";
|
||||
context->session_name = "";
|
||||
|
||||
@@ -617,15 +612,10 @@ String session_start(String session_name)
|
||||
}
|
||||
context->session_id = session_id;
|
||||
context->session_name = session_name;
|
||||
context->session_file_name = session_file_path(context->session_id);
|
||||
if(context->session_file_name != "")
|
||||
{
|
||||
context->session_lock_fd = file_open_locked(context->session_file_name, O_RDWR | O_CREAT, LOCK_EX, 0644);
|
||||
if(context->session_lock_fd == -1)
|
||||
printf("(!) Could not lock session file %s\n", context->session_file_name.c_str());
|
||||
}
|
||||
context->session = load_session_data(context->session_id);
|
||||
context->session_serialized = encode_query(context->session);
|
||||
auto session_path = session_file_path(context->session_id);
|
||||
auto serialized = session_load_serialized(session_path);
|
||||
context->session_loaded_hash = session_hash_serialized(serialized);
|
||||
context->session = parse_query(serialized);
|
||||
return(context->session_id);
|
||||
}
|
||||
|
||||
@@ -636,7 +626,6 @@ void session_destroy(String session_name)
|
||||
set_cookie(session_name, "", time() - int_val(context->server->config["SESSION_TIME"]));
|
||||
context->session.clear();
|
||||
save_session_data(context->session_id, context->session);
|
||||
session_release_lock(context);
|
||||
context->session_id = "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user