source: drsdaq/drsdaq.cpp@ 212

Last change on this file since 212 was 211, checked in by ogrimm, 15 years ago
Fix for exit signalling from DimCommand thread
File size: 3.2 KB
Line 
1/**************************************************************\
2
3 drsdaq
4
5 Main program for data acquisition, starts threads for console input.
6
7 Original program by S. Commichau.
8
9 Oliver Grimm, May 2010
10
11\**************************************************************/
12
13#define LOCKFILE "/tmp/CT3_DAQ_LOCK"
14
15#include <stdio.h>
16#include <signal.h>
17#include <readline/readline.h>
18#include <readline/history.h>
19
20#include "DAQReadout.h"
21#include "HVFeedback.h"
22
23// Function prototypes
24void CrashHandler(int);
25void ExitFunction();
26
27// ================
28// Main program
29// ================
30//
31// Several unlikely system call failures are handled via throwing an exception.
32
33int main() {
34
35 char str[MAX_COM_SIZE], *Command;
36 int LockDescriptor;
37
38 // Readline library uses getc() (allows interruption by signal)
39 rl_getc_function = getc;
40
41 // writev() in DAQ thread needs to be able to write at least 3 chunks
42 if(IOV_MAX < 3) {
43 printf("Fatal error: IOV_MAX is less than 3, cannot use writev() to write event data.\n");
44 exit(EXIT_FAILURE);
45 }
46
47 // Assure only one instance of program runs (lock creator written to log file)
48 // Lock file deleted by ExitFunction()
49 if((LockDescriptor = open(LOCKFILE,O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)) == -1) {
50 if(errno==EEXIST) {
51 printf("Error: Lock file already existing\n");
52 snprintf(str, sizeof(str), "paste %s -s -d ' '",LOCKFILE);
53 system(str);
54 }
55 else printf("Could not create lock file %s (%s)\n", LOCKFILE, strerror(errno));
56 exit(EXIT_FAILURE);
57 }
58 close(LockDescriptor);
59 sprintf(str,"echo Created >%s; date >>%s; echo by $USER@$HOSTNAME >>%s",LOCKFILE,LOCKFILE,LOCKFILE);
60 system(str);
61
62 system("clear");
63 printf("\n*** DRS readout (built %s, %s, revision %s) *** \n\n",__DATE__, __TIME__, REVISION);
64
65 // Construct main instance (static ensures destructor is called with exit())
66 static DAQReadout M;
67
68 // Set signal and exit handlers
69 atexit(&ExitFunction);
70 signal(SIGILL, &CrashHandler);
71 signal(SIGABRT, &CrashHandler);
72 signal(SIGFPE, &CrashHandler);
73 signal(SIGSEGV, &CrashHandler);
74 signal(SIGBUS, &CrashHandler);
75
76 // Command loop
77 while (!M.ExitRequest) {
78 // Assemble prompt
79 if (M.NumBoards == 0) snprintf(M.Prompt,sizeof(M.Prompt),"\rDAQ> ");
80 else if (M.FirstBoard == M.LastBoard) snprintf(M.Prompt,sizeof(M.Prompt),"\rDAQ|B%d> ",M.FirstBoard);
81 else snprintf(M.Prompt,sizeof(M.Prompt),"\rDAQ|B%d-%d> ",M.FirstBoard,M.LastBoard);
82
83 // Read Command
84 Command = readline(M.Prompt);
85 if (Command == NULL) continue;
86 if(strlen(Command)>0) add_history(Command);
87
88 // Process command
89 DimClient::sendCommand(SERVER_NAME"/Command", Command);
90
91 free(Command);
92 }
93}
94
95
96//*****************
97// Signal handlers
98//*****************
99
100// Remove lock file before running default signal code
101void CrashHandler(int Signal) {
102 remove(LOCKFILE);
103 printf("Caught signal number %d. Removing lockfile and performing standard signal action. Good luck.\n",Signal);
104 signal(Signal, SIG_DFL);
105 raise(Signal);
106}
107
108// This function will be implicitly called by exit()
109void ExitFunction() {
110
111 if (remove(LOCKFILE)==-1) {
112 printf("Could not remove lock file %s (%s)\n", LOCKFILE, strerror(errno));
113 }
114
115 return;
116}
Note: See TracBrowser for help on using the repository browser.