File: sleepctl.c

package info (click to toggle)
sleepd 2.15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 192 kB
  • sloc: ansic: 1,535; makefile: 50; sh: 36
file content (132 lines) | stat: -rw-r--r-- 2,236 bytes parent folder | download | duplicates (10)
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
/*
 * sleepd control program
 *
 * Copyright 2000, 2001 Joey Hess <joeyh@kitenet.net> under the terms of
 * the GNU GPL.
 */

#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "sleepd.h"

/* TODO: file locking */

pid_t getpid () {
	int f;
	char buf[10];
	pid_t pid;

	if ((f=open(PID_FILE, O_RDONLY)) == -1) {
		return -1;
	}
	if (read(f, buf, 9) == -1) {
		return -1;
	}
	pid=atoi(buf);
	if (pid < 1) {
		fprintf(stderr, "bad number in pid file");
		return -1;
	}
	return pid;
}

void usage () {
	fprintf(stderr, "Usage: sleepctl [on|off|status]\n");
}

int read_control () {
	int f;
	char buf[8];
	
	if ((f=open(CONTROL_FILE, O_RDONLY)) == -1) {
		return 0; // default
	}
	
	if (read(f, buf, 7) == -1) {
		perror("read");
		exit(2);
	}
	return atoi(buf);
}

int write_control (int value) {
	int f;
	char buf[10];
	pid_t pid;
	
	if ((f=open(CONTROL_FILE, O_WRONLY | O_CREAT, 0644)) == -1) {
		perror(CONTROL_FILE);
		exit(2);
	}
	snprintf(buf, 9, "%i\n", value);
	write(f, buf, strlen(buf));
	close(f);
	
	if ((pid = getpid()) == -1) {
		fprintf(stderr, "unable to determine daemon pid\n");
		exit(1);
	}
	if (kill(pid, SIGHUP) == -1) {
		perror("problem sending SIGHUP");
		exit(3);
	}
	
	return value;
}

void verify_daemon_running () {
	pid_t pid;
	if ((pid = getpid()) == -1 ||
	    (kill(getpid(), 0) == -1)) {
		printf("sleepd is not running\n");
	}
}

void show_status (int value) {
	switch (value) {
		case 0:
			printf("sleeping enabled\n");
			break;
		case 1:
			printf("sleeping disabled\n");
			break;
		case 2:
			printf("sleeping disabled (twice)\n");
			break;
		default:
			printf("sleeping disabled (%i times)\n", value);
	}
}

int main (int argc, char **argv) {
	int value;
	
	if (argc != 2) {
		usage();
		exit(1);
	}
	else if (strcmp(argv[1],"on") == 0) {
		value=read_control();
		if (value > 0)
			value--;
		show_status(write_control(value));
	}
	else if (strcmp(argv[1],"off") == 0) {
		show_status(write_control(read_control() + 1));
	}
	else if (strcmp(argv[1],"status") == 0) {
		verify_daemon_running();
		show_status(read_control());
	}
	else {
		usage();
		exit(1);
	}
	exit(0);
}