forked from rpm-software-management/rpm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpmlock.c
170 lines (143 loc) · 3.22 KB
/
rpmlock.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "system.h"
#include <errno.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmfileutil.h>
#include "lib/rpmlock.h"
#include "debug.h"
/* Internal interface */
struct rpmlock_s {
int fd;
int openmode;
char *path;
char *descr;
int fdrefs;
};
static rpmlock rpmlock_new(const char *lock_path, const char *descr)
{
rpmlock lock = (rpmlock) malloc(sizeof(*lock));
if (lock != NULL) {
mode_t oldmask = umask(022);
lock->fd = open(lock_path, O_RDWR|O_CREAT, 0644);
(void) umask(oldmask);
if (lock->fd == -1) {
if (errno == EACCES)
lock->fd = open(lock_path, O_RDONLY);
if (lock->fd == -1) {
free(lock);
lock = NULL;
} else {
lock->openmode = RPMLOCK_READ;
}
} else {
lock->openmode = RPMLOCK_WRITE | RPMLOCK_READ;
}
if (lock) {
lock->path = xstrdup(lock_path);
lock->descr = xstrdup(descr);
lock->fdrefs = 1;
}
}
return lock;
}
static void rpmlock_free(rpmlock lock)
{
if (--lock->fdrefs == 0) {
free(lock->path);
free(lock->descr);
(void) close(lock->fd);
free(lock);
}
}
static int rpmlock_acquire(rpmlock lock, int mode)
{
int res = 0;
if (!(mode & lock->openmode))
return res;
if (lock->fdrefs > 1) {
res = 1;
} else {
struct flock info;
int cmd;
if (mode & RPMLOCK_WAIT)
cmd = F_SETLKW;
else
cmd = F_SETLK;
if (mode & RPMLOCK_READ)
info.l_type = F_RDLCK;
else
info.l_type = F_WRLCK;
info.l_whence = SEEK_SET;
info.l_start = 0;
info.l_len = 0;
info.l_pid = 0;
if (fcntl(lock->fd, cmd, &info) != -1)
res = 1;
}
lock->fdrefs += res;
return res;
}
static void rpmlock_release(rpmlock lock)
{
/* if not locked then we must not release */
if (lock->fdrefs <= 1)
return;
if (--lock->fdrefs == 1) {
struct flock info;
info.l_type = F_UNLCK;
info.l_whence = SEEK_SET;
info.l_start = 0;
info.l_len = 0;
info.l_pid = 0;
(void) fcntl(lock->fd, F_SETLK, &info);
}
}
/* External interface */
rpmlock rpmlockNew(const char *lock_path, const char *descr)
{
rpmlock lock = rpmlock_new(lock_path, descr);
if (!lock) {
rpmlog(RPMLOG_ERR, _("can't create %s lock on %s (%s)\n"),
descr, lock_path, strerror(errno));
}
return lock;
}
int rpmlockAcquire(rpmlock lock)
{
int locked = 0; /* assume failure */
int myerrno = errno;
int maywait = isatty(STDIN_FILENO); /* dont wait within scriptlets */
errno = myerrno;
if (lock) {
locked = rpmlock_acquire(lock, RPMLOCK_WRITE);
if (!locked && (lock->openmode & RPMLOCK_WRITE) && maywait) {
rpmlog(RPMLOG_WARNING, _("waiting for %s lock on %s\n"),
lock->descr, lock->path);
locked = rpmlock_acquire(lock, (RPMLOCK_WRITE|RPMLOCK_WAIT));
}
if (!locked) {
rpmlog(RPMLOG_ERR, _("can't create %s lock on %s (%s)\n"),
lock->descr, lock->path, strerror(errno));
}
}
return locked;
}
void rpmlockRelease(rpmlock lock)
{
if (lock)
rpmlock_release(lock);
}
rpmlock rpmlockNewAcquire(const char *lock_path, const char *descr)
{
rpmlock lock = rpmlockNew(lock_path, descr);
if (!rpmlockAcquire(lock))
lock = rpmlockFree(lock);
return lock;
}
rpmlock rpmlockFree(rpmlock lock)
{
if (lock) {
rpmlock_release(lock);
rpmlock_free(lock);
}
return NULL;
}