summaryrefslogtreecommitdiffstats
path: root/src/libply/ply-terminal-session.c
blob: 8be1df3f84fa2995946325f040112d855f2201e4 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* ply-terminal-session.c - api for spawning a program in pseudo-terminal
 *
 * Copyright (C) 2007 Red Hat, Inc.
 *
 * 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, 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., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Written by: Ray Strode <rstrode@redhat.com>
 */
#include "config.h"
#include "ply-terminal-session.h"

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>

#include "ply-event-loop.h"
#include "ply-logger.h"
#include "ply-terminal.h"
#include "ply-utils.h"

struct _ply_terminal_session
{
  ply_terminal_t *terminal;
  ply_logger_t *logger;
  ply_event_loop_t *loop;
  char **argv;

  ply_terminal_session_output_handler_t output_handler;
  ply_terminal_session_done_handler_t   done_handler;
  void                                 *user_data;

  uint32_t is_running : 1;
  uint32_t console_is_redirected : 1;
};

static bool ply_terminal_session_open_console (ply_terminal_session_t *session);
static bool ply_terminal_session_execute (ply_terminal_session_t *session,
                                          bool                    look_in_path);
static void ply_terminal_session_start_logging (ply_terminal_session_t *session);
static void ply_terminal_session_stop_logging (ply_terminal_session_t *session);

static bool
ply_terminal_session_open_console (ply_terminal_session_t *session)
{
  int fd;
  const char *terminal_name;

  terminal_name = ply_terminal_get_device_name (session->terminal);

  fd = open (terminal_name, O_RDONLY); 

  if (fd < 0)
    return false;

  assert (fd == STDIN_FILENO);
  assert (ttyname (fd) != NULL);
  assert (strcmp (ttyname (fd), terminal_name) == 0);

  fd = open (terminal_name, O_WRONLY); 

  if (fd < 0)
    return false;

  assert (fd == STDOUT_FILENO);
  assert (ttyname (fd) != NULL);
  assert (strcmp (ttyname (fd), terminal_name) == 0);

  fd = open (terminal_name, O_WRONLY); 

  if (fd < 0)
    return false;

  assert (fd == STDERR_FILENO);
  assert (ttyname (fd) != NULL);
  assert (strcmp (ttyname (fd), terminal_name) == 0);

  return true;
}

static bool
ply_terminal_session_execute (ply_terminal_session_t *session,
                              bool                    look_in_path)
{
  ply_close_all_fds ();

  if (!ply_terminal_session_open_console (session))
    return false;

  if (look_in_path)
    execvp (session->argv[0], session->argv);
  else
    execv (session->argv[0], session->argv);

  return false;
}

ply_terminal_session_t *
ply_terminal_session_new (const char * const *argv) 
{
  ply_terminal_session_t *session;

  assert (argv == NULL || argv[0] != NULL);

  session = calloc (1, sizeof (ply_terminal_session_t));
  session->argv = argv == NULL ? NULL : ply_copy_string_array (argv);
  session->terminal = ply_terminal_new ();
  session->logger = ply_logger_new ();
  session->is_running = false;
  session->console_is_redirected = false;

  return session;
}

void
ply_terminal_session_free (ply_terminal_session_t *session)
{
  if (session == NULL)
    return;

  ply_terminal_session_stop_logging (session);
  ply_logger_free (session->logger);

  ply_free_string_array (session->argv);
  ply_terminal_free (session->terminal);
  free (session);
}

static void
ply_terminal_session_detach_from_event_loop (ply_terminal_session_t *session)
{
  assert (session != NULL);
  session->loop = NULL;
}

void 
ply_terminal_session_attach_to_event_loop (ply_terminal_session_t *session,
                                           ply_event_loop_t       *loop)
{
  assert (session != NULL);
  assert (loop != NULL);
  assert (session->loop == NULL);

  session->loop = loop;

  ply_event_loop_watch_for_exit (loop, (ply_event_loop_exit_handler_t) 
                                 ply_terminal_session_detach_from_event_loop,
                                 session); 
}

static bool
ply_terminal_session_redirect_console (ply_terminal_session_t *session)
{
  const char *terminal_name;
  int fd;

  assert (session != NULL);

  terminal_name = ply_terminal_get_device_name (session->terminal);

  assert (terminal_name != NULL);

  fd = open (terminal_name, O_RDWR | O_NOCTTY); 

  if (fd < 0)
    return false;

  if (ioctl (fd, TIOCCONS) < 0)
    {
      ply_save_errno ();
      close (fd);
      ply_restore_errno ();
      return false;
    }

  close (fd);
  session->console_is_redirected = true;
  return true;
}

static void
ply_terminal_session_unredirect_console (ply_terminal_session_t *session)
{
  int fd;

  assert (session != NULL);
  assert (session->console_is_redirected);

  fd = open ("/dev/console", O_RDWR | O_NOCTTY);
  if (fd >= 0)
    ioctl (fd, TIOCCONS);

  session->console_is_redirected = false;
}

bool 
ply_terminal_session_run (ply_terminal_session_t              *session,
                          ply_terminal_session_flags_t         flags,
                          ply_terminal_session_begin_handler_t begin_handler,
                          ply_terminal_session_output_handler_t output_handler,
                          ply_terminal_session_done_handler_t  done_handler,
                          void                                *user_data)
{
  pid_t pid;
  bool run_in_parent, look_in_path, should_redirect_console;

  assert (session != NULL);
  assert (session->loop != NULL);
  assert (!session->is_running);
  assert (session->done_handler == NULL);

  run_in_parent = (flags & PLY_TERMINAL_SESSION_FLAGS_RUN_IN_PARENT) != 0;
  look_in_path = (flags & PLY_TERMINAL_SESSION_FLAGS_LOOK_IN_PATH) != 0;
  should_redirect_console = 
    (flags & PLY_TERMINAL_SESSION_FLAGS_REDIRECT_CONSOLE) != 0;

  ply_trace ("creating terminal device");
  if (!ply_terminal_create_device (session->terminal))
    return false;
  ply_trace ("done creating terminal device");

  if (should_redirect_console)
    ply_trace ("redirecting system console to terminal device");
  if (should_redirect_console && 
      !ply_terminal_session_redirect_console (session))
    {
      ply_save_errno ();
      ply_terminal_destroy_device (session->terminal);
      ply_restore_errno ();
      return false;
    }
  if (should_redirect_console)
    ply_trace ("done redirecting system console to terminal device");

  ply_trace ("creating subprocess");
  pid = fork ();

  if (pid < 0)
    {
      ply_save_errno ();
      ply_terminal_session_unredirect_console (session);
      ply_terminal_destroy_device (session->terminal);
      ply_restore_errno ();
      return false;
    }

  if (((pid == 0) && run_in_parent) ||
      ((pid != 0) && !run_in_parent))
    {
      session->is_running = true;
      session->output_handler = output_handler;
      session->done_handler = done_handler;
      session->user_data = user_data;
      ply_terminal_session_start_logging (session);

      return true;
    }

  if (begin_handler != NULL)
    {
      ply_trace ("running 'begin handler'");
      begin_handler (user_data, session);
      ply_trace ("ran 'begin handler'");
    }

  ply_trace ("beginning session");
  ply_terminal_session_execute (session, look_in_path);

  _exit (errno);
  return false;
}

bool
ply_terminal_session_attach (ply_terminal_session_t               *session,
                             ply_terminal_session_flags_t          flags,
                             ply_terminal_session_output_handler_t output_handler,
                             ply_terminal_session_done_handler_t   done_handler,
                             int                                   ptmx,
                             void                                 *user_data)
{
  bool should_redirect_console;

  assert (session != NULL);
  assert (session->loop != NULL);
  assert (!session->is_running);
  assert (session->done_handler == NULL);
  assert (ptmx >= 0);

  should_redirect_console = 
    (flags & PLY_TERMINAL_SESSION_FLAGS_REDIRECT_CONSOLE) != 0;

  ply_terminal_set_fd(session->terminal, ptmx);

  if (should_redirect_console)
    ply_trace ("redirecting system console to terminal device");
  if (should_redirect_console && 
      !ply_terminal_session_redirect_console (session))
    {
      ply_save_errno ();
      ply_terminal_destroy_device (session->terminal);
      ply_restore_errno ();
      return false;
    }
  if (should_redirect_console)
    ply_trace ("done redirecting system console to terminal device");

  session->is_running = true;
  session->output_handler = output_handler;
  session->done_handler = done_handler;
  session->user_data = user_data;
  ply_terminal_session_start_logging (session);

  return true;
}

int
ply_terminal_session_get_fd (ply_terminal_session_t *session)
{
  assert (session != NULL);

  return ply_terminal_get_fd (session->terminal);
}

static void
ply_terminal_session_log_bytes (ply_terminal_session_t *session,
                                const uint8_t          *bytes,
                                size_t                  number_of_bytes)
{
  assert (session != NULL);
  assert (session->logger != NULL);
  assert (bytes != NULL);
  assert (number_of_bytes != 0);

  ply_logger_inject_bytes (session->logger, bytes, number_of_bytes);

  if (session->output_handler != NULL)
    session->output_handler (session->user_data,
                             bytes, number_of_bytes, session);
}

static void
ply_terminal_session_on_new_data (ply_terminal_session_t *session,
                                  int                     session_fd)
{
  uint8_t buffer[4096];
  ssize_t bytes_read;

  assert (session != NULL);
  assert (session_fd >= 0);

  bytes_read = read (session_fd, buffer, sizeof (buffer));

  if (bytes_read > 0)
    ply_terminal_session_log_bytes (session, buffer, bytes_read);

  ply_logger_flush (session->logger);
}

static void
ply_terminal_session_on_hangup (ply_terminal_session_t *session)
{
  assert (session != NULL);

  ply_logger_flush (session->logger);

  session->is_running = false;

  if (session->done_handler != NULL)
    session->done_handler (session->user_data, session);

  ply_terminal_session_stop_logging (session);
}

static void 
ply_terminal_session_start_logging (ply_terminal_session_t *session)
{
  int session_fd;

  assert (session != NULL);
  assert (session->logger != NULL);

  if (!ply_logger_is_logging (session->logger))
    ply_logger_toggle_logging (session->logger);

  session_fd = ply_terminal_session_get_fd (session);

  assert (session_fd >= 0);

  ply_event_loop_watch_fd (session->loop, session_fd,
                           PLY_EVENT_LOOP_FD_STATUS_HAS_DATA,
                           (ply_event_handler_t)
                           ply_terminal_session_on_new_data, 
                           (ply_event_handler_t)
                           ply_terminal_session_on_hangup, session);
}

static void
ply_terminal_session_stop_logging (ply_terminal_session_t *session)
{
  assert (session != NULL);
  assert (session->logger != NULL);

  if (ply_logger_is_logging (session->logger))
    ply_logger_toggle_logging (session->logger);
}

bool 
ply_terminal_session_open_log (ply_terminal_session_t *session,
                               const char             *filename)
{
  bool log_is_opened;

  assert (session != NULL);
  assert (filename != NULL);
  assert (session->logger != NULL);

  ply_save_errno ();
  log_is_opened = ply_logger_open_file (session->logger, filename);
  if (log_is_opened)
    ply_logger_flush (session->logger);
  ply_restore_errno ();

  return log_is_opened;
}

void 
ply_terminal_session_close_log (ply_terminal_session_t *session)
{
  assert (session != NULL);
  assert (session->logger != NULL);

  return ply_logger_close_file (session->logger);
}

#ifdef PLY_TERMINAL_SESSION_ENABLE_TEST

#include <stdio.h>

#include "ply-event-loop.h"
#include "ply-terminal-session.h"

static void
on_finished (ply_event_loop_t *loop)
{
  ply_event_loop_exit (loop, 0);
}

int
main (int    argc,
      char **argv)
{
  ply_event_loop_t *loop;
  ply_terminal_session_t *session;
  int exit_code;
  ply_terminal_session_flags_t flags;

  exit_code = 0;

  loop = ply_event_loop_new ();

  session = ply_terminal_session_new ((const char * const *) (argv + 1));

  flags = PLY_TERMINAL_SESSION_FLAGS_RUN_IN_PARENT;
  flags |= PLY_TERMINAL_SESSION_FLAGS_LOOK_IN_PATH;

  ply_terminal_session_attach_to_event_loop (session, loop);

  if (!ply_terminal_session_run (session, flags, 
                                 (ply_terminal_session_begin_handler_t) NULL,
                                 (ply_terminal_session_output_handler_t) NULL,
                                 (ply_terminal_session_done_handler_t) 
                                 on_finished, loop))
    {
      perror ("could not start terminal session");
      return errno;
    }

  ply_terminal_session_open_log (session, "foo.log");

  exit_code = ply_event_loop_run (loop);

  ply_terminal_session_free (session);

  return exit_code;
}

#endif /* PLY_TERMINAL_SESSION_ENABLE_TEST */
/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */