summaryrefslogtreecommitdiffstats
path: root/src/lock.c
blob: 49a23f36b88146c0500d310d877f7876360edbb3 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* $Id$ */

/*
 * This file is part of ivcall.
 *
 * ivcall is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * ivcall 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 ivcall; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pwd.h>
#include <inttypes.h>

#include "lock.h"
#include "util.h"

// Where do lockfiles reside?
#ifndef LOCK_DIR
#define LOCK_DIR "/var/lock"
#endif

static char* lockfile = NULL;

static const char *get_lockfile(const char *dev) {
    static char lockfile[PATH_MAX];
    snprintf(lockfile, sizeof(lockfile), "%s/LCK..%s", LOCK_DIR, basename((char*) dev));
    return lockfile;
}

static const char *get_tempfile(const char *path) {
    static char t[PATH_MAX];
    snprintf(t, sizeof(t), "%s.tmp.%lu", path, (unsigned long) getpid());
    return t;
}

int device_lock(const char *dev, const char *appname) {
    struct stat st;
    int fd;
    const char *path, *temp;
    char buf[100];
    char uidbuf[32];

    if (stat(LOCK_DIR, &st) != 0 || !S_ISDIR(st.st_mode)) {
        fprintf(stderr, "Failed to lock device, directory "LOCK_DIR" not existent.\n");
        return -1;
    }

    path = get_lockfile(dev);
    temp = get_tempfile(path);

    for (;;) {
        mode_t u;
        struct passwd* pw;
        char *username;
        
        if ((fd = open(path, O_RDONLY)) < 0) {
            if (errno != ENOENT) {
                fprintf(stderr, "Failed to open lock file: %s\n", strerror(errno));
                return -1;
            }
        }
        
        if (fd >= 0) {
            ssize_t n;

            n = loop_read(fd, buf, sizeof(buf) - 1);
            close(fd);

            if (n < 0) {
                close(fd);
                fprintf(stderr, "Failed to read from lock file: %s\n", strerror(errno));
                return -1;
            }
            
            if (n > 0) {
                pid_t pid;
                
                if (n == 4)
                    pid = (pid_t) *((uint32_t*) buf);
                else {
                    unsigned long v;
                    buf[n] = 0;
                    sscanf(buf, "%lu", &v);
                    pid = (pid_t) v;
                }
                
                if (pid > 0) {
                    if (kill(pid, 0) < 0 && errno == ESRCH) {
                        fprintf(stderr, "Lockfile is stale. Overriding it.\n");
                        /* Yes, here is a race condition */
                        unlink(path);
                    } else
                        return 1;
                }
            }
        }

        u = umask(0033);
        fd = open(temp, O_WRONLY | O_CREAT | O_EXCL, 0666);
        umask(u);
        
        if (fd < 0) {
            fprintf(stderr, "Failed to create temporary lock file: %s", strerror(errno));
            return -1;
        }

        if ((pw = getpwuid(getuid())))
            username = pw->pw_name;
        else
            snprintf(username = uidbuf, sizeof(uidbuf), "%lu", (unsigned long) getuid());

        snprintf(buf, sizeof(buf), "%10lu %s %.20s\n", (unsigned long) getpid(), appname, username);
        if (loop_write(fd, buf, strlen(buf)) < 0) {
            fprintf(stderr, "Failed to write to temporary lock file: %s", strerror(errno));
            close(fd);
            return -1;
        }

        close(fd);

        if (link(temp, path) < 0) {
            if (errno == EEXIST)
                continue;

            fprintf(stderr, "Failed to link temporary lock file: %s", strerror(errno));
        }

        unlink(temp);
        lockfile = strdup(path);

        return 0;
    }
}


// Tries to lock the first free device, returns 0 on success
int device_lock_first(const char *appname, char *dev, size_t l) {
    int i, found = 0;

    for (i = 2; i <= 20; i++) {
        snprintf(dev, l, "/dev/ttyI%i", i);
        
        if (!device_lock(dev, appname)) {
            found = 1;
            break;
        }
    }
    
    if (found)
        return 0;
    else {
        fprintf(stderr, "Could not find free device.\n");
        return -1;
    }
}

// Deletes the lockfile
void device_unlock(void) {
    if (lockfile) {
        unlink(lockfile);
        free (lockfile);
        lockfile = NULL;
    }
}