|
23 | 23 | #include <windows.h> |
24 | 24 |
|
25 | 25 | #include <aclapi.h> |
26 | | -#include <direct.h> /* _wmkdir */ |
27 | | -#include <errno.h> /* errno for spawn-failure logging */ |
28 | | -#include <fcntl.h> /* _O_RDONLY */ |
29 | | -#include <io.h> /* _wunlink, _open_osfhandle, _close */ |
30 | | -#include <stdint.h> /* intptr_t */ |
| 26 | +#include <direct.h> /* _wmkdir */ |
| 27 | +#include <errno.h> /* errno for spawn-failure logging */ |
| 28 | +#include <fcntl.h> /* _O_RDONLY */ |
| 29 | +#include <io.h> /* _wunlink, _open_osfhandle, _close */ |
| 30 | +#include <share.h> /* _SH_DENYRW */ |
| 31 | +#include <sys/stat.h> /* _S_IREAD */ |
| 32 | +#include <stdint.h> /* intptr_t */ |
31 | 33 | #include "foundation/log.h" |
32 | 34 | #include "foundation/win_utf8.h" |
33 | 35 |
|
@@ -588,6 +590,26 @@ int cbm_rmdir(const char *path) { |
588 | 590 | return ret; |
589 | 591 | } |
590 | 592 |
|
| 593 | +int cbm_lockfile_open(const char *path, bool create) { |
| 594 | + wchar_t *wpath = cbm_path_to_wide(path); |
| 595 | + if (!wpath) { |
| 596 | + errno = EINVAL; |
| 597 | + return -1; |
| 598 | + } |
| 599 | + int flags = _O_RDWR | _O_BINARY | _O_NOINHERIT | (create ? _O_CREAT : 0); |
| 600 | + /* _SH_DENYRW: every other open of this file, from any process, fails |
| 601 | + * with EACCES until this descriptor closes -- including at death. */ |
| 602 | + int fd = _wsopen(wpath, flags, _SH_DENYRW, _S_IREAD | _S_IWRITE); |
| 603 | + free(wpath); |
| 604 | + return fd; |
| 605 | +} |
| 606 | + |
| 607 | +void cbm_lockfile_close(int fd) { |
| 608 | + if (fd >= 0) { |
| 609 | + (void)_close(fd); |
| 610 | + } |
| 611 | +} |
| 612 | + |
591 | 613 | /* Build a properly-quoted Windows command line from an argv array. |
592 | 614 | * Returns a heap-allocated wide string, or NULL on allocation failure. |
593 | 615 | * Quoting follows the MSVC CRT convention: arguments containing spaces, |
@@ -737,6 +759,7 @@ int cbm_exec_no_shell(const char *const *argv) { |
737 | 759 | #include <dirent.h> |
738 | 760 | #include <errno.h> |
739 | 761 | #include <fcntl.h> |
| 762 | +#include <sys/file.h> |
740 | 763 | #include <sys/stat.h> |
741 | 764 | #include <sys/wait.h> |
742 | 765 | #include <unistd.h> |
@@ -1043,6 +1066,34 @@ int cbm_rmdir(const char *path) { |
1043 | 1066 | return rmdir(path); |
1044 | 1067 | } |
1045 | 1068 |
|
| 1069 | +int cbm_lockfile_open(const char *path, bool create) { |
| 1070 | + int flags = O_RDWR | O_CLOEXEC | O_NOFOLLOW | (create ? O_CREAT : 0); |
| 1071 | + int fd; |
| 1072 | + do { |
| 1073 | + fd = open(path, flags, S_IRUSR | S_IWUSR); |
| 1074 | + } while (fd < 0 && errno == EINTR); |
| 1075 | + if (fd < 0) { |
| 1076 | + return -1; |
| 1077 | + } |
| 1078 | + int rc; |
| 1079 | + do { |
| 1080 | + rc = flock(fd, LOCK_EX | LOCK_NB); |
| 1081 | + } while (rc != 0 && errno == EINTR); |
| 1082 | + if (rc != 0) { |
| 1083 | + int saved = errno; |
| 1084 | + (void)close(fd); |
| 1085 | + errno = saved; |
| 1086 | + return -1; |
| 1087 | + } |
| 1088 | + return fd; |
| 1089 | +} |
| 1090 | + |
| 1091 | +void cbm_lockfile_close(int fd) { |
| 1092 | + if (fd >= 0) { |
| 1093 | + (void)close(fd); |
| 1094 | + } |
| 1095 | +} |
| 1096 | + |
1046 | 1097 | int cbm_exec_no_shell(const char *const *argv) { |
1047 | 1098 | if (!argv || !argv[0]) { |
1048 | 1099 | return CBM_NOT_FOUND; |
|
0 commit comments