Belle II Software development
RFProcessManager.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include "daq/rfarm/manager/RFProcessManager.h"
10#include "daq/rfarm/manager/RFNSM.h"
11
12#include <fcntl.h>
13#include <stdio.h>
14#include <sys/types.h>
15#include <sys/wait.h>
16#include <unistd.h>
17
18#include <cstring>
19
20extern "C" {
21#include <nsm2/nsmlib2.h>
22#include <nsm2/nsmsys2.h>
23}
24
25using namespace std;
26using namespace Belle2;
27
28// Constructor/Destructor
29
30// *** OLD ***
31//RFProcessManager::RFProcessManager( string& nodename, string& conffile, string& infofile ) :
32// RFSharedMem (nodename.c_str()), RFNSM (nodename.c_str(), infofile.c_str() ), RFConf ( conffile.c_str() )
33
34RFProcessManager::RFProcessManager(char* /*nodename*/)
35{
36 // Create IO pipe for output logging
37 if (pipe2(m_iopipe, O_NONBLOCK) < 0) {
38 perror("pipe");
39 m_iopipe[0] = -1; m_iopipe[1] = -1;
40 }
41 FILE* f = fopen("pid.data", "w");
42 if (f != NULL) {
43 fprintf(f, "%d", getpid());
44 fclose(f);
45 }
46}
47
48RFProcessManager::~RFProcessManager()
49{
50}
51
52void RFProcessManager::signal_handler(int /*num*/)
53{
54}
55
56
57int RFProcessManager::Execute(char* scr, int nargs, char** args)
58{
59
60 printf("RFProcessManager : Execute : scr=%s, nargs=%d\n", scr, nargs);
61 // Fork processes
62 pid_t pid = fork();
63 if (pid == (pid_t) 0) { // In case of forked process
64 char argbuf[10][256];
65 char* argval[10];
66 int nargval = 0;
68 strcpy(argbuf[0], scr);
69 argval[nargval++] = argbuf[0];
70 // Fill argbuf
71 for (int i = 0; i < nargs; i++) {
72 strcpy(argbuf[nargval], args[i]);
73 argval[nargval] = argbuf[nargval];
74 nargval++;
75 // printf ( "argval[%d] = %s\n", i+1, argval[i+1] );
76 }
77 argval[nargval] = NULL;
78
79 // Connect stderr/out to write pipe[1], which is read by parent process thru pipe[0]
80 close(1);
81 dup2(m_iopipe[1], 1); // redirect stderr to pipe 1
82 close(2);
83 dup2(m_iopipe[1], 2); // redirect stdout to pipe 1
84 close(m_iopipe[0]); // Close read pipe in child process
85
86 // execute script in forked process by execvp()
87 printf("RFProcessManager : invoking %s\n", scr);
88 for (int i = 0; i < 10; i++) {
89 if (argval[i] == NULL) break;
90 printf("argval[%d] = %s\n", i, argval[i]);
91 }
92 fflush(stdout);
93 fflush(stderr);
94 execvp(scr, argval); // returns int stexe
95 perror("execvp!!");
96 fprintf(stderr, "missing file : %s\n", scr);
97 fflush(stderr);
98 exit(-1);
99 }
100 // Parent process (pid>0 : success, pid<0 : error )
101 printf("RFProcessManager : forked. pid=%d\n", pid);
102 //close(m_iopipe[1]);
103
104 m_pidlist.push_back(pid);
105 return pid;
106}
107
108int RFProcessManager::Execute(char* scr,
109 char* arg0, char* arg1, char* arg2, char* arg3,
110 char* arg4, char* arg5, char* arg6, char* arg7)
111{
112 char* arglist[8];
113 int narg = 0;
114 if (arg0 != NULL) {
115 arglist[0] = arg0;
116 narg++;
117 }
118 if (arg1 != NULL) {
119 arglist[1] = arg1;
120 narg++;
121 }
122 if (arg2 != NULL) {
123 arglist[2] = arg2;
124 narg++;
125 }
126 if (arg3 != NULL) {
127 arglist[3] = arg3;
128 narg++;
129 }
130 if (arg4 != NULL) {
131 arglist[4] = arg4;
132 narg++;
133 }
134 if (arg5 != NULL) {
135 arglist[5] = arg5;
136 narg++;
137 }
138 if (arg6 != NULL) {
139 arglist[6] = arg6;
140 narg++;
141 }
142 if (arg7 != NULL) {
143 arglist[7] = arg7;
144 narg++;
145 }
146 return Execute(scr, narg, arglist);
147}
148
149int RFProcessManager::CheckOutput()
150{
151 // Setup select parameter
152 fd_set fdset;
153
154 // Fetch fd
155 int nfd;
156 for (;;) {
157 FD_ZERO(&fdset);
158 int highest = 0;
159 if (m_iopipe[0] > 0) {
160 FD_SET(m_iopipe[0], &fdset);
161 highest = m_iopipe[0];
162 } else {
163 printf("Pipe for log is not availablle\n");
164 }
165 NSMcontext* nsmc = RFNSM::GetContext();
166 if (nsmc) {
167 if (nsmc->sock > highest) highest = nsmc->sock;
168 FD_SET(nsmc->sock, &fdset);
169 } else {
170 //printf("No NSM context is available\n");
171 }
172 // Time out parameter
173 struct timeval tv;
174 tv.tv_sec = 1;
175 tv.tv_usec = 1; // omajinai
176
177 if ((nfd = select(highest + 1, &fdset, NULL, NULL, &tv)) < 0) {
178 switch (errno) {
179 case EINTR: continue; // why? if we get a signal, we can return, too
180 case EAGAIN: continue;
181 default:
182 //close(m_iopipe[0]);
183 //m_iopipe[0] = -1;
184 return 0;
185 }
186 } else {
187 if (nsmc && FD_ISSET(nsmc->sock, &fdset)) {
188 // NSMCommunicator(nsmc).callContext();
189 char buf[NSM_TCPMSGSIZ];
190 if (nsmlib_recv(nsmc, reinterpret_cast<struct NSMtcphead_struct*>(buf), 1000) < 0)
191 printf("RFProcessManager: Failed to read NSM context\n");
192 else
193 nsmlib_call(nsmc, reinterpret_cast<struct NSMtcphead_struct*>(buf));
194 }
195 if (m_iopipe[0] > 0 &&
196 FD_ISSET(m_iopipe[0], &fdset)) {
197 break;
198 }
199 if (nfd == 0) break; // was a timeout -> return to do other stuff
200 }
201 }
202 // Return nfd
203 // time_t now = time ( NULL );
204 // printf ( "[%s] CheckOutput : nfd = %d\n", ctime(&now), nfd );
205 // fflush ( stdout );
206 return nfd;
207}
208
209int RFProcessManager::GetFd()
210{
211 return m_iopipe[0];
212}
213
214pid_t RFProcessManager::CheckProcess()
215{
216 for (vector<pid_t>::iterator it = m_pidlist.begin(); it != m_pidlist.end(); ++it) {
217 pid_t pid = *it;
218 int status;
219 pid_t outpid = waitpid(pid, &status, WNOHANG);
220 // if (outpid == -1) {
221 if (outpid != 0 || outpid == pid) {
222 m_pidlist.erase(it);
223 return pid;
224 }
225 }
226 return 0;
227}
228
int Execute(char *script, int nargs, char **args)
Abstract base class for different kinds of events.
STL namespace.