-
Colin Walters authored
Port to `glnx_autofd` tree wide, and add one missed `glnx_close_fd()` use in the tmpfile code.
Colin Walters authoredPort to `glnx_autofd` tree wide, and add one missed `glnx_close_fd()` use in the tmpfile code.
glnx-lockfile.c 5.77 KiB
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Now copied into libglnx:
- Use GError
Copyright 2010 Lennart Poettering
Copyright 2015 Colin Walters <walters@verbum.org>
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "config.h"
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "glnx-lockfile.h"
#include "glnx-errors.h"
#include "glnx-fdio.h"
#include "glnx-backport-autocleanups.h"
#include "glnx-local-alloc.h"
#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
/**
* glnx_make_lock_file:
* @dfd: Directory file descriptor (if not `AT_FDCWD`, must have lifetime `>=` @out_lock)
* @p: Path
* @operation: one of `LOCK_SH`, `LOCK_EX`, `LOCK_UN`, as passed to flock()
* @out_lock: (out) (caller allocates): Return location for lock
* @error: Error
*
* Block until a lock file named @p (relative to @dfd) can be created,
* using the flags in @operation, returning the lock data in the
* caller-allocated location @out_lock.
*
* This API wraps new-style process locking if available, otherwise
* falls back to BSD locks.
*/
gboolean
glnx_make_lock_file(int dfd, const char *p, int operation, GLnxLockFile *out_lock, GError **error) {
glnx_autofd int fd = -1;
g_autofree char *t = NULL;
int r;
/*
* We use UNPOSIX locks if they are available. They have nice
* semantics, and are mostly compatible with NFS. However,