Skip to content

Commit

Permalink
Add logging to fairlock
Browse files Browse the repository at this point in the history
Log to LOCAL2 at log level INFO when a lock is acquired and released,
and when the client holding the lock sends anything through the socket
(they can use this to identify themselves to the logging e.g. by sending
"PID <PID>"

Signed-off-by: Mark Syms <[email protected]>
  • Loading branch information
Tim Smith authored and MarkSymsCtx committed Jun 21, 2024
1 parent 618ecc4 commit 73dd4a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion misc/fairlock/fairlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <syslog.h>

int main(int argc, char *argv[]) {
struct sockaddr_un addr;
Expand Down Expand Up @@ -31,6 +32,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "listen(64) failed on socket %s: %s", argv[1], strerror(errno));
exit(1);
}
openlog("fairlock", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL2);

/* Now we have a socket, enter an endless loop of:
* 1) Accept a connection
Expand All @@ -50,8 +52,14 @@ int main(int argc, char *argv[]) {
while ((fd = accept(sock, NULL, NULL)) > -1) {
char buffer[128];

do {} while (read(fd, buffer, sizeof(buffer)) > 0);
syslog(LOG_INFO, "%s acquired\n", argv[1]);
while (read(fd, buffer, sizeof(buffer)) > 0) {
buffer[127]='\0';
syslog(LOG_INFO, "%s sent '%s'\n", argv[1], buffer);
}
close(fd);
syslog(LOG_INFO, "%s released\n", argv[1]);
}
}
closelog();
}
2 changes: 2 additions & 0 deletions misc/fairlock/fairlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def __enter__(self):
except (FileNotFoundError, ConnectionRefusedError):
self._ensure_service()
self.sock.connect(self.sockname)

self.sock.send(f'{os.getpid()} - {time.monotonic()}'.encode())
self.connected = True
return self

Expand Down

0 comments on commit 73dd4a4

Please sign in to comment.