File: pty.c

package info (click to toggle)
queue 1.30.1-4woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,388 kB
  • ctags: 1,630
  • sloc: ansic: 10,499; cpp: 2,771; sh: 2,640; lex: 104; makefile: 87
file content (230 lines) | stat: -rw-r--r-- 4,724 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
/*
 *  Queue load balancing system
 *  $Revision: 1.3 $
 *  Copyright (C) 1998-2000 W. G. Krebs <wkrebs@gnu.org>
 *
 *  wkrebs@gnu.org
 *
 *
 *  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 1, 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.
 *
 *
 *  If you make modifications to the source, I would be happy to have
 *  them to include in future releases.  Feel free to send them to:
 *      W. G. Krebs
 *	wkrebs@gnu.org
 *
 **************************************************************************/

#include "queue.h"

char *mtos(void);
char buf2[1000];

char *line = NULL;

struct utmp myu;

extern int compar(const void *one, const void *two);



int 
allocpty(void)
{
  static int num = 0;
  static char *dirstack = 0;
  static int dirsize = 0;
  static int dirnum = 0;
  struct stat buf;
  int i, jobs;
  char *dirname;
  int uid, gid,fd;
  DIR *dir;
  FILE *queue;
  struct dirent *temp;
  char *file, c;

  fd = getpt();

  if (fd >= 0 && grantpt (fd) >= 0 && unlockpt (fd) >= 0) {
     line = ptsname (fd);
     return fd;
  }

  if (fd >= 0)
     close (fd);

#if 0
#ifdef HAVE__GETPTY

  /*IRIX*/

  line = _getpty(&fd, O_RDWR|O_NDELAY, 0600, 0);

  if(line) return(fd);

#else

  dirnum = 0;
#ifdef __hpux
  dir = opendir("/dev/ptym");
#else
  dir = opendir("/dev");
#endif
  while (1) {
    if(dirnum == dirsize) {
      dirsize+=100;
      dirstack = realloc(dirstack, dirsize*(_MAXNAMLEN + 1));
    }
    temp = readdir(dir);
    if (!temp) break;
    /*#ifdef linux*/
    if (!(temp->d_name[0] == 'p' && temp->d_name[1] == 't' 
	  && temp->d_name[2] == 'y')) 
      continue;
    /*#endif*/
    strcpy(dirstack + (_MAXNAMLEN + 1) * dirnum++, temp->d_name);
  }
  closedir(dir);

  qsort(dirstack, dirnum-1, _MAXNAMLEN + 1, compar);
  jobs = 0;
  for(i=0; i<dirnum-1; ++i) {
    file = dirstack + i*(_MAXNAMLEN + 1);

#ifndef linux
    /*Next line really only needed for hpux version.*/
    if((!strcmp(file, "."))||(!strcmp(file, "..")))  continue;
#endif

#ifdef __hpux
    sprintf(buf2, "/dev/ptym/%s", file);
#else
    /*GNU/linux*/
    sprintf(buf2, "/dev/%s", file);
#endif


    if((fd = open(buf2, O_RDWR))<0) {
      if(errno==EBUSY) continue;
      if(errno==ENODEV) continue;
      if(errno==EIO) continue;
      syslog(LOG_ERR, "ptyalloc open:%s:%m", buf2);
      return(-1);
    }

    return(fd);
  }
#endif /*HAVE__GETPTY*/
#endif
  syslog(LOG_ERR, "ptyalloc: no more ptys");
  return (-1);
}

void 
deallocpty(void) 
{
#ifndef NO_ROOT
  setutent();
  myu.ut_type = DEAD_PROCESS;
  getutid(&myu);
  pututline(&myu);
  endutent();
#endif /*NO_ROOT*/
}


void 
mkutmp(char *name, int pty2, int uid, int gid)
{
  char *line;
  char * shortline;

  memset (&myu, 0, sizeof (struct utmp));

  line = mtos();

  if (strncmp (line, "/dev/", 5) == 0)
     line += 5;

  shortline = line;
  if (strncmp (shortline, "pts", 3) == 0)
     shortline += 3;

  if (strlen (shortline) > sizeof (myu.ut_id))
     shortline += strlen (shortline) - sizeof (myu.ut_id);

  strncpy (myu.ut_user, name, sizeof (myu.ut_user));
  strncpy (myu.ut_id, shortline, sizeof (myu.ut_id));
  strncpy (myu.ut_line, line, sizeof (myu.ut_line));

#if 0
  strcpy(buf, "/dev/tty");
  strcat(buf, myu.ut_id);
#ifdef linux
  strcpy(myu.ut_line, line+5);
#else
  strcpy(myu.ut_line, line+9);
#endif
#endif

  myu.ut_pid = getpid();
  myu.ut_type = USER_PROCESS;
  myu.ut_time = time(NULL);
#ifdef HAVE_UT_ADDR
  strncpy(myu.ut_host, "Queue process", sizeof (myu.ut_host));
  myu.ut_addr = 0L;
#endif

  setutent();
  getutid(&myu);
  pututline(&myu);
  endutent();
}

/*HP ptsname is buggy! So we write our own!*/
char 
*mtos(void)
{
  char *ptym;
  static char buf[16];
  register int i;

//#ifdef HAVE__GETPTY
  if(line) return(line);

  syslog(LOG_ERR, "mtos: call allocpty first.");
  return(NULL);

#if 0
  ptym = buf2;
  for(i=0;ptym[i]!=0;++i) if(ptym[i]=='/') {
    ptym = &ptym[i+1];
    i = 0;
  }
  if(ptym[0]!='p') 
    syslog(LOG_ERR, "mtos: bad pty: %s", ptym);

#ifdef __hpux
  sprintf(buf, "/dev/pty/t%s", ptym+1);
#else
  /*gnu/linux*/
  sprintf(buf, "/dev/t%s", ptym+1);
#endif

  return(buf);
#endif /*HAVE__GETPTY*/
}