Skip to content

Commit 73dd4a4

Browse files
Tim SmithMarkSymsCtx
Tim Smith
authored andcommitted
Add logging to fairlock
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 <mark.syms@cloud.com>
1 parent 618ecc4 commit 73dd4a4

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

misc/fairlock/fairlock.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sys/socket.h>
55
#include <sys/un.h>
66
#include <errno.h>
7+
#include <syslog.h>
78

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

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

53-
do {} while (read(fd, buffer, sizeof(buffer)) > 0);
55+
syslog(LOG_INFO, "%s acquired\n", argv[1]);
56+
while (read(fd, buffer, sizeof(buffer)) > 0) {
57+
buffer[127]='\0';
58+
syslog(LOG_INFO, "%s sent '%s'\n", argv[1], buffer);
59+
}
5460
close(fd);
61+
syslog(LOG_INFO, "%s released\n", argv[1]);
5562
}
5663
}
64+
closelog();
5765
}

misc/fairlock/fairlock.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def __enter__(self):
5757
except (FileNotFoundError, ConnectionRefusedError):
5858
self._ensure_service()
5959
self.sock.connect(self.sockname)
60+
61+
self.sock.send(f'{os.getpid()} - {time.monotonic()}'.encode())
6062
self.connected = True
6163
return self
6264

0 commit comments

Comments
 (0)