summaryrefslogtreecommitdiffstats
path: root/common/recipes-core/consoled/files/consoled.c
blob: 9f17e3168b9869ecff03df23e77c8f3dd5327f44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * consoled
 *
 * Copyright 2015-present Facebook. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <termios.h>
#include <signal.h>
#include <openbmc/pal.h>

#define BAUDRATE      B57600
#define CTRL_X        0x18
#define ASCII_ENTER   0x0D

static sig_atomic_t sigexit = 0;

static void
write_data(int file, char *buf, int len, char *fname) {
  int wlen = 0;
  char *tbuf = buf;
  while(len > 0) {
    errno = 0;
    wlen = write(file, tbuf, len);
    // In case wlen < 0, retry write()
    if (wlen >= 0) {
      len -= wlen;
      tbuf = tbuf + wlen;
    } else {
      syslog(LOG_ALERT, "write_data: write() failed to file %s | errno: %d",
          fname, errno);
      return;
    }
  }
}

static void
exit_session(int sig)
{
  sigexit = sig;
}

static void
print_usage() {
  printf("Usage: consoled [ %s ]\n", pal_server_list);
}

static void
run_console(char* fru_name, int term) {

  int i;
  int tty;    // serial port
  int buf_fd;  // Buffer File
  int blen;   // len for
  int nfd = 0;      // For number of fd
  int nevents;      // For number of events in fd
  int nline = 0;
  //int pid_fd;
  int flags;
  pid_t pid;        // For pid of the daemon
  uint8_t fru;
  char in;          // For stdin character
  char data[32];
  char pid_file[64];
  char devtty[32];  // For tty dev path
  char bfname[32];  // For buffer file path
  char old_bfname[32];  // For old buffer file path
  char buf[256];    // For buffer data
  struct termios ottytio, nttytio;  // For the tty dev

  int stdi;    // STDIN_FILENO
  int stdo; // STDOUT_FILENO
  struct termios ostditio, nstditio;  // For STDIN_FILENO
  struct termios ostdotio, nstdotio;  // For STDOUT_FILENO

  struct pollfd pfd[2];

  /* Start Daemon for the console buffering */
  if (!term) {
    daemon(0,1);
    openlog("consoled", LOG_CONS, LOG_DAEMON);
    syslog(LOG_INFO, "consoled: daemon started");
  }

  if (pal_get_fru_id(fru_name, &fru)) {
    exit(-1);
  }

  if (pal_get_fru_devtty(fru, devtty)) {
    exit(-1);
  }

  /* Handling the few Signals differently */
  signal(SIGHUP, exit_session);
  signal(SIGINT, exit_session);
  signal(SIGTERM, exit_session);
  signal(SIGPIPE, exit_session);
  signal(SIGQUIT, exit_session);

  /* Different flag value for tty dev */
  flags = term == 1 ? (O_RDWR | O_NOCTTY | O_NONBLOCK) :
    (O_RDONLY | O_NOCTTY | O_NONBLOCK);

  if ((tty = open(devtty, flags)) < 0) {
    syslog(LOG_ALERT, "Cannot open the file %s", devtty);
    exit(-1);
  }
  fcntl(tty, F_SETFL, O_RDWR);

  /* Changing the attributes of the tty dev */
  tcgetattr(tty, &ottytio);
  memcpy(&nttytio, &ottytio, sizeof(struct termios));
  cfmakeraw(&nttytio);
  cfsetspeed(&nttytio, BAUDRATE);
  tcflush(tty, TCIFLUSH);
  tcsetattr(tty, TCSANOW, &nttytio);
  pfd[0].fd = tty;
  pfd[0].events = POLLIN;
  nfd++;

  /* Buffering the console data into a file */
  sprintf(old_bfname, "/tmp/consoled_%s_log-old", fru_name);
  sprintf(bfname, "/tmp/consoled_%s_log", fru_name);
  if ((buf_fd = open(bfname, O_RDWR | O_APPEND | O_CREAT, 0666)) < 0) {
    syslog(LOG_ALERT, "Cannot open the file %s", bfname);
    exit(-1);
  }

  if (term) {
    /* Changing the attributes of STDIN_FILENO */
    stdi = STDIN_FILENO;
    tcgetattr(stdi, &ostditio);
    memcpy(&nstditio, &ostditio, sizeof(struct termios));
    cfmakeraw(&nstditio);
    tcflush(stdi, TCIFLUSH);
    tcsetattr(stdi, TCSANOW, &nstditio);

    /* Changing the attributes of STDOUT_FILENO */
    stdo = STDOUT_FILENO;
    tcgetattr(stdo, &ostdotio);
    memcpy(&nstdotio, &ostdotio, sizeof(struct termios));
    cfmakeraw(&nstdotio);
    tcflush(stdo, TCIFLUSH);
    tcsetattr(stdo, TCSANOW, &nstdotio);

    /* Adding STDIN_FILENO to the poll fd set */
    pfd[1].fd = stdi;
    pfd[1].events = POLLIN;
    nfd++;
  }

  /* Handling the input event from the  terminal and tty dev */
  while (!sigexit && (nevents = poll(pfd, nfd, -1 /* Timeout */))) {

    /* Input to the terminal from the user */
    if (term && nevents && nfd > 1 && pfd[1].revents > 0) {
      blen = read(stdi, &in, sizeof(in));
      if (blen < 1) {
        nfd--;
      }

      if (in == CTRL_X) {
        break;
      }

      write_data(tty, &in, sizeof(in), "tty");

      nevents--;
    }

    /* Input from the tty dev */
    if (nevents && pfd[0].revents > 0) {
      blen = read(tty, buf, sizeof(buf));
      if (blen > 0) {
        for (i = 0; i < blen; i++) {
          if (buf[i] == 0xD)
            nline++;
        }
        write_data(buf_fd, buf, blen, bfname);
        fsync(buf_fd);
        if (term) {
          write_data(stdo, buf, blen, "STDOUT_FILENO");
        }
      } else if (blen < 0) {
        raise(SIGHUP);
      }

      /* Log Rotation */
      if (nline >= 300) {
        close(buf_fd);
        remove(old_bfname);
        rename(bfname, old_bfname);
        if ((buf_fd = open(bfname, O_RDWR | O_APPEND | O_CREAT, 0666)) < 0) {
          syslog(LOG_ALERT, "Cannot open the file %s", bfname);
          exit(-1);
        }
        nline = 0;
      }
      nevents--;
    }
  }

  /* Close the console buffer file */
  close(buf_fd);

  /* Revert the tty dev to old attributes */
  tcflush(tty, TCIFLUSH);
  tcsetattr(tty, TCSANOW, &ottytio);

  /* Revert STDIN to old attributes */
  if (term) {
    tcflush(stdo, TCIFLUSH);
    tcsetattr(stdo, TCSANOW, &ostdotio);
    tcflush(stdi, TCIFLUSH);
    tcsetattr(stdi, TCSANOW, &ostditio);
  }

  /* Delete the pid file */
  if (!term)
    remove(pid_file);
}

int
main(int argc, void **argv) {
  int dev, rc, lock_file;
  char file[64];
  int term;
  char *fru_name;

  if (argc != 3) {
    print_usage();
    exit(1);
  }

  // A lock file for one instance of consoled for each fru
  sprintf(file, "/var/lock/consoled_%s", argv[1]);
  lock_file = open(file, O_CREAT | O_RDWR, 0666);
  rc = flock(lock_file, LOCK_EX | LOCK_NB);
  if(rc) {
    if(errno == EWOULDBLOCK) {
      printf("Another consoled %s instance is running...\n", argv[1]);
      exit(-1);
    }
  } else {

    fru_name = argv[1];

    if (!strcmp(argv[2], "--buffer")) {
        term = 0;
    } else if (!strcmp(argv[2], "--term")) {
        term = 1;
    } else {
      print_usage();
      exit(-1);
    }

    run_console(fru_name, term);
  }

  return sigexit;
}

OpenPOWER on IntegriCloud