File: ttyserver.c

package info (click to toggle)
chdrv 1.0.13-0.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,204 kB
  • ctags: 1,069
  • sloc: ansic: 8,972; perl: 412; asm: 234; makefile: 195; sh: 36
file content (722 lines) | stat: -rw-r--r-- 15,631 bytes parent folder | download
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722


/*
   ChangeLog
   Build date 08/25/1993
       09/01/1993
           (1) when we can't get ack in WAIT_TIME seconds,I will send
	       A reset signal to chdam.
       09/03/1993
           (1) integrate chdam & ttyserver to a single program
       09/04/1993
           (1) use pseudo terminal to redirect the terminal
	   
*/


#define CHINESE_KERNEL
#include <sys/types.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/vt.h>
#include <sys/kd.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <termios.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/socket.h>
#include "chmsg.h"
#include "chinese.h"
#include <pwd.h>
char copyright[] = {
"+-----------------------------------------------+\n"
"|               ����ݾ������{��                |\n"
"|              ���{���Ѥ������s�g               |\n"
"|        ��󭫻s�P�ϥΧ��ݲŦXGPL���W�d        |\n"
"|        �p���ϥΤW�����DMANUAL.DOC�Mchdrv.FAQ  |\n"
"|        ���i�H���z������U�C                   |\n" 
"+-----------------------------------------------+\n"};

#ifndef CHBIN
#error "CHBIN not defined"
#endif

static struct {
  int client_read_pipe;
  int client_write_pipe;
  char devicename[10];
  int isused;
  struct vt_mode vtmode;
  int shellpid,clientpid;
} cons[CONS_NUM];
int outfd[2],infd[2];
static int child_fd,parent_fd;
FILE *stddebug;
#define D(p)  p


#define master   (cons[curcons].client_read_pipe)
#define slave  (cons[curcons].client_write_pipe)
#define devicename (cons[curcons].devicename)
#define vmode      (cons[curcons].vtmode)
#define used       (cons[curcons].isused) 
#define scrtext    (cons[curcons].scrtext)
#define scrattr    (cons[curcons].scrattr)
#define shellpid   (cons[curcons].shellpid)
#define clientpid  (cons[curcons].clientpid)
#define WAIT_TIME  120
#if 0
static int semid;
#endif
static char ttys[] = {"/dev/tty0"};
static char cins[] = {"/dev/cin0"};
static char couts[]= {"/dev/cout0"};
static unsigned char buf[528]={255,254};
static int ptypid,driverpid;
static int commandpipe;
static jmp_buf albuf,reset;
static int serverpid;
static int programpid;
void open_file();
static void init_fds();
static daemon_init()
{
  int   fd;
  struct stat buf;
  /* before start,we will check if there is another ttyserver exist */

  fd = stat("/tmp/chdrv.lck",&buf);
  if (fd == 0)
    {
      /* Their is a process that open the /dev/cin for read   */
      /* So we can realize there are another copie(s) active  */
      printf("There is somewaht another copy of process active or\n");
      printf("other process use /dev/cin to operate.The Chinese tty\n");
      printf("Daemon can't start.\n");
      exit(1);
    }
  close(fd);
  fd = creat("/tmp/chdrv.lck",0600);
  if (fd < 0)
    {
      printf("Can't open lock file /tmp/chdrv.lck\n");
      exit(-1);
    }
  write(fd,"LOCK",4);
  close(fd);
  stddebug = fopen("/dev/tty8","w");
  if (stddebug<0)
    {
      SYSERR("Can't open /dev/tty8 for debug output");
      exit(-1);
    }
  if (geteuid() != 0)
    {
      printf("Can't execute in non-root mode\n");
      exit(1);
    }
  chdir("/");
  umask(0);
  if (getppid() == 1)
    return;

  if (fork() != 0)
    exit(0);
  setpgrp();
  if (fork()!=0)
    exit(0);

  printf("\nChinese tty writen by Yu-Ching Wang %s\n",version);
  printf("Use 'kill %d' to kill me\n",getpid());
  printf("All debug message is redirected to console #8\n");
}

static void sig_init(int signo)
{
  kill(serverpid,SIGINT);
  longjmp(reset,1);
}

void sendcommand(int cmd,int tty,char *sbuf,int len)
{
  int rlen;
  buf[0] = 255;buf[1] = 254;
  buf[2] = len;buf[3] = 0;
  buf[4] = tty;
  buf[5] = cmd;

  write(outfd[1],buf,6);
  while (len > 0)
    {
      rlen = (len > 255)? 255 :len;
      write(outfd[1],sbuf,rlen);
      sbuf +=rlen;
      len -= rlen;
    }
}


static void sigquit(int signo)
{
  unlink("/tmp/chdrv.lck");
  SYSERR("UNLINK");
  kill(child_fd,SIGTERM);
  kill(serverpid,SIGTERM);
  exit(0);
}


static char intbuf[4096];
static int bufhead=0,bufend=0;

static void add_write(char *buf,int len)
{
  if ((bufend+len)%4095 > bufhead)
    {
      /* in this case ,we write directly to pipe
	 This maybe block,but....
	 */
      while(bufhead != bufend)
	out_buf();
      while(len)
	len -= write(outfd[1],buf,len);
      return;
    }
  if ((bufend+len) > 4095)
    {
      memcpy(intbuf+bufend,buf,4095-bufend);
      buf += 4095-len;
      len -= 4095-len;
      bufend = 0;
      memcpy(intbuf,buf,len);
      bufend = len;
      return;
    }
  memcpy(intbuf+bufend,buf,len);
  bufend += len;
}
out_buf()
{
  int ptr;
  if (bufend < bufhead)
    bufhead += write(outfd[1],intbuf+bufhead,4096-bufhead);
  else
    bufhead += write(outfd[1],intbuf+bufhead,bufend-bufhead);
}

static void sigquit1(intsigno)
{
  kill(programpid,SIGTERM);
  kill(parent_fd,SIGTERM);
  SYSERR("Child killed");
  exit(0);
}

/* 
   Don't confuse with the name "waitinloop" ,this procedure is used to
   'READ' from the real program and 'WRITE' it to the chinese daemon.
*/

static fd_set readset,writeset;
static int toclient;
static maxfd;
inline int READ_SELECT(struct timeval *tptr)
{
  int curcons,max,len,ret;
  fd_set rset,wset;
  struct timeval timep;
  static whofirst = 0;

  while(1)
    {
      rset = readset;wset=writeset;timep = *tptr;
      if (bufend != bufhead)
	FD_SET(outfd[1],&wset);
      if (toclient <=0)
	toclient = open("/dev/cin",O_RDONLY);
      if (toclient > 0)
	FD_SET(toclient,&rset);

      if ((len = select(maxfd+1,&rset,&wset,NULL,&timep)) < 0)
	{
	  close(toclient);
	  toclient = 0;
	  init_fds();
	  continue;
	}

      /* if a request to write to screen */
      if (FD_ISSET(outfd[1],&wset))
	  out_buf();

      /* if a request to build a session */
      if (FD_ISSET(toclient,&rset))
	{
	  open_file();
	  init_fds();
	}
      /* if a input from chinese daemon */
      for(curcons=0;curcons<CONS_NUM;curcons++)
	{
	  if (used)
	    {
	      if (FD_ISSET(master,&rset))
		{
		  buf[0] = 255;buf[1] = 254;
		  buf[5] = CCMD_PRINT;
		  buf[4] = curcons;
		  len = read(master,buf+6,255);
		  *((short int *)(buf + 2)) = len;

		  add_write(buf,len+6);

		}
	      /* check if there is a shell has die */
	      if (shellpid != 0)
		if ((ret = waitpid(shellpid,NULL,WNOHANG)) != 0)
		  {
		    shellpid = 0;
		    if (ret == -1)
		      {
			fprintf(stddebug,"SHELL DIE ANYWAY\n");
			continue;
		      }
		    sendcommand(CCMD_TEXT,curcons,NULL,0);
		    sendcommand(CCMD_END,curcons,NULL,0);
		    /* Wake up the ccc driver                   */
		    /* the ccc driver is in pause() call        */
		    kill(clientpid,SIGALRM);
		    used = 0;
		    shellpid = clientpid = 0;
		    /* clear up all garbage in psuedo terminal */
		    ioctl(master,TCFLSH,2);
		    init_fds();
		  }
	    }
	}
    }
}

/*
   The pipe in there have three kinds
    (1) pipe link from client program
    (2) pty write from user program shell
    (3) pty read from user program shell
*/


static void init_fds()
{
  int curcons;
  if (toclient)
    close(toclient);
  toclient = open("/dev/cin",O_RDONLY|O_NDELAY);
  FD_ZERO(&readset);FD_ZERO(&writeset);
  
  maxfd = toclient;
  for(curcons=0;curcons<CONS_NUM;curcons++)
    {
      if (used)
	{
	  FD_SET(master,&readset);
	  if (master > maxfd)
	    maxfd = master;
	}
    }
}
int READ_WAIT(struct timeval *timeout)
{
  fd_set readfd;

  FD_ZERO(&readfd);
  FD_SET(infd[0],&readfd);
  FD_SET(commandpipe,&readfd);
  select(commandpipe>infd[0]? commandpipe+1:infd[0]+1,&readfd,NULL,NULL,NULL);
  if (FD_ISSET(commandpipe,&readfd))
    {
      char cc;
      int curcons;
    }
  if (FD_ISSET(infd[0],&readfd))
    return 1;
  return -1;
}
  
void waitinloop()
{
  int fds,len,rlen;
  static int curcons = -1;
  static unsigned char ibuf[5];
  static int ptr=0;
  fd_set readfds,writefds;
  struct timeval timeout;

  /* wait for 0.3 seconds */
  timeout.tv_sec = 10;
  timeout.tv_usec = 0;
  
  init_fds();
  while(1)
    {
      while(1)
	{
	  if (READ_WAIT(&timeout) < 0)
	    break;
	  if (read(infd[0],ibuf,1) != 1)
	    continue;
	  ptr++;	      
	  if (ibuf[0] != 255)
	    break;
	  if (READ_WAIT(&timeout) < 0)
	    break;
	  
	  if (read(infd[0],ibuf+1,1) != 1)
	    break;
	  ptr++;
	  if (ibuf[1] != 254)
	    break;

	  if (READ_WAIT(&timeout) < 0)
	    break;
	  
	  if (read(infd[0],ibuf+2,2) != 2)
	    break;
	  len = *(short int *) (ibuf+2);
	  ptr += 2;
	  if (len > 8192)
	    break;
	  if (read(infd[0],buf,1) != 1)
	    continue;
	  curcons = *buf;
	  while(len > 0)
	    {
	      rlen = (len > 10)? 10 : len;
	      len = read(infd[0],buf,rlen);
	      rlen = write(master,buf,rlen);
	      len -= rlen;
	    }
	  ptr = 0;
	}
      /* If ibuf is not empty,it stands for a incomplete head.We 
	 send it as data */
      if (ptr != 0)
	{
	  if (curcons == -1)
	    SYSERR("No tty set head first");
	  len = write(master,ibuf,ptr);

	  ptr = 0;
	}
    }
}

void waitoutloop()
{
  int fds,len,rlen;
  static int curcons = -1;
  static unsigned char ibuf[5];
  static int ptr=0;
  fd_set readfds,writefds;
  struct timeval timeout;

  /* wait for 0.3 seconds */
  timeout.tv_sec = 5;
  timeout.tv_usec = 0;
  
  init_fds();
  READ_SELECT(&timeout);
}

char *set_env_list(int);
void open_file()
{
  int i,curcons,len,fd;
  struct winsize size;
  char *tmpname,*shell;
  char name[25];
  CMSG msg;
  char path[256];
  CHAR ww[2];
  char cc;


  if (read(toclient,(char *)&msg,sizeof(CMSG)) != sizeof(CMSG))
    {
      close(toclient);
      toclient = -1;
      return;
    }
  if (msg.type == CHTTY_EXIST)
    {
      close(toclient);
      toclient = -1;
      sendcommand(CCMD_CHTTY,msg.tty,NULL,0);
      read(infd[0],buf,1);
      fd = open("/dev/cout",O_WRONLY);
      write(fd,buf,1);
      close(fd);
      return ;
    }
  else if (msg.type == CHTTY_KILL)
    {
      sigquit1(SIGKILL);
    }

  buf[msg.length_of_name] = 0;
  fcntl(toclient,F_SETFL,0);
  if ((len = read(toclient,path,msg.length_of_home)) != msg.length_of_home)
    {
      fprintf(stddebug,"only %d bytes of home path read,should be %d\n(%s)",
	                                   len,msg.length_of_home,
	                                   _sys_errlist[_sys_nerr]);
      close(toclient);
      toclient = -1;
      return;
    }

  close(toclient);
  toclient = -1;
  path[msg.length_of_home] = 0;
  if (msg.tty < 0 || msg.tty >= CONS_NUM)
    return;
  
  /* Start to enable a process which IO is control */
  /* by out PTY piplines                           */
  curcons = msg.tty;
  
  sendcommand(CCMD_START,msg.tty,NULL,0);
  
  if (msg.width_of_screen > MAX_DIMX)
    msg.width_of_screen = MAX_DIMX;
  else if (msg.width_of_screen < MIN_DIMX)
    msg.width_of_screen = MIN_DIMX;
  
  if (msg.lines_of_screen > MAX_DIMY)
    msg.lines_of_screen = MAX_DIMY;
  else if (msg.lines_of_screen < MIN_DIMY)
    msg.lines_of_screen = MIN_DIMY;
  
  /* change the window size of chinese driver */
  ww[0] = msg.width_of_screen;
  ww[1] = msg.lines_of_screen;
  sendcommand(CCMD_SWINSZ,msg.tty,ww,2);
  fprintf(stddebug,"WINDOW SIZE %d %d\n",buf[0],buf[1]);
  sendcommand(CCMD_GRAPH,msg.tty,NULL,0);
  
  if ((shellpid =fork())==0)
    {
      int fd;

      if (setsid()<0)
	{
	  SYSERR("Fail to set secttion");
	  exit(-1);
	}
      close(0);
      close(1);
      close(2);
      slave = ptys_open(master,devicename);
      if (slave < 0)
	{
	  SYSERR1("Can't open slave pty:%s\n",strerror(errno));
	  close(master);
	  exit(0);
	}

      if (setuid(msg.uid) < 0)
	{
	  SYSERR1("SETUID FAIL:%s",strerror(errno));
	  exit(0);
	}

      /* close all opened file descriptor */
      /* In fact,the client ccc should send its stdin & stdout */
      /* to us,but I don't know how to transmit it in Linux ,  */
      /* Any one can tell me?                                  */
      /*
       */  
      /* if path is wrong used root default */
      if (chdir(path) < 0)
	chdir("/tmp");
#if 0      
      /* Enter a new secttion */
      if (setpgid(getpid(),msg.pgrp) < 0)
	{
	  /* I don't know whether this case will happen. */
	  /* But consider it for safty.If somethone can  */
	  /* tell me.thank you                           */
	  setpgid(getpid(),getpid());

	}
#endif      
      size.ws_row = msg.lines_of_screen;
      size.ws_col = msg.width_of_screen;
      ioctl(slave,TIOCSWINSZ,(int)&size);
      
      if (dup2(slave,0) != 0)
	  fprintf(stddebug,"DUP2 ERROR\n");
      if (dup2(slave,1) != 1)
	  fprintf(stddebug,"DUP2 ERROR\n");
      if (dup2(slave,2) != 2)
	  fprintf(stddebug,"DUP2 ERROR\n");
      sleep(1);
      write(0,copyright,strlen(copyright));

      /* ch_profile is generated by chconfig to do some internal
	 work. '.chrc' is a user specified script */
      /* 
	 We should get data from passwd file to load a proper shell
	 according user specify but I don't dot now.
	 */

      system(CHSYS"ch_profile");
      if (access("~/.chrc",755)==0)
	system("~/.chrc");
      shell = set_env_list(msg.uid);
      if (execl(shell,shell,NULL)<0)
	{
	  SYSERR("Execute error");
	  exit(0);
	}
    }
  else if (shellpid < 0)
    {
      fprintf(stddebug,"fork error in create session\n");
      close(master);
    }
  used = 1;
  clientpid = msg.pid;

  fprintf(stddebug,"A session build\n");
}
void pipe_broken(int signo)
{
  sigquit(SIGKILL);
}

void clear_all_env(char *[]);

main(int argc,char *argv[],char *env[])
{
  int curcons,len;
  int oldf,i;
  int cpipe[2];

  daemon_init();
  D(fprintf(stddebug,"Chinese System 0.9 By Yu-Chung Wang\n"));

  signal(SIGPIPE,pipe_broken);
  /* open a pipe to link to chinese driver */
  if (pipe(outfd)<0)
    {
      SYSERR("Can't build a link to chinese driver");
      exit(1);
    }

  if (pipe(infd)<0)
    {
      SYSERR("Can't build a link to child ttyserver");
      exit(1);
    }
  setsid();
  /* execute chinese driver */

  if ((driverpid = fork())==0)
    {
      close(outfd[1]);
      close(infd[0]);
      if (dup2(outfd[0],0)!=0)
	{
	  SYSERR("Can't redirect stdin");
	  exit(1);
	}
      if (dup2(infd[1],1) != 1)
	{
	  SYSERR("Can't redirect stdout");
	  exit(1);
	}
      close(outfd[0]);
      close(infd[1]);
      if (execl(CHBIN"chdam","chdam",NULL)<0)
	{
	  SYSERR("Can't exec chinese driver");
	  exit(1);
	}
      SYSERR("Impossible bug");
      exit(-1);
    }
  else
    {
      close(outfd[0]);
      /* wait chdam initial correctly */
    }

  for(curcons=0;curcons<CONS_NUM;curcons++)
    {
      master = ptym_open(devicename);
      used = 0;
    }
  toclient = 0;
  parent_fd = getpid();

  socketpair(AF_UNIX, SOCK_STREAM, 0, cpipe); 
  fcntl(cpipe[0], F_SETFL, O_RDWR );
  fcntl(cpipe[1], F_SETFL, O_RDWR );

  if ((child_fd = fork())==0)
    {
      commandpipe = cpipe[0];
      signal(SIGTERM,sigquit);
      signal(SIGQUIT,sigquit);
      waitinloop();
    }
  else
    {
      commandpipe = cpipe[1];
      signal(SIGTERM,sigquit1);
      signal(SIGQUIT,sigquit1);
      waitoutloop();
    }

  sigquit(0);
}

void clear_all_env(char *env[])
{
  int i;
  char buf[20];

  while(*env)
    {
      i = 0;
      while(env[0][i]!= '=' && env[0][i]) i++;
      strncpy(buf,env[0],i+1);
      buf[i]=0;
      unsetenv(buf);
      fprintf(stddebug,"CLEAR ENV %s\n",buf);
      env++;
    }
}
  
char *set_env_list(int uid)
{
  char buf[100];
  struct passwd *entry = getpwuid(uid);
  setenv("HOME",entry->pw_dir,1);
  setenv("USER",entry->pw_name,1);
  setenv("LOGNAME",entry->pw_name,1);
  setenv("TERM","console",1);
  strcpy(buf,"/var/spool/mail/");
  strcat(buf,entry->pw_name);
  setenv("MAIL",buf,1);
  return entry->pw_shell;
}