summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorPeter Elliott <pelliott@ualberta.ca>2021-07-18 23:55:13 -0600
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-07-20 17:44:30 +0430
commit2ce8cca7b5f5e3837ec49901869f6167f2894013 (patch)
treeb2614ffb4bfefd71bc903f07238c6c9015b55e06 /Userland
parent3fa28166428c41ca4a608d7c5c7090c69e55353e (diff)
downloadserenity-2ce8cca7b5f5e3837ec49901869f6167f2894013.zip
LibC: Implement flock(2) using fcntl's F_SETLK
While flock is not a posix interface, it exists on linux and all BSDs as far as I am aware.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibC/CMakeLists.txt1
-rw-r--r--Userland/Libraries/LibC/sys/file.cpp26
-rw-r--r--Userland/Libraries/LibC/sys/file.h13
3 files changed, 40 insertions, 0 deletions
diff --git a/Userland/Libraries/LibC/CMakeLists.txt b/Userland/Libraries/LibC/CMakeLists.txt
index ab1288a007..3108399552 100644
--- a/Userland/Libraries/LibC/CMakeLists.txt
+++ b/Userland/Libraries/LibC/CMakeLists.txt
@@ -39,6 +39,7 @@ set(LIBC_SOURCES
strings.cpp
stubs.cpp
syslog.cpp
+ sys/file.cpp
sys/mman.cpp
sys/prctl.cpp
sys/ptrace.cpp
diff --git a/Userland/Libraries/LibC/sys/file.cpp b/Userland/Libraries/LibC/sys/file.cpp
new file mode 100644
index 0000000000..503604b9e9
--- /dev/null
+++ b/Userland/Libraries/LibC/sys/file.cpp
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2021, Peter Elliott <pelliott@ualberta.ca>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Assertions.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/file.h>
+
+extern "C" {
+
+int flock(int fd, int operation)
+{
+ struct flock lock {
+ short(operation & 0b11), SEEK_SET, 0, 0, 0
+ };
+ if (operation & LOCK_NB) {
+ return fcntl(fd, F_SETLK, &lock);
+ }
+
+ // FIXME: Implement F_SETLKW in fcntl.
+ VERIFY_NOT_REACHED();
+}
+}
diff --git a/Userland/Libraries/LibC/sys/file.h b/Userland/Libraries/LibC/sys/file.h
index f9ae0c1ad8..2abfb7665d 100644
--- a/Userland/Libraries/LibC/sys/file.h
+++ b/Userland/Libraries/LibC/sys/file.h
@@ -5,3 +5,16 @@
*/
#pragma once
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+#define LOCK_SH 0
+#define LOCK_EX 1
+#define LOCK_UN 2
+#define LOCK_NB (1 << 2)
+
+int flock(int fd, int operation);
+
+__END_DECLS