File: locking.h

package info (click to toggle)
snapd 2.73-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,460 kB
  • sloc: sh: 16,736; ansic: 16,652; python: 11,215; makefile: 1,966; exp: 190; awk: 58; xml: 22
file content (118 lines) | stat: -rw-r--r-- 4,075 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2017-2018 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#ifndef SNAP_CONFINE_LOCKING_H
#define SNAP_CONFINE_LOCKING_H

// Include config.h which pulls in _GNU_SOURCE which in turn allows sys/types.h
// to define O_PATH. Since locking.h is included from locking.c this is
// required to see O_PATH there.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdbool.h>
#include <sys/types.h>

/**
 * Obtain a flock-based, exclusive, globally scoped, lock.
 *
 * The actual lock is placed in "/run/snap/ns/.lock"
 *
 * If the lock cannot be acquired for three seconds (via
 * sc_enable_sanity_timeout) then the function fails and the process dies.
 *
 * The return value needs to be passed to sc_unlock(), there is no need to
 * check for errors as the function will die() on any problem.
 **/
int sc_lock_global(void);

/**
 * Obtain a flock-based, exclusive, snap-scoped, lock.
 *
 * The actual lock is placed in "/run/snapd/ns/$SNAP_NAME.lock"
 * It should be acquired only when holding the global lock.
 *
 * If the lock cannot be acquired for three seconds (via
 * sc_enable_sanity_timeout) then the function fails and the process dies.
 *
 * The return value needs to be passed to sc_unlock(), there is no need to
 * check for errors as the function will die() on any problem.
 **/
int sc_lock_snap(const char *snap_name);

/**
 * Verify that a flock-based, exclusive, snap-scoped, lock is held.
 *
 * If the lock is not held the process dies. The details about the lock
 * are exactly the same as for sc_lock_snap().
 **/
void sc_verify_snap_lock(const char *snap_name);

/**
 * Obtain a flock-based, exclusive, snap-scoped, lock.
 *
 * The actual lock is placed in "/run/snapd/ns/$SNAP_NAME.$UID.lock"
 * It should be acquired only when holding the snap-specific lock.
 *
 * If the lock cannot be acquired for three seconds (via
 * sc_enable_sanity_timeout) then the function fails and the process dies.
 * The return value needs to be passed to sc_unlock(), there is no need to
 * check for errors as the function will die() on any problem.
 **/
int sc_lock_snap_user(const char *snap_name, uid_t uid);

/**
 * Release a flock-based lock.
 *
 * All kinds of locks can be unlocked the same way. This function simply
 * unlocks the lock and closes the file descriptor.
 **/
void sc_unlock(int lock_fd);

/**
 * Enable a sanity-check timeout.
 *
 * The timeout is based on good-old alarm(2) and is intended to break a
 * suspended system call, such as flock, after a few seconds. The built-in
 * timeout is primed for three seconds. After that any sleeping system calls
 * are interrupted and a flag is set.
 *
 * The call should be paired with sc_disable_sanity_check_timeout() that
 * disables the alarm and acts on the flag, aborting the process if the timeout
 * gets exceeded.
 **/
void sc_enable_sanity_timeout(void);

/**
 * Disable sanity-check timeout and abort the process if it expired.
 *
 * This call has to be paired with sc_enable_sanity_timeout(), see the function
 * description for more details.
 **/
void sc_disable_sanity_timeout(void);

typedef enum sc_snap_inhibition_hint {
    SC_SNAP_HINT_INHIBITED_FOR_REMOVE = 1 << 0,
} sc_snap_inhibition_hint;

/**
 * sc_snap_is_inhibited returns true if a given inhibition hint is set for given snap.
 * This is determined by testing the presence of a file in /var/lib/snapd/inhibit/<snap_name>.<hint>.
 **/
bool sc_snap_is_inhibited(const char *snap_name, sc_snap_inhibition_hint hint);

#endif  // SNAP_CONFINE_LOCKING_H