source: fact/BIASctrl/BIASctrl.cc@ 10108

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