Low-Energy Muon (LEM) Experiment  0.5.1
ets_logout.c
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: ets_logout.c
4  Created by: Andreas Suter 2005/04/19
5 
6  Contents: Routine to logout out a specific port of the
7  ets terminal server.
8 
9 \********************************************************************/
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 
15 #include "midas.h"
16 
17 #include "tcpip_rs232.h"
18 #include "ets_logout.h"
19 
20 typedef struct {
21  char host[256];
22  int port;
23  int debug;
25 
26 typedef struct {
28  int fd;
30 
31 extern int tcpip_rs232_open(char *host, int port);
33 extern int tcpip_rs232_puts(TCPIP_RS232_INFO *info, char *str);
34 extern int tcpip_rs232_gets(TCPIP_RS232_INFO *info, char *str, int size, char *pattern, int millisec);
35 
36 //----------------------------------------------------------------------------
62 int ets_logout_successfull(char *str, int size)
63 {
64  int i, j;
65  int success = 1;
66  char buffer[512], *pbuf, subbuf[512];
67 
68  // init buffer
69  memset(buffer, 0, sizeof(buffer));
70  pbuf = &buffer[0];
71 
72  // first remove all '\0' from str
73  j=0;
74  for (i=0; i<size; i++) {
75  if (str[i] != 0) {
76  buffer[j] = str[i];
77  j++;
78  }
79  }
80 
81  // since buffer is now a c-string, str
82  pbuf = strstr(buffer, "Status:");
83  if (pbuf == 0) // couldn't find status, i.e. something is very bad :-(
84  return 0;
85 
86  // isolate sub-string which contains the information
87  memset(subbuf, 0, sizeof(subbuf));
88  strncpy(subbuf, pbuf, 32);
89 
90  // check is port is Idle
91  if (strstr(subbuf, "Idle"))
92  success = 1;
93  else
94  success = 0;
95 
96  return success;
97 }
98 
99 //------------------------------------------------------------------------
113 int ets_logout(void *info, int wait, int detailed_msg)
114 {
115  TCPIP_RS232_INFO *bd_info;
116  int status;
117  char cmd[32], str[1024];
118  int done, count = 0, wait_time = wait;
119  int success = 1;
120 
121  bd_info = (TCPIP_RS232_INFO *)info;
122 
123  do {
124  // port 23 for telnet
125  bd_info->fd = tcpip_rs232_open(bd_info->settings.host, 23);
126  if (bd_info->fd <= 0)
127  return 0;
128 
129  // send logout commands
130  usleep(wait_time);
131  strcpy(cmd, "\r\n");
132  tcpip_rs232_puts(bd_info, cmd);
133  status = tcpip_rs232_gets(bd_info, str, sizeof(str), "Username>", 1000);
134 
135  usleep(wait_time);
136  strcpy(cmd, "s\r\n");
137  tcpip_rs232_puts(bd_info, cmd);
138  status = tcpip_rs232_gets(bd_info, str, sizeof(str), ">", 1000);
139 
140  usleep(wait_time);
141  strcpy(cmd, "su\r\n");
142  tcpip_rs232_puts(bd_info, cmd);
143  status = tcpip_rs232_gets(bd_info, str, sizeof(str), ">", 1000);
144 
145  usleep(wait_time);
146  strcpy(cmd, "system\r\n");
147  tcpip_rs232_puts(bd_info, cmd);
148  status = tcpip_rs232_gets(bd_info, str, sizeof(str), ">>", 1000);
149 
150  usleep(wait_time);
151  sprintf(cmd, "logout port %d\r\n", bd_info->settings.port-4000);
152  tcpip_rs232_puts(bd_info, cmd);
153  status = tcpip_rs232_gets(bd_info, str, sizeof(str), ">>", 1000);
154 
155  usleep(wait_time);
156  sprintf(cmd, "show ports %d status\r\n", bd_info->settings.port-4000);
157  tcpip_rs232_puts(bd_info, cmd);
158  status = tcpip_rs232_gets(bd_info, str, sizeof(str), ">>", 1000);
159 
160  if (ets_logout_successfull(str, status)) {
161  done = 1;
162  } else {
163  done = 0;
164  count++;
165  wait_time += 1e4; // increase wait time inbetween sending commands by 10(ms)
166  }
167 
168  usleep(wait_time);
169  sprintf(cmd, "logout \r\n");
170  tcpip_rs232_puts(bd_info, cmd);
171 
172  tcpip_rs232_exit(bd_info);
173 
174  } while (!done && (count < 5)); // try maximal 5 times
175 
176  if (detailed_msg) {
177  if (count < 5) {
178  success = 1;
179  cm_msg(MINFO, "ets_logout", "ets_logout: logged out port %d of ets %s",
180  bd_info->settings.port, bd_info->settings.host);
181  } else {
182  success = 0;
183  cm_msg(MERROR, "ets_logout", "ets_logout: couldn't logout port %d of ets %s",
184  bd_info->settings.port, bd_info->settings.host);
185  }
186  }
187 
188  return success;
189 }
INFO info
Definition: vme_fe.c:206
TCPIP_RS232_SETTINGS settings
Definition: ets_logout.c:27
int tcpip_rs232_gets(TCPIP_RS232_INFO *info, char *str, int size, char *pattern, int millisec)
int tcpip_rs232_open(char *host, int port)
int ets_logout(void *info, int wait, int detailed_msg)
Definition: ets_logout.c:113
int tcpip_rs232_exit(TCPIP_RS232_INFO *info)
int tcpip_rs232_puts(TCPIP_RS232_INFO *info, char *str)
int fd
device handle for socket device
Definition: ets_logout.c:28
int ets_logout_successfull(char *str, int size)
Definition: ets_logout.c:62