MAILOCK(3)
π MAILOCK β the maillock(3) glow-up π
okay bestie, sit down, hydrate, and let me tell you about mailbox lockfiles β¨ this is maillock(3) from the Linux Programmer's Manual, and it's literally the girlies who keep your mail from getting trampled when everybody's writing at once. no cap.
β¨ NAME β¨
maillock, mailunlock, touchlock β manage mailbox lockfiles π
πΈ SYNOPSIS πΈ
#include <maillock.h>
cc [ flag ... ] file ... -llockfile [ library ]
int maillock( const char *user, int retrycnt );
void mailunlock( void );
void touchlock( void );
π¦ DESCRIPTION π¦
so maillock tries to create a lockfile for the user's mailbox in an NFS-safe (or at least NFS-resistant) way π
the whole algorithm is documented over in lockfile_create(3) if you wanna go read the lore.
the mailbox is typically hanging out in /var/mail, which means the lockfile name becomes /var/mail/USERNAME.lock β iconic, consistent, we love her. but if the environment variable $MAIL is set, and it ends with the same username that you passed to maillock(), then that file gets taken as the mailbox to lock instead. plot twist π
now here's the tea: there is genuinely no good way to see if a lockfile is stale π so the rule is β if the lockfile is older than 5 minutes, it gets removed. which is exactly why touchlock exists: while you're holding the lock, you have to refresh it regularly (every minute or so) by calling touchlock (). don't skip your skincare, don't skip your touchlock π§βοΈ
and finally, mailunlock removes the lockfile. clean up after yourself, queen π
π RETURN VALUES π
maillock returns one of these status codes, and yes we're listing all of them, periodt:
#define L_SUCCESS 0 /* Lockfile created */
#define L_NAMELEN 1 /* Recipient name too long (> 13 chars) */
#define L_TMPLOCK 2 /* Error creating tmp lockfile */
#define L_TMPWRITE 3 /* Can't write pid int tmp lockfile */
#define L_MAXTRYS 4 /* Failed after max. number of attempts */
#define L_ERROR 5 /* Unknown error; check errno */
#define L_RMSTALE 8 /* Failed to remove stale lockfile */
π NOTES π
okay important stuff, bestie, don't scroll past this π
- these functions are not thread safe. like, at all. if you need thread safe functions, or you need to lock other mailbox-(like) files that aren't in the standard location, use
lockfile_create(3)instead. she's the upgrade π - these functions call
lockfile_create(3)to do the actual work. that function might spawn a set group-id executable to do the real locking if the current process doesn't have enough privileges π so yeah, permissions drama, as always. - there are some issues with flushing the kernel's attribute cache if you're using NFS β go read the
lockfile_create(3)manpage for that whole saga. iykyk π
π FILES π
/var/mail/user.lock/usr/lib/liblockfile.so.1
π AUTHOR π
Miquel van Smoorenburg β we stan a legend π«Ά
π SEE ALSO π
lockfile_create(3)lockfile_touch (3)lockfile_remove(3)
and that's the whole vibe check, bestie π this page came from the Linux Manpage crew, dated 28 March 2001 β yeah, she's vintage, and she still ate. go lock your mailboxes responsibly β¨π
π¦ where this page comes from
- alpine 3.22: liblockfile-doc 1.17-r4 Β· LGPL-2.0-or-later AND GPL-2.0-or-later
- alpine 3.23: liblockfile-doc 1.17-r4 Β· LGPL-2.0-or-later AND GPL-2.0-or-later
- alpine 3.24: liblockfile-doc 1.17-r4 Β· LGPL-2.0-or-later AND GPL-2.0-or-later
- arch rolling: liblockfile 1.17_2-1 Β· GPL
- debian bookworm: liblockfile-dev 1.17-1+b1 Β· LGPL-2.0-or-later
- debian trixie: liblockfile-dev 1.17-2 Β· LGPL-2.0-or-later
- epel 10: liblockfile-devel 1.17-11.el10_2 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- fedora 43: liblockfile-devel 1.17-11.fc43 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- fedora 43: liblockfile-devel 1.17-11.fc43 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- fedora 44: liblockfile-devel 1.17-12.fc44 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- fedora 44: liblockfile-devel 1.17-12.fc44 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- nixos 26.05: liblockfile 1.17
- nixpkgs unstable: liblockfile 1.17
- opensuse-leap 16.0: liblockfile-devel 1.17-bp160.1.12 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- opensuse-leap 16.0: liblockfile-devel 1.17-bp160.1.12 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- opensuse-leap 16.0: liblockfile-devel 1.17-bp160.1.12 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- opensuse-leap 16.0: liblockfile-devel 1.17-bp160.1.12 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- ubuntu jammy: liblockfile-dev 1.17-1build2 Β· LGPL-2.0-or-later
- ubuntu noble: liblockfile-dev 1.17-1build3 Β· LGPL-2.0-or-later
- ubuntu questing: liblockfile-dev 1.17-2build1
- ubuntu resolute: liblockfile-dev 1.17-2build2 Β· LGPL-2.0-or-later
- ubuntu stonking: liblockfile-dev 1.17-2build2 Β· LGPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms