428
|
1 /* Modified version of unexec for convex machines.
|
|
2 Copyright (C) 1985, 1986, 1988 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 19.31. */
|
|
22
|
|
23
|
|
24 /* modified for C-1 arch by jthomp@convex 871103 */
|
|
25 /* Corrected to support convex SOFF object file formats and thread specific
|
|
26 * regions. streepy@convex 890302
|
|
27 */
|
|
28
|
|
29 /*
|
|
30 * unexec.c - Convert a running program into an a.out file.
|
|
31 *
|
|
32 * Author: Spencer W. Thomas
|
|
33 * Computer Science Dept.
|
|
34 * University of Utah
|
|
35 * Date: Tue Mar 2 1982
|
|
36 * Modified heavily since then.
|
|
37 *
|
|
38 * Synopsis:
|
|
39 * unexec (new_name, a_name, data_start, bss_start, entry_address)
|
|
40 * char *new_name, *a_name;
|
|
41 * unsigned data_start, bss_start, entry_address;
|
|
42 *
|
|
43 * Takes a snapshot of the program and makes an a.out format file in the
|
|
44 * file named by the string argument new_name.
|
|
45 * If a_name is non-NULL, the symbol table will be taken from the given file.
|
|
46 * On some machines, an existing a_name file is required.
|
|
47 *
|
|
48 * The boundaries within the a.out file may be adjusted with the data_start
|
|
49 * and bss_start arguments. Either or both may be given as 0 for defaults.
|
|
50 *
|
|
51 * Data_start gives the boundary between the text segment and the data
|
|
52 * segment of the program. The text segment can contain shared, read-only
|
|
53 * program code and literal data, while the data segment is always unshared
|
|
54 * and unprotected. Data_start gives the lowest unprotected address.
|
|
55 * The value you specify may be rounded down to a suitable boundary
|
|
56 * as required by the machine you are using.
|
|
57 *
|
|
58 * Specifying zero for data_start means the boundary between text and data
|
|
59 * should not be the same as when the program was loaded.
|
|
60 * If NO_REMAP is defined, the argument data_start is ignored and the
|
|
61 * segment boundaries are never changed.
|
|
62 *
|
|
63 * Bss_start indicates how much of the data segment is to be saved in the
|
|
64 * a.out file and restored when the program is executed. It gives the lowest
|
|
65 * unsaved address, and is rounded up to a page boundary. The default when 0
|
|
66 * is given assumes that the entire data segment is to be stored, including
|
|
67 * the previous data and bss as well as any additional storage allocated with
|
|
68 * break (2).
|
|
69 *
|
|
70 * The new file is set up to start at entry_address.
|
|
71 *
|
|
72 * If you make improvements I'd like to get them too.
|
|
73 * harpo!utah-cs!thomas, thomas@Utah-20
|
|
74 *
|
|
75 */
|
|
76
|
|
77 /* There are several compilation parameters affecting unexec:
|
|
78
|
|
79 * COFF
|
|
80
|
|
81 Define this if your system uses COFF for executables.
|
|
82 Otherwise we assume you use Berkeley format.
|
|
83
|
|
84 * NO_REMAP
|
|
85
|
|
86 Define this if you do not want to try to save Emacs's pure data areas
|
|
87 as part of the text segment.
|
|
88
|
|
89 Saving them as text is good because it allows users to share more.
|
|
90
|
|
91 However, on machines that locate the text area far from the data area,
|
|
92 the boundary cannot feasibly be moved. Such machines require
|
|
93 NO_REMAP.
|
|
94
|
|
95 Also, remapping can cause trouble with the built-in startup routine
|
|
96 /lib/crt0.o, which defines `environ' as an initialized variable.
|
|
97 Dumping `environ' as pure does not work! So, to use remapping,
|
|
98 you must write a startup routine for your machine in Emacs's crt0.c.
|
|
99 If NO_REMAP is defined, Emacs uses the system's crt0.o.
|
|
100
|
|
101 * SECTION_ALIGNMENT
|
|
102
|
|
103 Some machines that use COFF executables require that each section
|
|
104 start on a certain boundary *in the COFF file*. Such machines should
|
|
105 define SECTION_ALIGNMENT to a mask of the low-order bits that must be
|
|
106 zero on such a boundary. This mask is used to control padding between
|
|
107 segments in the COFF file.
|
|
108
|
|
109 If SECTION_ALIGNMENT is not defined, the segments are written
|
|
110 consecutively with no attempt at alignment. This is right for
|
|
111 unmodified system V.
|
|
112
|
|
113 * SEGMENT_MASK
|
|
114
|
|
115 Some machines require that the beginnings and ends of segments
|
|
116 *in core* be on certain boundaries. For most machines, a page
|
|
117 boundary is sufficient. That is the default. When a larger
|
|
118 boundary is needed, define SEGMENT_MASK to a mask of
|
|
119 the bits that must be zero on such a boundary.
|
|
120
|
|
121 * A_TEXT_OFFSET(HDR)
|
|
122
|
|
123 Some machines count the a.out header as part of the size of the text
|
|
124 segment (a_text); they may actually load the header into core as the
|
|
125 first data in the text segment. Some have additional padding between
|
|
126 the header and the real text of the program that is counted in a_text.
|
|
127
|
|
128 For these machines, define A_TEXT_OFFSET(HDR) to examine the header
|
|
129 structure HDR and return the number of bytes to add to `a_text'
|
|
130 before writing it (above and beyond the number of bytes of actual
|
|
131 program text). HDR's standard fields are already correct, except that
|
|
132 this adjustment to the `a_text' field has not yet been made;
|
|
133 thus, the amount of offset can depend on the data in the file.
|
|
134
|
|
135 * A_TEXT_SEEK(HDR)
|
|
136
|
|
137 If defined, this macro specifies the number of bytes to seek into the
|
|
138 a.out file before starting to write the text segment.a
|
|
139
|
|
140 * EXEC_MAGIC
|
|
141
|
|
142 For machines using COFF, this macro, if defined, is a value stored
|
|
143 into the magic number field of the output file.
|
|
144
|
|
145 * ADJUST_EXEC_HEADER
|
|
146
|
|
147 This macro can be used to generate statements to adjust or
|
|
148 initialize nonstandard fields in the file header
|
|
149
|
|
150 * ADDR_CORRECT(ADDR)
|
|
151
|
|
152 Macro to correct an int which is the bit pattern of a pointer to a byte
|
|
153 into an int which is the number of a byte.
|
|
154
|
|
155 This macro has a default definition which is usually right.
|
|
156 This default definition is a no-op on most machines (where a
|
|
157 pointer looks like an int) but not on all machines.
|
|
158
|
|
159 */
|
|
160
|
|
161 #include <config.h>
|
|
162 #define PERROR(file) report_error (file, new)
|
|
163
|
|
164 #include <a.out.h>
|
|
165 /* Define getpagesize () if the system does not.
|
|
166 Note that this may depend on symbols defined in a.out.h
|
|
167 */
|
|
168 #include "getpagesize.h"
|
|
169
|
|
170 #include <sys/types.h>
|
|
171 #include <stdio.h>
|
|
172 #include <sys/stat.h>
|
|
173 #include <errno.h>
|
|
174
|
|
175 extern char *start_of_text (); /* Start of text */
|
|
176 extern char *start_of_data (); /* Start of initialized data */
|
|
177
|
|
178 #include <machine/filehdr.h>
|
|
179 #include <machine/opthdr.h>
|
|
180 #include <machine/scnhdr.h>
|
|
181 #include <machine/pte.h>
|
|
182
|
|
183 static long block_copy_start; /* Old executable start point */
|
|
184 static struct filehdr f_hdr; /* File header */
|
|
185 static struct opthdr f_ohdr; /* Optional file header (a.out) */
|
|
186 long bias; /* Bias to add for growth */
|
|
187 #define SYMS_START block_copy_start
|
|
188
|
|
189 static long text_scnptr;
|
|
190 static long data_scnptr;
|
|
191
|
|
192 static int pagemask;
|
|
193 static int pagesz;
|
|
194
|
|
195 static
|
|
196 report_error (file, fd)
|
|
197 char *file;
|
|
198 int fd;
|
|
199 {
|
|
200 if (fd)
|
|
201 close (fd);
|
563
|
202 signal_error (Qio_error, "Failure operating on file",
|
|
203 build_string (file));
|
428
|
204 }
|
|
205
|
|
206 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
|
|
207 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
|
|
208 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
|
|
209
|
|
210 static
|
|
211 report_error_1 (fd, msg, a1, a2)
|
|
212 int fd;
|
|
213 char *msg;
|
|
214 int a1, a2;
|
|
215 {
|
|
216 close (fd);
|
563
|
217 signal_ferror (Qio_error, msg, a1, a2);
|
428
|
218 }
|
|
219
|
|
220 /* ****************************************************************
|
|
221 * unexec
|
|
222 *
|
|
223 * driving logic.
|
|
224 */
|
|
225 unexec (new_name, a_name, data_start, bss_start, entry_address)
|
|
226 char *new_name, *a_name;
|
|
227 unsigned data_start, bss_start, entry_address;
|
|
228 {
|
|
229 int new, a_out = -1;
|
|
230
|
|
231 if (a_name && (a_out = open (a_name, 0)) < 0) {
|
|
232 PERROR (a_name);
|
|
233 }
|
|
234 if ((new = creat (new_name, 0666)) < 0) {
|
|
235 PERROR (new_name);
|
|
236 }
|
|
237
|
|
238 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0
|
|
239 || copy_text_and_data (new) < 0
|
|
240 || copy_sym (new, a_out, a_name, new_name) < 0 ) {
|
|
241 close (new);
|
|
242 return -1;
|
|
243 }
|
|
244
|
|
245 close (new);
|
|
246 if (a_out >= 0)
|
|
247 close (a_out);
|
|
248 mark_x (new_name);
|
|
249 return 0;
|
|
250 }
|
|
251
|
|
252 /* ****************************************************************
|
|
253 * make_hdr
|
|
254 *
|
|
255 * Make the header in the new a.out from the header in core.
|
|
256 * Modify the text and data sizes.
|
|
257 */
|
|
258
|
|
259 struct scnhdr *stbl; /* Table of all scnhdr's */
|
|
260 struct scnhdr *f_thdr; /* Text section header */
|
|
261 struct scnhdr *f_dhdr; /* Data section header */
|
|
262 struct scnhdr *f_tdhdr; /* Thread Data section header */
|
|
263 struct scnhdr *f_bhdr; /* Bss section header */
|
|
264 struct scnhdr *f_tbhdr; /* Thread Bss section header */
|
|
265
|
|
266 static int
|
|
267 make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
|
|
268 int new, a_out;
|
|
269 unsigned data_start, bss_start, entry_address;
|
|
270 char *a_name;
|
|
271 char *new_name;
|
|
272 {
|
|
273 int scns;
|
|
274 unsigned int bss_end;
|
|
275 unsigned int eo_data; /* End of initialized data in new exec file */
|
|
276 int scntype; /* Section type */
|
|
277 int i; /* Var for sorting by vaddr */
|
|
278 struct scnhdr scntemp; /* For swapping entries in sort */
|
|
279 extern char *start_of_data();
|
|
280
|
|
281 pagemask = (pagesz = getpagesize()) - 1;
|
|
282
|
|
283 /* Adjust text/data boundary. */
|
|
284 if (!data_start)
|
|
285 data_start = (unsigned) start_of_data ();
|
|
286
|
|
287 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
|
|
288
|
|
289 bss_end = (sbrk(0) + pagemask) & ~pagemask;
|
|
290
|
|
291 /* Adjust data/bss boundary. */
|
|
292 if (bss_start != 0) {
|
|
293 bss_start = (bss_start + pagemask) & ~pagemask;/* (Up) to page bdry. */
|
|
294 if (bss_start > bss_end) {
|
|
295 ERROR1 ("unexec: Specified bss_start (%x) is past end of program",
|
|
296 bss_start);
|
|
297 }
|
|
298 } else
|
|
299 bss_start = bss_end;
|
|
300
|
|
301 if (data_start > bss_start) { /* Can't have negative data size. */
|
|
302 ERROR2 ("unexec: data_start (%x) can't be greater than bss_start (%x)",
|
|
303 data_start, bss_start);
|
|
304 }
|
|
305
|
|
306 /* Salvage as much info from the existing file as possible */
|
|
307 if (a_out < 0) {
|
|
308 ERROR0 ("can't build a COFF file from scratch yet");
|
|
309 /*NOTREACHED*/
|
|
310 }
|
|
311
|
|
312 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) {
|
|
313 PERROR (a_name);
|
|
314 }
|
|
315 block_copy_start += sizeof (f_hdr);
|
|
316 if (f_hdr.h_opthdr > 0) {
|
|
317 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) {
|
|
318 PERROR (a_name);
|
|
319 }
|
|
320 block_copy_start += sizeof (f_ohdr);
|
|
321 }
|
|
322
|
|
323 /* Allocate room for scn headers */
|
|
324 stbl = (struct scnhdr *)malloc( sizeof(struct scnhdr) * f_hdr.h_nscns );
|
|
325 if( stbl == NULL ) {
|
|
326 ERROR0( "unexec: malloc of stbl failed" );
|
|
327 }
|
|
328
|
|
329 f_tdhdr = f_tbhdr = NULL;
|
|
330
|
|
331 /* Loop through section headers, copying them in */
|
|
332 for (scns = 0; scns < f_hdr.h_nscns; scns++) {
|
|
333
|
|
334 if( read( a_out, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) {
|
|
335 PERROR (a_name);
|
|
336 }
|
|
337
|
|
338 scntype = stbl[scns].s_flags & S_TYPMASK; /* What type of section */
|
|
339
|
|
340 if( stbl[scns].s_scnptr > 0L) {
|
|
341 if( block_copy_start < stbl[scns].s_scnptr + stbl[scns].s_size )
|
|
342 block_copy_start = stbl[scns].s_scnptr + stbl[scns].s_size;
|
|
343 }
|
|
344
|
|
345 if( scntype == S_TEXT) {
|
|
346 f_thdr = &stbl[scns];
|
|
347 } else if( scntype == S_DATA) {
|
|
348 f_dhdr = &stbl[scns];
|
|
349 #ifdef S_TDATA
|
|
350 } else if( scntype == S_TDATA ) {
|
|
351 f_tdhdr = &stbl[scns];
|
|
352 } else if( scntype == S_TBSS ) {
|
|
353 f_tbhdr = &stbl[scns];
|
|
354 #endif /* S_TDATA (thread stuff) */
|
|
355
|
|
356 } else if( scntype == S_BSS) {
|
|
357 f_bhdr = &stbl[scns];
|
|
358 }
|
|
359
|
|
360 }
|
|
361
|
|
362 /* We will now convert TEXT and DATA into TEXT, BSS into DATA, and leave
|
|
363 * all thread stuff alone.
|
|
364 */
|
|
365
|
|
366 /* Now we alter the contents of all the f_*hdr variables
|
|
367 to correspond to what we want to dump. */
|
|
368
|
|
369 f_thdr->s_vaddr = (long) start_of_text ();
|
|
370 f_thdr->s_size = data_start - f_thdr->s_vaddr;
|
|
371 f_thdr->s_scnptr = pagesz;
|
|
372 f_thdr->s_relptr = 0;
|
|
373 f_thdr->s_nrel = 0;
|
|
374
|
|
375 eo_data = f_thdr->s_scnptr + f_thdr->s_size;
|
|
376
|
|
377 if( f_tdhdr ) { /* Process thread data */
|
|
378
|
|
379 f_tdhdr->s_vaddr = data_start;
|
|
380 f_tdhdr->s_size += f_dhdr->s_size - (data_start - f_dhdr->s_vaddr);
|
|
381 f_tdhdr->s_scnptr = eo_data;
|
|
382 f_tdhdr->s_relptr = 0;
|
|
383 f_tdhdr->s_nrel = 0;
|
|
384
|
|
385 eo_data += f_tdhdr->s_size;
|
|
386
|
|
387 /* And now for DATA */
|
|
388
|
|
389 f_dhdr->s_vaddr = f_bhdr->s_vaddr; /* Take BSS start address */
|
|
390 f_dhdr->s_size = bss_end - f_bhdr->s_vaddr;
|
|
391 f_dhdr->s_scnptr = eo_data;
|
|
392 f_dhdr->s_relptr = 0;
|
|
393 f_dhdr->s_nrel = 0;
|
|
394
|
|
395 eo_data += f_dhdr->s_size;
|
|
396
|
|
397 } else {
|
|
398
|
|
399 f_dhdr->s_vaddr = data_start;
|
|
400 f_dhdr->s_size = bss_start - data_start;
|
|
401 f_dhdr->s_scnptr = eo_data;
|
|
402 f_dhdr->s_relptr = 0;
|
|
403 f_dhdr->s_nrel = 0;
|
|
404
|
|
405 eo_data += f_dhdr->s_size;
|
|
406
|
|
407 }
|
|
408
|
|
409 f_bhdr->s_vaddr = bss_start;
|
|
410 f_bhdr->s_size = bss_end - bss_start + pagesz /* fudge */;
|
|
411 f_bhdr->s_scnptr = 0;
|
|
412 f_bhdr->s_relptr = 0;
|
|
413 f_bhdr->s_nrel = 0;
|
|
414
|
|
415 text_scnptr = f_thdr->s_scnptr;
|
|
416 data_scnptr = f_dhdr->s_scnptr;
|
|
417 bias = eo_data - block_copy_start;
|
|
418
|
|
419 if (f_ohdr.o_symptr > 0L) {
|
|
420 f_ohdr.o_symptr += bias;
|
|
421 }
|
|
422
|
|
423 if (f_hdr.h_strptr > 0) {
|
|
424 f_hdr.h_strptr += bias;
|
|
425 }
|
|
426
|
|
427 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) {
|
|
428 PERROR (new_name);
|
|
429 }
|
|
430
|
|
431 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) {
|
|
432 PERROR (new_name);
|
|
433 }
|
|
434
|
|
435 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) {
|
|
436
|
|
437 /* This is a cheesy little loop to write out the section headers
|
|
438 * in order of increasing virtual address. Dull but effective.
|
|
439 */
|
|
440
|
|
441 for( i = scns+1; i < f_hdr.h_nscns; i++ ) {
|
|
442 if( stbl[i].s_vaddr < stbl[scns].s_vaddr ) { /* Swap */
|
|
443 scntemp = stbl[i];
|
|
444 stbl[i] = stbl[scns];
|
|
445 stbl[scns] = scntemp;
|
|
446 }
|
|
447 }
|
|
448
|
|
449 }
|
|
450
|
|
451 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) {
|
|
452
|
|
453 if( write( new, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) {
|
|
454 PERROR (new_name);
|
|
455 }
|
|
456
|
|
457 }
|
|
458
|
|
459 return (0);
|
|
460
|
|
461 }
|
|
462
|
|
463 /* ****************************************************************
|
|
464 * copy_text_and_data
|
|
465 *
|
|
466 * Copy the text and data segments from memory to the new a.out
|
|
467 */
|
|
468 static int
|
|
469 copy_text_and_data (new)
|
|
470 int new;
|
|
471 {
|
|
472 int scns;
|
|
473
|
|
474 for( scns = 0; scns < f_hdr.h_nscns; scns++ )
|
|
475 write_segment( new, &stbl[scns] );
|
|
476
|
|
477 return 0;
|
|
478 }
|
|
479
|
|
480 write_segment( new, sptr )
|
|
481 int new;
|
|
482 struct scnhdr *sptr;
|
|
483 {
|
|
484 char *ptr, *end;
|
|
485 int nwrite, ret;
|
|
486 char buf[80];
|
|
487 extern int errno;
|
|
488 char zeros[128];
|
|
489
|
|
490 if( sptr->s_scnptr == 0 )
|
|
491 return; /* Nothing to do */
|
|
492
|
|
493 if( lseek( new, (long) sptr->s_scnptr, 0 ) == -1 )
|
|
494 PERROR( "unexecing" );
|
|
495
|
647
|
496 memset (zeros, 0, sizeof (zeros));
|
428
|
497
|
|
498 ptr = (char *) sptr->s_vaddr;
|
|
499 end = ptr + sptr->s_size;
|
|
500
|
|
501 while( ptr < end ) {
|
|
502
|
|
503 /* distance to next multiple of 128. */
|
|
504 nwrite = (((int) ptr + 128) & -128) - (int) ptr;
|
|
505 /* But not beyond specified end. */
|
|
506 if (nwrite > end - ptr) nwrite = end - ptr;
|
|
507 ret = write (new, ptr, nwrite);
|
|
508 /* If write gets a page fault, it means we reached
|
|
509 a gap between the old text segment and the old data segment.
|
|
510 This gap has probably been remapped into part of the text segment.
|
|
511 So write zeros for it. */
|
|
512 if (ret == -1 && errno == EFAULT)
|
|
513 write (new, zeros, nwrite);
|
|
514 else if (nwrite != ret) {
|
|
515 sprintf (buf,
|
|
516 "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d",
|
|
517 ptr, new, nwrite, ret, errno);
|
|
518 PERROR (buf);
|
|
519 }
|
|
520 ptr += nwrite;
|
|
521 }
|
|
522 }
|
|
523
|
|
524 /* ****************************************************************
|
|
525 * copy_sym
|
|
526 *
|
|
527 * Copy the relocation information and symbol table from the a.out to the new
|
|
528 */
|
|
529 static int
|
|
530 copy_sym (new, a_out, a_name, new_name)
|
|
531 int new, a_out;
|
|
532 char *a_name, *new_name;
|
|
533 {
|
|
534 char page[1024];
|
|
535 int n;
|
|
536
|
|
537 if (a_out < 0)
|
|
538 return 0;
|
|
539
|
|
540 if (SYMS_START == 0L)
|
|
541 return 0;
|
|
542
|
|
543 lseek (a_out, SYMS_START, 0); /* Position a.out to symtab. */
|
|
544 lseek( new, (long)f_ohdr.o_symptr, 0 );
|
|
545
|
647
|
546 while ((n = read (a_out, page, sizeof (page))) > 0) {
|
428
|
547 if (write (new, page, n) != n) {
|
|
548 PERROR (new_name);
|
|
549 }
|
|
550 }
|
|
551 if (n < 0) {
|
|
552 PERROR (a_name);
|
|
553 }
|
|
554 return 0;
|
|
555 }
|
|
556
|
|
557 /* ****************************************************************
|
|
558 * mark_x
|
|
559 *
|
|
560 * After successfully building the new a.out, mark it executable
|
|
561 */
|
|
562 static
|
|
563 mark_x (name)
|
|
564 char *name;
|
|
565 {
|
|
566 struct stat sbuf;
|
|
567 int um;
|
|
568 int new = 0; /* for PERROR */
|
|
569
|
|
570 um = umask (777);
|
|
571 umask (um);
|
|
572 if (stat (name, &sbuf) == -1) {
|
|
573 PERROR (name);
|
|
574 }
|
|
575 sbuf.st_mode |= 0111 & ~um;
|
|
576 if (chmod (name, sbuf.st_mode) == -1)
|
|
577 PERROR (name);
|
|
578 }
|
|
579
|
|
580 /* Find the first pty letter. This is usually 'p', as in ptyp0, but
|
|
581 is sometimes configured down to 'm', 'n', or 'o' for some reason. */
|
|
582
|
|
583 first_pty_letter ()
|
|
584 {
|
|
585 struct stat buf;
|
|
586 char pty_name[16];
|
|
587 char c;
|
|
588
|
|
589 for (c = 'o'; c >= 'a'; c--)
|
|
590 {
|
|
591 sprintf (pty_name, "/dev/pty%c0", c);
|
|
592 if (stat (pty_name, &buf) < 0)
|
|
593 return c + 1;
|
|
594 }
|
|
595 return 'a';
|
|
596 }
|
|
597
|