File: dummyups.c

package info (click to toggle)
nut 0.45.5-rel-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,984 kB
  • ctags: 2,146
  • sloc: ansic: 22,216; sh: 1,138; makefile: 405
file content (140 lines) | stat: -rw-r--r-- 3,307 bytes parent folder | download
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
/* dummyups.c - Dummy model driver for testing shutdowns nondestructively

   Copyright (C) 2000  Russell Kroll <rkroll@exploits.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/termios.h>

#define _MIT_POSIX_THREADS 1
#include <pthread.h>

#define INFO_MAX 8

#include "config.h"
#include "proto.h"
#include "shared.h"
#include "version.h"
#include "upscommon.h"
#include "common.h"

	static	pthread_t	thread;
	static	pthread_attr_t	threadattr;

void initinfo (void)
{
	create_info (INFO_MAX, 1);

	/* setup the basics */

	addinfo (INFO_MFR, "Testing", 0, 0);
	addinfo (INFO_MODEL, "Dummy UPS", 0, 0);
	addinfo (INFO_STATUS, "OL", 0, 0);
}

/* background thread - keep the struct current so upsd is happy */
static void *updateinfo (void *ptr)
{
	for (;;) {
/*		printf ("writeinfo\n"); */
		writeinfo();
		sleep (2);
	}
}

#if 0
/* install pointers to functions for msg handlers called from msgparse */
void setuphandlers()
{
	/* TODO: future */
}
#endif

int main (int argc, char **argv)
{
	int	res;
	char	exec[512], status[32], buf[128], *portname;
	FILE	*xd;

	printf ("Network UPS Tools - Dummy UPS driver 0.01 (%s)\n", UPS_VERSION);

	if (argc < 2)
		portname = "null";
	else
		portname = argv[1];

	snprintf (statefn, sizeof(statefn), "%s/dummyups-%s", STATEPATH, portname);

	printf ("State file: %s\n", statefn);

	droproot();
	initinfo();
	background();

	snprintf (pidfn, sizeof(pidfn), "dummyups-%s", portname);
	writepid(pidfn);

	pthread_attr_init(&threadattr);
	pthread_attr_setscope(&threadattr, PTHREAD_SCOPE_SYSTEM);
	res = pthread_create (&thread, &threadattr, updateinfo, NULL);

	strcpy (status, "OL");

	for (;;) {
		snprintf (exec, sizeof(exec), "%s %s %s %s %s %s %s %s %s",
			"Xdialog --title \"Network UPS Tools: Dummy UPS driver:",
			portname,
			"\" --stdout --radiolist \"Pick UPS status\" 20 50 3",
			"\"OL\" \"Online\"",
			strcmp(status, "OL") ? "off" : "ON",
			"\"OB\" \"On Battery\"",
			strcmp(status, "OB") ? "off" : "ON",
			"\"OB LB\" \"On Battery + Low Battery\"",
			strcmp(status, "OB LB") ? "off" : "ON");

		/* call Xdialog */

		xd = popen (exec, "r");

		if (!xd)
			fatal("popen");

		/* read choice */

		bzero (buf, sizeof(buf));
		fgets (buf, sizeof(buf), xd);
		pclose (xd);

		if (strlen(buf) > 1)
			buf[strlen(buf) - 1] = '\0';
		else
			exit (1);

		snprintf (status, sizeof(status), "%s", buf);
		setinfo(INFO_STATUS, "%s", status);
	}
}