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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
|
/* See LICENSE file for copyright and license details. */
#include "arg.h"
#include "util.h"
#include "state.h"
#include "servers-master.h"
#include "servers-kernel.h"
#include "servers-crtc.h"
#include "servers-gamma.h"
#include "servers-coopgamma.h"
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/**
* Number put in front of the marshalled data
* so the program an detect incompatible updates
*/
#define MARSHAL_VERSION 0
#ifndef GCC_ONLY
# if defined(__GNUC__) && !defined(__clang__)
# define GCC_ONLY(...) __VA_ARGS__
# else
# define GCC_ONLY(...) /* nothing */
# endif
#endif
/**
* Lists all function recognised adjustment methods,
* will call macro X with the code for the each
* adjustment method as the first argument and
* corresponding name as the second argument
*/
#define LIST_ADJUSTMENT_METHODS\
X(LIBGAMMA_METHOD_DUMMY, "dummy")\
X(LIBGAMMA_METHOD_X_RANDR, "randr")\
X(LIBGAMMA_METHOD_X_VIDMODE, "vidmode")\
X(LIBGAMMA_METHOD_LINUX_DRM, "drm")\
X(LIBGAMMA_METHOD_W32_GDI, "gdi")\
X(LIBGAMMA_METHOD_QUARTZ_CORE_GRAPHICS, "quartz")
/**
* Used by initialisation functions as their return type. If a
* value not listed here is returned by such function, it is the
* exit value the process shall exit with as soon as possible.
*/
enum init_status {
/**
* Initialisation was successful
*/
INIT_SUCCESS = -1,
/**
* Initialisation failed
*/
INIT_FAILURE = -2,
/**
* Server is already running
*/
INIT_RUNNING = -3,
};
/**
* The pathname of the PID file
*/
extern char *restrict pidpath;
char *restrict pidpath = NULL;
/**
* The pathname of the socket
*/
extern char *restrict socketpath;
char *restrict socketpath = NULL;
/**
* Called when the process receives
* a signal telling it to re-execute
*
* @param signo The received signal
*/
static void
sig_reexec(int signo)
{
int saved_errno = errno;
reexec = 1;
signal(signo, sig_reexec);
errno = saved_errno;
}
/**
* Called when the process receives
* a signal telling it to terminate
*
* @param signo The received signal
*/
static void
sig_terminate(int signo)
{
terminate = 1;
(void) signo;
}
/**
* Called when the process receives
* a signal telling it to disconnect
* from or reconnect to the site
*
* @param signo The received signal
*/
static void
sig_connection(int signo)
{
int saved_errno = errno;
connection = signo - SIGRTMIN + 1;
signal(signo, sig_connection);
errno = saved_errno;
}
/**
* Called when the process receives
* a signal telling it to dump its
* state to stderr
*
* @param signo The received signal
*/
static void
sig_info(int signo)
{
int saved_errno = errno;
char *env;
signal(signo, sig_info);
env = getenv("COOPGAMMAD_PIDFILE_TOKEN");
fprintf(stderr, "PID file token: %s\n", env ? env : "(null)");
fprintf(stderr, "PID file: %s\n", pidpath ? pidpath : "(null)");
fprintf(stderr, "Socket path: %s\n", socketpath ? socketpath : "(null)");
state_dump();
errno = saved_errno;
}
/**
* Parse adjustment method name (or stringised number)
*
* @param arg The adjustment method name (or stringised number)
* @return The adjustment method, -1 (negative) on error
*/
GCC_ONLY(__attribute__((__nonnull__)))
static int
get_method(const char *restrict arg)
{
#if LIBGAMMA_METHOD_MAX > 5
# warning libgamma has added more adjustment methods
#endif
const char *restrict p;
#define X(C, N) if (!strcmp(arg, N)) return C;
LIST_ADJUSTMENT_METHODS;
#undef X
if (!*arg || (/* avoid overflow: */ strlen(arg) > 4))
goto bad;
for (p = arg; *p; p++)
if ('0' > *p || *p > '9')
goto bad;
return atoi(arg);
bad:
fprintf(stderr, "%s: unrecognised adjustment method name: %s\n", argv0, arg);
errno = 0;
return -1;
}
/**
* Set up signal handlers
*
* @return Zero on success, -1 on error
*/
static int
set_up_signals(void)
{
if (signal(SIGUSR1, sig_reexec) == SIG_ERR ||
signal(SIGUSR2, sig_info) == SIG_ERR ||
#if defined(SIGINFO)
signal(SIGINFO, sig_info) == SIG_ERR ||
#endif
signal(SIGTERM, sig_terminate) == SIG_ERR ||
signal(SIGRTMIN + 0, sig_connection) == SIG_ERR ||
signal(SIGRTMIN + 1, sig_connection) == SIG_ERR)
return -1;
return 0;
}
/**
* Fork the process to the background
*
* @param keep_stderr Keep stderr open?
* @return An `enum init_status` value or an exit value
*/
static enum init_status
daemonise(int keep_stderr)
{
pid_t pid;
int fd = -1, saved_errno;
int notify_rw[2] = {-1, -1};
char a_byte = 0;
ssize_t got;
if (pipe(notify_rw) < 0)
goto fail;
if (notify_rw[0] <= STDERR_FILENO)
if ((notify_rw[0] = fcntl(notify_rw[0], F_DUPFD, STDERR_FILENO + 1)) < 0)
goto fail;
if (notify_rw[1] <= STDERR_FILENO)
if ((notify_rw[1] = fcntl(notify_rw[1], F_DUPFD, STDERR_FILENO + 1)) < 0)
goto fail;
if ((pid = fork()) < 0)
goto fail;
if (pid > 0) {
/* Original process (parent): */
waitpid(pid, NULL, 0);
close(notify_rw[1]), notify_rw[1] = -1;
got = read(notify_rw[0], &a_byte, 1);
if (got < 0)
goto fail;
close(notify_rw[0]);
errno = 0;
return got == 0 ? INIT_FAILURE : (enum init_status)0;
}
/* Intermediary process (child): */
close(notify_rw[0]), notify_rw[0] = -1;
if (setsid() < 0)
goto fail;
if ((pid = fork()) < 0)
goto fail;
if (pid > 0) {
/* Intermediary process (parent): */
return (enum init_status)0;
}
/* Daemon process (child): */
/* Replace std* with /dev/null */
fd = open("/dev/null", O_RDWR);
if (fd < 0 ||
dup2(fd, STDIN_FILENO) < 0 ||
dup2(fd, STDOUT_FILENO) < 0 ||
(keep_stderr && dup2(fd, STDERR_FILENO) < 0))
goto fail;
if (fd > STDERR_FILENO)
close(fd);
fd = -1;
/* Update PID file */
fd = open(pidpath, O_WRONLY);
if (fd < 0)
goto fail;
if (dprintf(fd, "%llu\n", (unsigned long long)getpid()) < 0)
goto fail;
close(fd);
fd = -1;
/* Notify */
if (write(notify_rw[1], &a_byte, 1) <= 0)
goto fail;
close(notify_rw[1]);
return INIT_SUCCESS;
fail:
saved_errno = errno;
if (fd >= 0)
close(fd);
if (notify_rw[0] >= 0)
close(notify_rw[0]);
if (notify_rw[1] >= 0)
close(notify_rw[1]);
errno = saved_errno;
return INIT_FAILURE;
}
/**
* Initialise the process
*
* @param foreground Keep process in the foreground
* @param keep_stderr Keep stderr open
* @param query Was -q used, see `main` for description
* @return An `enum init_status` value or an exit value
*/
static enum init_status
initialise(int foreground, int keep_stderr, int query)
{
struct rlimit rlimit;
size_t i, n;
sigset_t mask;
int s;
enum init_status r;
/* Zero out some memory so it can be destroyed safely. */
memset(&site, 0, sizeof(site));
if (!query) {
/* Close all file descriptors above stderr */
if (getrlimit(RLIMIT_NOFILE, &rlimit) || rlimit.rlim_cur == RLIM_INFINITY)
n = 4 << 10;
else
n = (size_t)(rlimit.rlim_cur);
for (i = STDERR_FILENO + 1; i < n; i++)
close((int)i);
/* Set umask, reset signal handlers, and reset signal mask */
umask(0);
for (s = 1; s < _NSIG; s++)
signal(s, SIG_DFL);
if (sigfillset(&mask))
perror(argv0);
else
sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
/* Get method */
if (method < 0 && libgamma_list_methods(&method, 1, 0) < 1)
return fprintf(stderr, "%s: no adjustment method available\n", argv0), -1;
/* Go no further if we are just interested in the adjustment method and site */
if (query)
return INIT_SUCCESS;
/* Get site */
if (initialise_site() < 0)
goto fail;
/* Get PID file and socket pathname */
if (!(pidpath = get_pidfile_pathname()) ||
!(socketpath = get_socket_pathname()))
goto fail;
/* Create PID file */
if ((r = create_pidfile(pidpath)) < 0) {
free(pidpath);
pidpath = NULL;
if (r == -2)
return INIT_RUNNING;
goto fail;
}
/* Get partitions and CRTC:s */
if (initialise_crtcs() < 0)
goto fail;
/* Get CRTC information */
if (outputs_n && !(outputs = calloc(outputs_n, sizeof(*outputs))))
goto fail;
if (initialise_gamma_info() < 0)
goto fail;
/* Sort outputs */
qsort(outputs, outputs_n, sizeof(*outputs), output_cmp_by_name);
/* Load current gamma ramps */
store_gamma();
/* Preserve current gamma ramps at priority=0 if -p */
if (preserve && preserve_gamma() < 0)
goto fail;
/* Create socket and start listening */
if (create_socket(socketpath) < 0)
goto fail;
/* Get the real pathname of the process's binary, in case
* it is relative, so we can re-execute without problem. */
if (*argv0 != '/' && strchr(argv0, '/') && !(argv0_real = realpath(argv0, NULL)))
goto fail;
/* Change directory to / to avoid blocking umounting */
if (chdir("/") < 0)
perror(argv0);
/* Set up signal handlers */
if (set_up_signals() < 0)
goto fail;
/* Place in the background unless -f */
if (!foreground) {
return daemonise(keep_stderr);
} else {
/* Signal the spawner that the service is ready */
close(STDOUT_FILENO);
/* Avoid potential catastrophes that would occur if a library
* that is being used was so mindless as to write to stdout. */
if (dup2(STDERR_FILENO, STDOUT_FILENO) < 0)
perror(argv0);
}
return INIT_SUCCESS;
fail:
return INIT_FAILURE;
}
/**
* Deinitialise the process
*
* @param full Perform a full deinitialisation, shall be
* done iff the process is going to re-execute
*/
static void
destroy(int full)
{
if (full) {
disconnect_all();
close_socket(socketpath);
free(argv0_real);
if (outputs && connected)
restore_gamma();
}
state_destroy();
free(socketpath);
if (full && pidpath)
unlink(pidpath);
free(pidpath);
}
#if defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wcast-align"
#endif
/**
* Marshal the state of the process
*
* @param buf Output buffer for the marshalled data,
* `NULL` to only measure how large the
* buffer needs to be
* @return The number of marshalled bytes
*/
static size_t
marshal(void *restrict buf)
{
size_t off = 0, n;
char *restrict bs = buf;
if (bs)
*(int *)&bs[off] = MARSHAL_VERSION;
off += sizeof(int);
n = strlen(pidpath) + 1;
if (bs)
memcpy(&bs[off], pidpath, n);
off += n;
n = strlen(socketpath) + 1;
if (bs)
memcpy(&bs[off], socketpath, n);
off += n;
off += state_marshal(bs ? &bs[off] : NULL);
return off;
}
/**
* Unmarshal the state of the process
*
* @param buf Buffer with the marshalled data
* @return The number of marshalled bytes, 0 on error
*/
GCC_ONLY(__attribute__((__nonnull__)))
static size_t
unmarshal(const void *restrict buf)
{
size_t off = 0, n;
const char *restrict bs = buf;
if (*(const int *)&bs[off] != MARSHAL_VERSION) {
fprintf(stderr, "%s: re-executing to incompatible version, sorry about that\n", argv0);
errno = 0;
return 0;
}
off += sizeof(int);
n = strlen(&bs[off]) + 1;
if (!(pidpath = memdup(&bs[off], n)))
return 0;
off += n;
n = strlen(&bs[off]) + 1;
if (!(socketpath = memdup(&bs[off], n)))
return 0;
off += n;
off += n = state_unmarshal(&bs[off]);
if (!n)
return 0;
return off;
}
#if defined(__clang__)
# pragma GCC diagnostic pop
#endif
/**
* Do minimal initialisation, unmarshal the state of
* the process and merge with new state
*
* @param statefile The state file
* @return Zero on success, -1 on error
*/
GCC_ONLY(__attribute__((__nonnull__)))
static int
restore_state(const char *restrict statefile)
{
void *marshalled = NULL;
int fd = -1, saved_errno;
size_t r, n;
if (set_up_signals() < 0)
goto fail;
fd = open(statefile, O_RDONLY);
if (fd < 0)
goto fail;
if (!(marshalled = nread(fd, &n)))
goto fail;
close(fd);
fd = -1;
unlink(statefile);
statefile = NULL;
r = unmarshal(marshalled);
if (!r)
goto fail;
if (r != n) {
fprintf(stderr, "%s: unmarshalled state file was %s than the unmarshalled state: read %zu of %zu bytes\n",
argv0, n > r ? "larger" : "smaller", r, n);
errno = 0;
goto fail;
}
free(marshalled);
marshalled = NULL;
if (connected) {
connected = 0;
if (reconnect() < 0)
goto fail;
}
return 0;
fail:
saved_errno = errno;
if (fd >= 0)
close(fd);
free(marshalled);
errno = saved_errno;
return -1;
}
/**
* Reexecute the server
*
* Returns only on failure
*
* @return Pathname of file where the state is stored,
* `NULL` if the state is in tact
*/
static char *
reexecute(void)
{
char *statefile = NULL;
char *statebuffer = NULL;
size_t buffer_size;
int fd = -1, saved_errno;
statefile = get_state_pathname();
if (!statefile)
goto fail;
buffer_size = marshal(NULL);
statebuffer = malloc(buffer_size);
if (!statebuffer)
goto fail;
if (marshal(statebuffer) != buffer_size) {
fprintf(stderr, "%s: internal error\n", argv0);
errno = 0;
goto fail;
}
fd = open(statefile, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd < 0)
goto fail;
if (nwrite(fd, statebuffer, buffer_size) != buffer_size)
goto fail;
free(statebuffer);
statebuffer = NULL;
if (close(fd) < 0) {
fd = -1;
if (errno != EINTR)
goto fail;
}
fd = -1;
destroy(0);
execlp(argv0_real ? argv0_real : argv0, argv0, "- ", statefile, NULL);
free(argv0_real);
argv0_real = NULL;
return statefile;
fail:
saved_errno = errno;
free(statebuffer);
if (fd >= 0)
close(fd);
if (statefile != NULL) {
unlink(statefile);
free(statefile);
}
errno = saved_errno;
return NULL;
}
/**
* Print the response for the -q option
*
* @param query See -q for `main`, must be atleast 1
* @return Zero on success, -1 on error
*/
static int
print_method_and_site(int query)
{
const char *restrict methodname = NULL;
const char *const_sitename;
char *p;
if (query == 1) {
switch (method) {
#define X(C, N) case C: methodname = N; break;
LIST_ADJUSTMENT_METHODS;
#undef X
default:
if (printf("%i\n", method) < 0)
return -1;
break;
}
if (methodname)
if (printf("%s\n", methodname) < 0)
return -1;
}
if (!sitename)
if ((const_sitename = libgamma_method_default_site(method)))
if (!(sitename = memdup(const_sitename, strlen(const_sitename) + 1)))
return -1;
if (sitename) {
switch (method) {
case LIBGAMMA_METHOD_X_RANDR:
case LIBGAMMA_METHOD_X_VIDMODE:
if ((p = strrchr(sitename, ':')))
if ((p = strchr(p, '.')))
*p = '\0';
break;
default:
break;
}
}
if (sitename && query == 1)
if (printf("%s\n", sitename) < 0)
return -1;
if (query == 2) {
site.method = method;
site.site = sitename;
sitename = NULL;
socketpath = get_socket_pathname();
if (!socketpath)
return -1;
if (printf("%s\n", socketpath) < 0)
return -1;
}
if (fflush(stdout))
return -1;
return 0;
}
/**
* Print usage information and exit
*/
#if defined(__GNU__) || defined(__clang__)
__attribute__((__noreturn__))
#endif
static void
usage(void)
{
fprintf(stderr, "usage: %s [-m method] [-s site] [-fkpq]\n", argv0);
exit(1);
}
#if defined(__clang__)
# pragma GCC diagnostic ignored "-Wdocumentation-unknown-command"
#endif
/**
* Must not be started without stdin, stdout, or stderr (may be /dev/null)
*
* argv[0] must refer to the real command name or pathname,
* otherwise, re-execute will not work
*
* The process closes stdout when the socket has been created
*
* @signal SIGUSR1 Re-execute to updated process image
* @signal SIGUSR2 Dump the state of the process to standard error
* @signal SIGINFO Ditto
* @signal SIGTERM Terminate the process gracefully
* @signal SIGRTMIN+0 Disconnect from the site
* @signal SiGRTMIN+1 Reconnect to the site
*
* @param argc The number of elements in `argv`
* @param argv Command line arguments. Recognised options:
* -s SITENAME
* The site to which to connect
* -m METHOD
* Adjustment method name or adjustment method number
* -p
* Preserve current gamma ramps at priority 0
* -f
* Do not fork the process into the background
* -k
* Keep stderr open
* -q
* Print the select (possiblity default) adjustment
* method on the first line in stdout, and the
* selected (possibility defasult) site on the second
* line in stdout, and exit. If the site name is `NULL`,
* the second line is omitted. This is indented to
* be used by clients to figure out to which instance
* of the service it should connect. Use twice to
* simply ge the socket pathname, an a terminating LF.
* By combining -q and -m you can enumerate the name
* of all recognised adjustment method, start from 0
* and work up until a numerical adjustment method is
* returned.
* @return 0: Successful
* 1: An error occurred
* 2: Already running
*/
int
main(int argc, char *argv[])
{
int rc = 1, foreground = 0, keep_stderr = 0, query = 0, r;
char *statefile = NULL, *statefile_ = NULL;
ARGBEGIN {
case 's':
sitename = EARGF(usage());
/* To simplify re-exec: */
sitename = memdup(sitename, strlen(sitename) + 1);
if (!sitename)
goto fail;
break;
case 'm':
method = get_method(EARGF(usage()));
if (method < 0)
goto fail;
break;
case 'p': preserve = 1; break;
case 'f': foreground = 1; break;
case 'k': keep_stderr = 1; break;
case 'q': query = 1 + !!query; break;
case ' ': /* Internal, do not document */
statefile = statefile_ = EARGF(usage());
break;
default:
usage();
}
ARGEND;
if (argc > 0)
usage();
restart:
if (!statefile) {
switch ((r = initialise(foreground, keep_stderr, query))) {
case INIT_SUCCESS: break;
case INIT_RUNNING: rc = 2; /* fall through */
case INIT_FAILURE: goto fail;
default: return r;
}
} else if (restore_state(statefile) < 0) {
goto fail;
} else {
if (statefile != statefile_)
free(statefile);
unlink(statefile);
statefile = NULL;
}
if (query) {
if (print_method_and_site(query) < 0)
goto fail;
goto done;
}
reenter_loop:
if (main_loop() < 0)
goto fail;
if (reexec && !terminate) {
statefile = reexecute();
if (statefile) {
perror(argv0);
fprintf(stderr, "%s: restoring state without re-executing\n", argv0);
reexec = 0;
goto restart;
}
perror(argv0);
fprintf(stderr, "%s: continuing without re-executing\n", argv0);
reexec = 0;
goto reenter_loop;
}
done:
rc = 0;
deinit:
if (statefile)
unlink(statefile);
if (reexec)
free(statefile);
destroy(1);
return rc;
fail:
if (errno)
perror(argv0);
goto deinit;
}
|