1 /*
2 * Advanced Linux Sound Architecture Control Program
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "aconfig.h"
23 #include "version.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include "alsactl.h"
32
state_lock_(const char * file,int lock,int timeout,int _fd)33 static int state_lock_(const char *file, int lock, int timeout, int _fd)
34 {
35 int fd = -1, err = 0;
36 struct flock lck;
37 struct stat st;
38 char lcktxt[14];
39 char *nfile = lockfile;
40
41 if (do_lock <= 0)
42 return 0;
43
44 lck.l_type = lock ? F_WRLCK : F_UNLCK;
45 lck.l_whence = SEEK_SET;
46 lck.l_start = 0;
47 lck.l_len = 11;
48 lck.l_pid = 0;
49 if (lock) {
50 snprintf(lcktxt, sizeof(lcktxt), "%10li\n", (long)getpid());
51 } else {
52 snprintf(lcktxt, sizeof(lcktxt), "%10s\n", "");
53 fd = _fd;
54 }
55 while (fd < 0 && timeout-- > 0) {
56 fd = open(nfile, O_RDWR);
57 if (!lock && fd < 0) {
58 err = -EIO;
59 goto out;
60 }
61 if (fd < 0) {
62 fd = open(nfile, O_RDWR|O_CREAT|O_EXCL, 0644);
63 if (fd < 0) {
64 if (errno == EBUSY || errno == EAGAIN) {
65 sleep(1);
66 continue;
67 }
68 if (errno == EEXIST) {
69 fd = open(nfile, O_RDWR);
70 if (fd >= 0)
71 break;
72 }
73 err = -errno;
74 goto out;
75 }
76 }
77 }
78 if (fd < 0 && timeout <= 0) {
79 err = -EBUSY;
80 goto out;
81 }
82 if (fstat(fd, &st) < 0) {
83 err = -errno;
84 goto out;
85 }
86 if (st.st_size != 11 || !lock) {
87 if (write(fd, lcktxt, 11) != 11) {
88 err = -EIO;
89 goto out;
90 }
91 if (lock && lseek(fd, 0, SEEK_SET)) {
92 err = -errno;
93 goto out;
94 }
95 }
96 while (timeout > 0) {
97 if (fcntl(fd, F_SETLK, &lck) < 0) {
98 sleep(1);
99 timeout--;
100 } else {
101 break;
102 }
103 }
104 if (timeout <= 0) {
105 err = -EBUSY;
106 goto out;
107 }
108 if (lock) {
109 if (write(fd, lcktxt, 11) != 11) {
110 err = -EIO;
111 goto out;
112 }
113 return fd;
114 }
115 err = 0;
116
117 out:
118 if (fd >= 0)
119 close(fd);
120 return err;
121 }
122
state_lock(const char * file,int timeout)123 int state_lock(const char *file, int timeout)
124 {
125 int err;
126
127 err = state_lock_(file, 1, timeout, -1);
128 if (err < 0)
129 error("file %s lock error: %s", file, strerror(-err));
130 return err;
131 }
132
state_unlock(int _fd,const char * file)133 int state_unlock(int _fd, const char *file)
134 {
135 int err;
136
137 err = state_lock_(file, 0, 10, _fd);
138 if (err < 0)
139 error("file %s unlock error: %s", file, strerror(-err));
140 return err;
141 }
142