summaryrefslogtreecommitdiff
path: root/packages/sccd/files/scc-utils.c
blob: ab48ecb67775757fd255bb730d3562838ccc1a54 (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
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
/*
 * Copyright (c) 2006
 *	Protium Computing, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Protium Computing, Inc.
 * 4. The name of Protium Computing, Inc. may not be used to endorse or 
 *    promote products derived from this software without specific prior 
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PROTIUM COMPUTING ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL PROTIUM COMPUTING BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/syslog.h>
#include <sys/resource.h>
#include <termios.h>

#include "scc.h"

int scc_die = 0;

char
scc_cksum(int *i, int *j)
{
	unsigned char s;
	unsigned char *b;

	b = (unsigned char *)i;
	s = b[0] + b[1] + b[2] + b[3];
	b = (unsigned char *)j;
	s = s + b[0] + b[1] + b[2];
 	s = (s & 0x7f);

	return((char)s);
}

int 
scc_validate(int *i, int *j, int verbose)
{
	int r, s;

	/* if (verbose) printf("Packet: [0x%08x 0x%08x]\n", *i, *j); */

	switch (*i & SCC_POWERMASK) {
	case SCC_RUN:
		if (verbose) printf("\tPower: run\n");
		break;
	case SCC_STOP:
		if (verbose) printf("\tPower: stop\n");
		break;
	case SCC_ADVISESTOP:
		if (verbose) printf("\tPower: advise stop\n");
		break;
	case SCC_RESTART:
		if (verbose) printf("\tPower: restart\n");
		break;
	case SCC_ADVISERESTART:
		if (verbose) printf("\tPower: advise restart\n");
		break;
	case SCC_RESET:
		if (verbose) printf("\tPower: reset\n");
		break;
	case SCC_ADVISERESET:
		if (verbose) printf("\tPower: advise reset\n");
		break;
	default:
		if (verbose) printf("validate: bad power 0x%08x\n", *i);
		return(-1);	
	}

	switch (*i & SCC_LEDMASK) {
	case SCC_LEDOFF:
		if (verbose) printf("\tLed: off\n");
		break;
	case SCC_LEDBLUE:
		if (verbose) printf("\tLed: blue\n");
		break;
	case SCC_LEDRED:
		if (verbose) printf("\tLed: red\n");
		break;
	case SCC_LEDBLUEFLASH:
		if (verbose) printf("\tLed: blue flash\n");
		break;
	case SCC_LEDREDFLASH:
		if (verbose) printf("\tLed: red flash\n");
		break;
	case SCC_LEDALTERNATE1:
		if (verbose) printf("\tLed: alternate1\n");
		break;
	case SCC_LEDALTERNATE3:
		if (verbose) printf("\tLed: alternate3\n");
		break;
	default:
		if (verbose) printf("validate: bad led value 0x%08x\n", *i);
		return(-1);	
	}	

	r = ((*i & SCC_LEDRATEMASK)  >> SCC_LEDRATESHIFT);

	if (verbose) {
		printf("\tLed Flash Rate: %d\n", r);
	}

	if (r >= SCC_LEDRATEHI) 
		printf("warning: led rate too high - led on (%d)\n", r);
	if (r <= SCC_LEDRATELO) 
		printf("warning: led rate too low - led off (%d)\n", r);


	switch (*i & SCC_FANMASK) {
	case SCC_FANAUTO:
		if (verbose) printf("\tFan: auto\n");
		break;
	case SCC_FANON:	
		if (verbose) printf("\tFan: on\n");
		break;
	default:
		if (verbose) printf("validate: bad fan value 0x%08x\n", *i);
		return(-1);	
	}

	r = ((*j & SCC_FANTEMPONMASK)  >> SCC_FANTEMPONSHIFT);
	s = ((*j & SCC_FANTEMPOFFMASK) >> SCC_FANTEMPOFFSHIFT);

	if (verbose) {
		printf("\tFan On  Temprature: %dC\n", r);
		printf("\tFan Off Temprature: %dC\n", s);
	}

	if (r >= SCC_FANTEMPONHI) 
		printf("warning: fan on temp too high - fan off (%dC)\n", r);
	if (s <= SCC_FANTEMPONLO) 
		printf("warning: fan on temp too low - fan on (%dC)\n", s);
	if ((r - s) < SCC_FANTEMPDIFF) {
		printf("warning: fan on/off temp too close or on < off \n");
		printf("warning:\tfan on (%dC) fan off (%dC) \n", r, s);
	}

	r = *j & SCC_IDMASK;
	if ((r != SCC_HOST) && 
	    (r != SCC_CTLR00) &&  
	    (r != SCC_CTLR12)) {
		printf("warning: bad id %08x\n", r);
	}	

	r = *j & SCC_CKSUMMASK; 
	if (r != scc_cksum(i, j)) {
		printf("warning: checksum incorrect (%02x != %02x)\n", 
		       r, scc_cksum(i, j));
	}
	return(0);	
}

int
scc_setval(int *i, int mask, int val)
{
	/* clear the field */
	*i = (*i & (mask ^ 0xffffffff));

	/* insert the field */
	*i = (*i ^ val);
	return(0);
}

int
scc_setrate(int *i, int mask, int shift, int val)
{
	/* Assumption: val is < field width */

	/* clear the field */
	*i = (*i & (mask ^ 0xffffffff));

	/* insert the field */
	*i = (*i ^ (val << shift));
	return(0);
}

int 
scc_defaults(int *i, int *j)
{
	scc_setval(i,  SCC_POWERMASK, SCC_POWERDEFAULT);
	scc_setval(i,  SCC_LEDMASK, SCC_LEDDEFAULT);	
	scc_setrate(i, SCC_LEDRATEMASK, SCC_LEDRATESHIFT, SCC_LEDRATEDEFAULT);
	scc_setval(i,  SCC_FANMASK, SCC_FANDEFAULT);

	scc_setrate(j, SCC_FANTEMPONMASK, 
		SCC_FANTEMPONSHIFT, SCC_FANTEMPONDEFAULT);
	scc_setrate(j, SCC_FANTEMPOFFMASK, 
		SCC_FANTEMPOFFSHIFT, SCC_FANTEMPOFFDEFAULT);
	return(0);
}

void 
scc_sighandler(int sig)
{
	/*
	 * Just catch quit and term and set die
	 */
	switch(sig) {
	case SIGQUIT:
	case SIGTERM:
		syslog(LOG_INFO, "Terminating");
		scc_die=1;
		break;
	}
}

int
scc_daemonize(int cores)
{
	int	fd, i;
	char	pidstr[20];
	pid_t	pid;

	struct rlimit limit[1] = {{ 0, 0 }};


	if (!cores) {
		/* 
		 * No corefiles please 
		 */
		if (getrlimit(RLIMIT_CORE, limit) == -1) {
			perror("getrlimit");
			return -1;
		}

		limit->rlim_cur = 0;

		if (setrlimit(RLIMIT_CORE, limit) != 0) {
			perror("setrlimit");
			return -1;
		}
	}
	
	/*
	 * Must be root
	 */
	if (getuid() != 0) {
		fprintf(stderr, "Must be root\n");
		return -1;
	}

	/* 
	 * If parent isn't init, make it init.
	 */
	if (getppid() != 1) {
    
		pid = fork();
		if (pid == -1) {
			return -1;
		}
		if (pid != 0) {
			exit(EXIT_SUCCESS);
		}

		setsid();

		/*
		 * ignore sig hup so that when our new padre exits
		 * the kinder won't exit
		 */
		signal(SIGHUP, SIG_IGN);

		pid = fork();
		if (pid == -1) {
			return -1;
		}
		if (pid != 0) {
			exit(EXIT_SUCCESS);
		}
	}

	/*
	 * Set working dir to root
	 */
	chdir("/");

	/*
	 * Make sure file creations are created with explicit perms
	 */
	umask(0);

	/*
	 * Close all FDs
	 */
	for (i = 0; i < sysconf(_SC_OPEN_MAX); i++) {
		close(i);
	}
	
	/* 
	 * set std in, out, err to /dev/null
	 */
	for (i = 0; i < 3; i++) {

		if ((fd = open("/dev/null", (i==0?O_RDONLY:O_WRONLY))) == -1) {
			perror("setting up stdin, stdout, stderr");
			return -1;
		}
		
		if (i != fd) {
			if (dup2(fd, i) == -1) {
				perror("setting up stdin, stdout, stderr");
				return -1;
			}
			close(fd);
		}
	}

	/*
	 * Open and lock the pid file. Ensures only one daemon
	 * Write our pid into the file 
	 */
	fd = open(SCC_PIDFILE, O_RDWR|O_CREAT, 0640);
	if (fd < 0) {
		return(-1);
		
	}
	
	if (lockf(fd, F_TLOCK, 0) < 0) {
		/* we are not alone */
		close(fd);
		return(-1);
	}

	sprintf(pidstr,"%-6d\n", getpid());
	write(fd, pidstr, strlen(pidstr)); 

	/*
	 * Open syslog
	 */
	openlog("sccd", LOG_PID, LOG_DAEMON);
	syslog(LOG_INFO, "Starting");

	/*
	 * Ignore some signals and handle others
	 */
	signal(SIGCHLD, SIG_IGN);
	signal(SIGTSTP, SIG_IGN);
	signal(SIGTTOU, SIG_IGN);
	signal(SIGTTIN, SIG_IGN);
	signal(SIGQUIT, scc_sighandler);
	signal(SIGTERM, scc_sighandler);

	return(0);
}