source: fact/BIASctrl/BIASctrl.cc@ 10018

Last change on this file since 10018 was 9917, checked in by ogrimm, 14 years ago
Initial commit of BIASctrl
File size: 2.9 KB
Line 
1/**************************************************************\
2
3 Control program for the fACT G-APD bias supply
4
5 Adapted from previous version 'hvcontrol'.
6
7 Oliver Grimm, August 2010
8
9\**************************************************************/
10
11#include <stdio.h>
12
13#include "ProcessIO.h"
14
15#include <readline/readline.h>
16#include <readline/history.h>
17
18#define LOCKFILE "/tmp/CTX_HV_LOCK"
19
20//
21// Remove lock file before running default signal code
22//
23void CrashHandler(int Signal) {
24
25 remove(LOCKFILE);
26 printf("Caught signal number %d. Removing lockfile and performing standard signal action. Good luck.\n",Signal);
27 signal(Signal, SIG_DFL);
28 raise(Signal);
29}
30
31//
32// Dummy handler to return from blocking syscalls
33//
34void DummyHandler(int) {};
35
36//
37// This function will be implicitly called by exit()
38//
39void ExitFunction() {
40
41 if (remove(LOCKFILE) == -1) {
42 printf("Could not remove lock file %s (%s)\n", LOCKFILE, strerror(errno));
43 }
44}
45
46//
47// Main program
48//
49int main() {
50
51 char str[MAX_COM_SIZE], *Command;
52 int Lock;
53
54 // Assure only one instance runs
55 // O_EXCL with O_CREAT assure that the lock file cannot be
56 // opened by another instance, i.e. there are no parallel write accesses
57 if((Lock = open(LOCKFILE,O_WRONLY|O_CREAT|O_EXCL,S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)) == -1) {
58 if(errno==EEXIST) {
59 printf("Error: Lock file already existing\n");
60 sprintf(str, "paste %s -s -d ' '", LOCKFILE);
61 system(str);
62 }
63 else printf("Could not create lock file %s (%s)\n", LOCKFILE, strerror(errno));
64 exit(EXIT_FAILURE);
65 }
66 close(Lock);
67 sprintf(str,"echo Created >%s; date >>%s; echo by $USER@$HOSTNAME>>%s", LOCKFILE, LOCKFILE, LOCKFILE);
68 system(str);
69
70 system("clear");
71 printf("\n*** Bias control (compiled %s, %s) ***\n\n", __DATE__, __TIME__);
72
73 // Install signal handler and set signal SIGUSR1 to interrupt blocking system calls
74 signal(SIGUSR1, &DummyHandler);
75 siginterrupt (SIGUSR1, true);
76
77 // Assure lock file is deleted in case of a program crash or call to exit()
78 signal(SIGILL, &CrashHandler);
79 signal(SIGABRT, &CrashHandler);
80 signal(SIGFPE, &CrashHandler);
81 signal(SIGSEGV, &CrashHandler);
82 signal(SIGBUS, &CrashHandler);
83 atexit(&ExitFunction);
84
85 // Construct main instance
86 static ProcessIO M;
87
88 // These signals were set during construction of EvidenceServer
89 signal(SIGQUIT, &CrashHandler); // CTRL-Backspace
90 signal(SIGINT, &CrashHandler); // CTRL-C
91 signal(SIGHUP, &CrashHandler); // Terminal closed
92 signal(SIGTERM, &CrashHandler);
93
94 // Handle command-line input
95 while (!M.ExitRequest) {
96 Command = readline("\rBias> ");
97
98 // NULL returned if interrupted by signal
99 if (Command == NULL) continue;
100 if (strlen(Command) > 0) add_history(Command);
101
102 // Process command (via DIM gives automatic thread serialisation)
103 DimClient::sendCommand("Bias/Command", Command);
104 free(Command);
105 }
106}
Note: See TracBrowser for help on using the repository browser.