Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this user
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux
/
mxq
Public
forked from
mariux64/mxq
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
0
Security
Insights
Issues
Additional navigation options
Code
Pull requests
Actions
Projects
Security
Insights
Issues
Files
issues/issue15
manpages
mysql
web
.gitignore
LICENSE
Makefile
README.md
mx_flock.c
mx_flock.h
mx_getopt.c
mx_getopt.h
mx_log.c
mx_log.h
mx_mysql.c
mx_mysql.h
mx_util.c
mx_util.h
mxq.h
mxq_group.c
mxq_group.h
mxq_job.c
mxq_job.h
mxq_log.c
mxqadmin.c
mxqd.c
mxqd.h
mxqdctl-hostconfig.sh
mxqdump.c
mxqkill.c
mxqsub.c
os-release
test.c
test_mx_log.c
test_mx_mysql.c
test_mx_util.c
Breadcrumbs
mxq
/
mx_flock.c
Blame
Blame
Latest commit
History
History
157 lines (119 loc) · 2.83 KB
Breadcrumbs
mxq
/
mx_flock.c
Top
File metadata and controls
Code
Blame
157 lines (119 loc) · 2.83 KB
Raw
#define _GNU_SOURCE #include <sys/types.h> #include <sys/stat.h> #include <sys/file.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <assert.h> #include <errno.h> #include <stdio.h> #include <stdarg.h> #include "mx_log.h" #include "mx_flock.h" #define FLOCK_MODE 0600 #if !defined mx_free_null # define mx_free_null(a) do { free((a)); (a) = NULL; } while(0) #endif static inline int _flock_open(struct mx_flock *lock, mode_t mode) { int fd; assert(lock); assert(lock->fname); fd = open(lock->fname, O_RDONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, mode); if (fd < 0) { mx_log_debug("open(): %m"); return -1; } lock->fd = fd; return fd; } static inline int _flock_close(struct mx_flock *lock) { int res; assert(lock); res = close(lock->fd); if (res < 0) mx_log_debug("close(): %m"); lock->fd = -1; return res; } static inline void _flock_free(struct mx_flock *lock) { if (!lock) return; if (lock->fname) mx_free_null(lock->fname); if (lock->fd >= 0) _flock_close(lock); free(lock); } struct mx_flock *mx_flock(int operation, char *fmt, ...) { struct mx_flock *lock; va_list ap; char *fname; int res; struct stat s0, s1; assert(fmt); lock = malloc(sizeof(*lock)); if (!lock) { mx_log_debug("malloc(): %m"); return NULL; } lock->fd = -1; lock->operation = -1; lock->locked = 0; va_start(ap, fmt); res = vasprintf(&fname, fmt, ap); va_end(ap); if (res == -1) { mx_log_debug("vasprintf(): %m"); _flock_free(lock); return NULL; } lock->fname = fname; while (1) { res = _flock_open(lock, FLOCK_MODE); if (res < 0) { _flock_free(lock); return NULL; } res = flock(lock->fd, operation|LOCK_NB); if (res < 0) { if (errno == EWOULDBLOCK) return lock; mx_log_debug("flock(): %m"); _flock_free(lock); return NULL; } fstat(lock->fd, &s0); stat(lock->fname, &s1); if (s0.st_ino == s1.st_ino) { lock->operation = operation; break; } mx_log_warning("failed to acquire lock on %s - file changed on disk - retrying", lock->fname); _flock_close(lock); } lock->locked = 1; return lock; } int mx_funlock(struct mx_flock *lock) { int res; assert(lock); assert(lock->fname); assert(lock->fd >= 0); assert(lock->operation >= 0); assert(lock->locked); res = unlink(lock->fname); if (res < 0) mx_log_debug("unlink(): %m"); res = flock(lock->fd, LOCK_UN); if (res < 0) mx_log_debug("flock(): %m"); _flock_close(lock); _flock_free(lock); return res; }
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
You can’t perform that action at this time.