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 organization
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 }}
mariux64
/
mxq
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
20
Pull requests
3
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
7b1c066
helper
manpages
mysql
web
.gitignore
.vimrc
Doxyfile
LICENSE
Makefile
README.md
keywordset.c
keywordset.h
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_proc.c
mx_proc.h
mx_util.c
mx_util.h
mxq.h
mxq_daemon.c
mxq_daemon.h
mxq_group.c
mxq_group.h
mxq_job.c
mxq_job.h
mxq_log.c
mxqadmin.c
mxqd.c
mxqd.h
mxqd_control.c
mxqd_control.h
mxqdctl-hostconfig.sh
mxqdump.c
mxqkill.c
mxqps.c
mxqset.c
mxqsub.c
os-release
parser.y
ppidcache.c
ppidcache.h
test_keywordset.c
test_mx_log.c
test_mx_mysql.c
test_mx_util.c
test_mxqd_control.c
test_parser.c
xmalloc.h
Breadcrumbs
mxq
/
mx_flock.c
Blame
Blame
Latest commit
History
History
168 lines (128 loc) · 2.97 KB
Breadcrumbs
mxq
/
mx_flock.c
Top
File metadata and controls
Code
Blame
168 lines (128 loc) · 2.97 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_err("%s: %m",lock->fname); 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; mx_free_null(lock->fname); 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_err("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_err("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_err("flock(): %m"); _flock_close(lock); _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; if (!lock) return 0; if (!lock->locked) { _flock_free(lock); return 0; } assert(lock->fname); assert(lock->fd >= 0); assert(lock->operation >= 0); res = unlink(lock->fname); if (res < 0) mx_log_warning("unlink(): %m"); _flock_close(lock); _flock_free(lock); return res; } /* as above, but don't delete lock file */ void mx_funlock_nodelete(struct mx_flock *lock) { _flock_close(lock); _flock_free(lock); } void mx_flock_free(struct mx_flock *lock) { _flock_free(lock); }
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
You can’t perform that action at this time.