summaryrefslogtreecommitdiff
path: root/src/main/remote.c
blob: 4be342d0d82e0c57692345c3da3a88c73ab0bc4b (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Remote-control functions.
 *
 * Copyright 2012 Andrew Wood, distributed under the Artistic License 2.0.
 */

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

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>


struct remote_msg {
	long mtype;
	unsigned char progress;		 /* progress bar flag */
	unsigned char timer;		 /* timer flag */
	unsigned char eta;		 /* ETA flag */
	unsigned char rate;		 /* rate counter flag */
	unsigned char average_rate;	 /* average rate counter flag */
	unsigned char bytes;		 /* bytes transferred flag */
	unsigned long long rate_limit;	 /* rate limit, in bytes per second */
	unsigned long long buffer_size;	 /* buffer size, in bytes (0=default) */
	unsigned long long size;	 /* total size of data */
	double interval;		 /* interval between updates */
	unsigned int width;		 /* screen width */
	unsigned int height;		 /* screen height */
	char name[256];			 /* RATS: ignore */
};


static opts_t remote__opts = NULL;


/*
 * Return a key for use with msgget() which will be unique to the current
 * user.
 *
 * We can't just use ftok() because the queue needs to be user-specific
 * so that a user cannot send messages to another user's process, and we
 * can't easily find out the terminal a given process is connected to in a
 * cross-platform way.
 */
static key_t remote__genkey(opts_t opts)
{
	int uid;
	key_t key;

	uid = geteuid();
	if (uid < 0)
		uid = 0;

	key = ftok("/tmp", 'P') | uid;

	return key;
}


/*
 * Return a message queue ID that is unique to the current user and the
 * given process ID, or -1 on error.
 */
static int remote__msgget(opts_t opts)
{
	return msgget(remote__genkey(opts), IPC_CREAT | 0600);
}


/*
 * Set the options of a remote process by setting up an IPC message queue,
 * sending a message containing the new options, and then sending a SIGUSR1
 * so the process knows it has a message to read.
 *
 * Returns nonzero on error.
 */
int remote_set(opts_t opts)
{
	struct remote_msg msgbuf;
	int msgid;

	memset(&msgbuf, 0, sizeof(msgbuf));
	msgbuf.mtype = opts->remote;
	msgbuf.progress = opts->progress;
	msgbuf.timer = opts->timer;
	msgbuf.eta = opts->eta;
	msgbuf.rate = opts->rate;
	msgbuf.average_rate = opts->average_rate;
	msgbuf.rate_limit = opts->rate_limit;
	msgbuf.buffer_size = opts->buffer_size;
	msgbuf.size = opts->size;
	msgbuf.interval = opts->interval;
	msgbuf.width = opts->width;
	msgbuf.height = opts->height;
	if (opts->name != NULL) {
		strncpy(msgbuf.name, opts->name, sizeof(msgbuf.name) - 1);
	}

	msgid = remote__msgget(opts);
	if (msgid < 0) {
		fprintf(stderr, "%s: %s\n", opts->program_name,
			strerror(errno));
		return 1;
	}

	if (msgsnd(msgid, &msgbuf, sizeof(msgbuf) - sizeof(long), 0) != 0) {
		fprintf(stderr, "%s: %s\n", opts->program_name,
			strerror(errno));
		return 1;
	}

	if (kill(opts->remote, SIGUSR1) != 0) {
		fprintf(stderr, "%s: %s\n", opts->program_name,
			strerror(errno));
		return 1;
	}

	return 0;
}


/*
 * Handle SIGUSR1 by replacing the current process's options with those
 * being passed in via IPC message. The message queue is deleted afterwards.
 */
static void remote__sig_usr1(int s)
{
	struct remote_msg msgbuf;
	struct msqid_ds qbuf;
	ssize_t got;
	int msgid;

	memset(&msgbuf, 0, sizeof(msgbuf));

	msgid = remote__msgget(remote__opts);
	if (msgid < 0) {
		return;
	}

	got =
	    msgrcv(msgid, &msgbuf, sizeof(msgbuf) - sizeof(long), getpid(),
		   IPC_NOWAIT);
	if (got < 0) {
		msgctl(msgid, IPC_RMID, &qbuf);
		return;
	}

	if (msgctl(msgid, IPC_RMID, &qbuf) == 0) {
		if (qbuf.msg_qnum < 1) {
			msgctl(msgid, IPC_RMID, &qbuf);
		}
	}

	if ((got < 1) || (remote__opts == NULL)) {
		return;
	}


	remote__opts->progress = msgbuf.progress;
	remote__opts->timer = msgbuf.timer;
	remote__opts->eta = msgbuf.eta;
	remote__opts->rate = msgbuf.rate;
	remote__opts->average_rate = msgbuf.average_rate;

	if (msgbuf.rate_limit > 0)
		remote__opts->rate_limit = msgbuf.rate_limit;
	if (msgbuf.buffer_size > 0) {
		remote__opts->buffer_size = msgbuf.buffer_size;
		pv_set_buffer_size(msgbuf.buffer_size, 1);
	}
	if (msgbuf.size > 0)
		remote__opts->size = msgbuf.size;
	if (msgbuf.interval > 0)
		remote__opts->interval = msgbuf.interval;
	if (msgbuf.width > 0)
		remote__opts->width = msgbuf.width;
	if (msgbuf.height > 0)
		remote__opts->height = msgbuf.height;
	if (msgbuf.name[0] != 0)
		remote__opts->name = strdup(msgbuf.name);
}


/*
 * Initialise handling of SIGUSR1 so that remote control messages can be
 * processed correctly.
 */
void remote_sig_init(opts_t opts)
{
	struct sigaction sa;

	remote__opts = opts;

	sa.sa_handler = remote__sig_usr1;
	sigemptyset(&(sa.sa_mask));
	sa.sa_flags = 0;
	sigaction(SIGUSR1, &sa, NULL);
}

/* EOF */