commit 3e37b20fd2c4a1b1a75838bf4ab81b52ff1c2226
parent 1f86a7bb32b059c0d115ecd196f3aa3beb06b53f
Author: kocotian <kocotian@kocotian.pl>
Date: Fri, 15 Jan 2021 21:35:10 +0100
locking skelet
Diffstat:
3 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/fwin.c b/fwin.c
@@ -10,9 +10,13 @@
#include <flib.h>
#include "getch.h"
+#define LOCKFILE "/tmp/.F0.lock"
+
int
main(void)
{
+ int err;
+
FbDrawable fbdev, buffer;
/* fbdev is actual framebuffer device, buffer is only temporary
buffer for making operations and its content is copied to fbdev.
@@ -24,6 +28,9 @@ main(void)
if ((fbdev.fbdesc = fbopen(DEFAULT_FRAMEBUFFER)) < 0)
exit(fbdev.fbdesc);
+ if ((err = fblock(LOCKFILE)) < 0)
+ exit(err);
+
/* Getting resolution from fbdev */
fbdev.res[0] = getres(fbdev.fbdesc);
fbdev.res[1] = getvres(fbdev.fbdesc);
@@ -66,4 +73,5 @@ main(void)
/* Unmapping and closing fbdev */
fbunmap(&fbdev.mappoint, fbdev.len);
fbclose(fbdev.fbdesc);
+ fbunlock(LOCKFILE);
}
diff --git a/include/flib.h b/include/flib.h
@@ -41,6 +41,9 @@ void fbunmap(char **mappoint, int64_t len);
int fbgetinfo(int fbdesc, struct fb_var_screeninfo *info);
int fbsetinfo(int fbdesc, struct fb_var_screeninfo *info);
+int fblock(const char *lockfile);
+int fbunlock(const char *lockfile);
+
int64_t getres(int fbdesc);
int64_t getvres(int fbdesc);
diff --git a/lib/flib.c b/lib/flib.c
@@ -10,6 +10,7 @@
#include <sys/types.h>
#include <fcntl.h>
+#include <stdio.h>
#include <unistd.h>
#include <flib.h>
@@ -92,6 +93,36 @@ fbsetinfo(int fbdesc, struct fb_var_screeninfo *info)
return ioctl(fbdesc, FBIOPUT_VSCREENINFO, info);
}
+/* --- typical server function --- */
+
+int
+fblock(const char *lockfile)
+{
+ int fd, bc;
+ char pidstr[8];
+
+ if (!access(lockfile, F_OK))
+ return -1;
+
+ if ((fd = open(lockfile, O_RDWR | O_CREAT)) < 0)
+ return -2;
+
+ bc = snprintf(pidstr, 8, "%d", getpid());
+ if (write(fd, pidstr, bc) < 0)
+ return -3;
+
+ if (close(fd) < 0)
+ return -4;
+
+ return 0;
+}
+
+int
+fbunlock(const char *lockfile)
+{
+ return unlink(lockfile);
+}
+
int64_t /* first 32 bits are x, second 32 bits are y */
getres(int fbdesc)
{