-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslstatus.c
163 lines (133 loc) · 3.69 KB
/
slstatus.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
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/wait.h>
#include "arg.h"
#include "slstatus.h"
#include "util.h"
struct arg {
const char *(*func)(const char *);
const char *fmt;
const char *args;
const char *status_no;
const unsigned int update_interval;
};
char buf[1024];
static volatile sig_atomic_t done;
#include "config.h"
static void
terminate(const int signo)
{
if (signo != SIGUSR1)
done = 1;
}
static void
difftimespec(struct timespec *res, struct timespec *a, struct timespec *b)
{
res->tv_sec = a->tv_sec - b->tv_sec - (a->tv_nsec < b->tv_nsec);
res->tv_nsec = a->tv_nsec - b->tv_nsec + (a->tv_nsec < b->tv_nsec) * 1E9;
}
static void
usage(void)
{
die("usage: %s [-s] [-1]", argv0);
}
int
main(int argc, char *argv[])
{
struct sigaction act;
struct timespec start, current, diff, intspec, snooze;
size_t i;
int wait_status;
unsigned int loop_count = 0;
char status[MAXLEN];
char status_no[3] = {0};
const char *res;
/* The external command to run to update individual statuses. */
const char *extcmd[] = { "duskc", "--ignore-reply", "run_command", "setstatus", status_no, status, NULL };
/* Get the bar height and store it in an environment variable.
* The run command will return NULL if dusk is not running. */
const char *bar_height = run_command("duskc get_bar_height");
if (bar_height)
setenv("BAR_HEIGHT", bar_height, 1);
/* Create a lock file that prevents multiple instances of this
* program to be running for the same user on the same display. */
char lock_file[100] = {0};
snprintf(lock_file, sizeof lock_file - 1, "/tmp/.%s.slstatus.%s.lock", getenv("USER"), getenv("DISPLAY"));
struct flock fl = {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
.l_start = 0,
.l_len = 0
};
int lock_fd = open(lock_file, O_CREAT | O_RDWR, 0644);
if (lock_fd == -1) {
fprintf(stderr, "Error: Failed to open lock file %s: %s\n", lock_file, strerror(errno));
exit(1);
}
/* Try to acquire a lock on the file */
if (fcntl(lock_fd, F_SETLK, &fl) == -1) {
fprintf(stderr, "Error: Another instance of the program is already running\n");
exit(1);
}
ARGBEGIN {
case '1':
done = 1;
/* FALLTHROUGH */
default:
usage();
} ARGEND
if (argc)
usage();
memset(&act, 0, sizeof(act));
act.sa_handler = terminate;
sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
act.sa_flags |= SA_RESTART;
sigaction(SIGUSR1, &act, NULL);
do {
if (clock_gettime(CLOCK_MONOTONIC, &start) < 0)
die("clock_gettime:");
for (i = 0; i < LEN(args); i++) {
if (loop_count % args[i].update_interval)
continue;
status[0] = '\0';
if (!(res = args[i].func(args[i].args)))
res = unknown_str;
if (esnprintf(status, sizeof(status), args[i].fmt, res) < 0)
break;
esnprintf(status_no, sizeof(status_no), args[i].status_no);
if (fork() == 0) {
setsid();
close(lock_fd);
execvp(extcmd[0], (char **)extcmd);
die("Error: execvp '%s' failed:", extcmd[0]);
}
wait(&wait_status);
}
++loop_count;
if (!done) {
if (clock_gettime(CLOCK_MONOTONIC, ¤t) < 0)
die("clock_gettime:");
difftimespec(&diff, ¤t, &start);
intspec.tv_sec = interval / 1000;
intspec.tv_nsec = (interval % 1000) * 1E6;
difftimespec(&snooze, &intspec, &diff);
if (snooze.tv_sec >= 0 && nanosleep(&snooze, NULL) < 0 && errno != EINTR)
die("nanosleep:");
}
} while (!done);
/* Release the lock on the file */
fl.l_type = F_UNLCK;
fcntl(lock_fd, F_SETLK, &fl);
/* Close the lock file when the program exits */
close(lock_fd);
unlink(lock_file);
return 0;
}