annotate src/unexaix.c @ 531:202463e1df14

*** empty log message ***
author Jim Blandy <jimb@redhat.com>
date Tue, 04 Feb 1992 02:27:43 +0000
parents 8c615e453683
children 68111c4a3885
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
117
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
1 /*
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
2 This file is not used because it occasionally fails to work. This
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
3 happens because the bss address when Emacs is run is not always the
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
4 same. If it happens to be different from what it was
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
5 when Emacs was dumped, the dumped data won't work.
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
6 No one has been able to prevent the address from varying.
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
7
485
8c615e453683 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 117
diff changeset
8 The following comments should be in share-lib/MACHINES if this
8c615e453683 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 117
diff changeset
9 dumping is ever made to work:
117
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
10
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
11 Note that Emacs can store the pure Lisp data in a shared segment on
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
12 this machine, but only if you put this command in one of the boot
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
13 scripts
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
14
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
15 emacs -batch -q
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
16
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
17 so that it is run as root when the machine is booted. This command
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
18 creates a read-only shared segment owned by root. Otherwise the
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
19 pure data is not shared. The pure data size is around 100K, which
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
20 may not be enough to matter for most users of this machine.
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
21 */
08356dc1077c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 110
diff changeset
22
110
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23 /* Dumping and loading data areas, for Emacs under AIX.
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 (It may also work on other kinds of system V.)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25 Copyright (C) 1990 Free Software Foundation, Inc.
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27 This file is part of GNU Emacs.
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29 GNU Emacs is free software; you can redistribute it and/or modify
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30 it under the terms of the GNU General Public License as published by
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31 the Free Software Foundation; either version 1, or (at your option)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32 any later version.
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
33
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
34 GNU Emacs is distributed in the hope that it will be useful,
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37 GNU General Public License for more details.
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39 You should have received a copy of the GNU General Public License
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 along with GNU Emacs; see the file COPYING. If not, write to
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43 /* This is based on a public domain program written by IBM. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45 /*************** SYSTEM DEFINES *********************************/
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 #include "config.h"
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48 #include "paths.h"
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49 #include <sys/types.h>
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 #include <sys/files.h>
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 #include <fcntl.h>
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52 #include <sys/mode.h>
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 #include <sys/ipc.h>
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 #include <sys/shm.h>
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 #include <malloc.h>
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56 #include <stdio.h> /* MWW */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 #include "lisp.h"
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 /*************** LOCAL DEFINES **********************************/
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 struct data_header /* saved data header */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 char *start; /* dump _data addr */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 char *end; /* dump _end addr */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 char *sbrk1; /* dump original sbrk addr */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 char *sbrk2; /* dump final sbrk addr */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 int puresize; /* size of pure data dumped */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 };
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 #define EMACSSHMKEY "EMACSSHMKEY"
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71 #define EMACS_DATA_FILE "EMACS-DATA"
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 #define NEW_SHMGET_FLAGS (IPC_CREAT | S_IWUSR | S_IRUSR \
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 #define OLD_SHMAT_FLAGS SHM_RDONLY
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 #define OLD_SHMGET_FLAGS (S_IRUSR | S_IRGRP | S_IROTH)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 #define OLD_OPEN_FLAGS O_RDONLY
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 #define NEW_OPEN_FLAGS (O_RDWR | O_CREAT | O_TRUNC)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 /*************** EXTERNAL / GLOBAL DATA AREA ********************/
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 extern char _data; /* start of data addr */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 extern char _end; /* end of all data + 1 addr */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 static char *original_sbrk; /* sbrk when dump first run */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
85 void
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86 map_in_data (use_dumped_data)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 int use_dumped_data;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
88 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89 int bufsize; /* malloc buffer size */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90 struct data_header dh; /* saved data header */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 int fd; /* saved data file descriptor */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 char *finaladdr; /* last addr in bucket */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93 char *ipckey = getenv (EMACSSHMKEY); /* env ipc key string */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94 int length; /* dumped data lengths */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 char *newaddr; /* new malloc buffer addr */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96 int numblks; /* number of remaining mallocs */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97 int shmid; /* shared memory id */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98 key_t shmkey; /* shared memory key */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 /* Note that using malloc here may not be safe. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100 char name[sizeof (PATH_EXEC) + sizeof (EMACS_DATA_FILE) + 2];
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 /* Consume remaining malloc space without increasing */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103 /* the end of data space */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 original_sbrk = sbrk (0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 for (bufsize = 16; bufsize < getpagesize (); bufsize *= 2)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
107 while ((newaddr = (char *)malloc (bufsize - 8)) < original_sbrk)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108 ;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109 for (numblks = (getpagesize () / bufsize) - 1; numblks > 0; numblks--)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
110 malloc (bufsize - 8);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111 finaladdr = sbrk (0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 original_sbrk = sbrk (0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 /* If we don't want the dumped data, get an unshared segment. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116 if (!use_dumped_data)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 shmid = shmget (IPC_PRIVATE, PURESIZE, NEW_SHMGET_FLAGS);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 if (shmid == -1
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
120 || shmat (shmid, (char *)PURE_SEG_BITS, 0) == -1)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 fprintf (stderr, "emacs: failure obtaining new unshared memory segment.\n");
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 exit (1);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125 return;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 /* Compute the file name with the dumped data. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129 strcpy (name, PATH_EXEC);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130 strcat (name, "/");
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131 strcat (name, EMACS_DATA_FILE);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133 /* Open the file and make sure the addresses have not changed. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134 fd = open (name, OLD_OPEN_FLAGS, 0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135 if (fd < 0)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 fprintf (stderr, "emacs: failure opening `%s'\n", name);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 exit (1);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 if (read (fd, (char *)&dh, sizeof (dh)) != sizeof (dh)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 || dh.start != &_data
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 || dh.end != &_end
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 || dh.sbrk1 != original_sbrk
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144 || dh.puresize != PURESIZE)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
146 fprintf (stderr, "emacs: header mismatch in `%s'\n", name);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147 exit (1);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150 /* Load in the unshared contents. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 if (!(length = dh.end - dh.start)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 || read (fd, (char *)&_data, length) != length
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153 || !(length = dh.sbrk2 - dh.sbrk1)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
154 || brk (dh.sbrk2) == -1
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
155 || read (fd, dh.sbrk1, length) != length)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
156 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
157 fprintf (stderr, "emacs: failure loading unshared data.\n");
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
158 exit (1);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
159 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
160
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 /* Determine ipc key from environment or default */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 if (ipckey && *ipckey)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163 shmkey = atoi (ipckey);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164 else
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165 shmkey = SHMKEY;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 /* Attach to "pure data" shared memory segment */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 if ((shmid = shmget (shmkey, 0, 0)) == -1
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 || (newaddr = shmat (shmid, (char *)PURE_SEG_BITS, OLD_SHMAT_FLAGS)) == -1)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171 /* We were unable to open an existing segment. Make a new one. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
172 struct shmid_ds buf;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
173
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
174 /* First get rid of the one we tried to get. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
175 shmdt ((char *)PURE_SEG_BITS);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 shmctl (shmid, IPC_RMID, 0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 /* If we could not write the data file,
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 don't make a shared segment that we could write.
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 Make an unshared segment instead. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 if (access (name, W_OK) == 0)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 shmid = shmget (IPC_PRIVATE, PURESIZE, NEW_SHMGET_FLAGS);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 if (shmid == -1
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 || shmat (shmid, (char *)PURE_SEG_BITS, 0) == -1)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
186 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
187 fprintf (stderr, "emacs: failure obtaining new unshared memory segment.\n");
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188 exit (1);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
190
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
191 /* Load the proper data into it. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
192 if (read (fd, PURE_SEG_BITS, PURESIZE) != PURESIZE)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 fprintf (stderr, "emacs: failure loading shared memory data.\n");
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195 shmdt ((char *)PURE_SEG_BITS);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196 shmctl (shmid, IPC_RMID, 0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 exit (1);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
198 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
199
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
200 close (fd);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
201 return;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
202 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
203
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
204 /* Allocate the new shared segment and arrange to write it. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205 if ((shmid = shmget (shmkey, PURESIZE, NEW_SHMGET_FLAGS)) == -1
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 || shmat (shmid, (char *)PURE_SEG_BITS, 0) == -1)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
208 fprintf (stderr, "emacs: failure obtaining new shared memory segment.\n");
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209 shmdt ((char *)PURE_SEG_BITS);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210 shmctl (shmid, IPC_RMID, 0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
211 exit (1);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 /* Load the proper data into it. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215 if (read (fd, PURE_SEG_BITS, PURESIZE) != PURESIZE)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 fprintf (stderr, "emacs: failure loading shared memory data.\n");
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218 shmdt ((char *)PURE_SEG_BITS);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219 shmctl (shmid, IPC_RMID, 0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220 exit (1);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
222
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
223 /* Detach from the segment and bring it back readonly. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224 shmdt ((char *)PURE_SEG_BITS);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226 shmctl (shmid, IPC_STAT, &buf);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227 buf.shm_perm.mode = OLD_SHMGET_FLAGS;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
228 shmctl (shmid, IPC_SET, &buf);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
230 newaddr = shmat (shmid, (char *)PURE_SEG_BITS, OLD_SHMAT_FLAGS);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231 if (newaddr == -1)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 fprintf (stderr, "emacs: failure reattaching shared memory segment.\n");
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
234 shmctl (shmid, IPC_RMID, 0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235 exit (1);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
237 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239 close (fd);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240 }
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
241
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
242 /* Dump the appropriate parts of memory into a file named NEW
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
243 from which the shared segment can be initialized. */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
244
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
245 void
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
246 map_out_data (new)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
247 char *new;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
248 {
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
249 struct data_header dh; /* saved data header */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
250 int fd; /* saved data file descriptor */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
251 int length; /* dumped data length; */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
252
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
253
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
254 /* Create "saved data" file header */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
255 dh.start = &_data;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
256 dh.end = &_end;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
257 dh.sbrk1 = original_sbrk;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
258 dh.sbrk2 = sbrk (0);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
259 dh.puresize = PURESIZE;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
260
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
261 /* Create new "saved data" dump file */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
262 unlink (new);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
263 fd = open (new, NEW_OPEN_FLAGS, 0666);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
264 if (fd < 0)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
265 report_file_error ("Opening dump file", Fcons (build_string (new), Qnil));
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
266
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
267 /* Write saved header and data */
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
268 length = sizeof (dh);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
269 if (write (fd, (char *)&dh, length) != length)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
270 report_file_error ("Writing dump file header",
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
271 Fcons (build_string (new), Qnil));
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
272 length = dh.end - dh.start;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
273 if (write (fd, dh.start, length) != length)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
274 report_file_error ("Writing low core in dump file",
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
275 Fcons (build_string (new), Qnil));
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
276 length = dh.sbrk2 - dh.sbrk1;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
277 if (write (fd, dh.sbrk1, length) != length)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
278 report_file_error ("Writing heap in dump file",
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
279 Fcons (build_string (new), Qnil));
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
280 length = PURESIZE;
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
281 if (write (fd, PURE_SEG_BITS, length) != length)
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
282 report_file_error ("Writing pure data in dump file",
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
283 Fcons (build_string (new), Qnil));
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
284 close (fd);
2968112113f9 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
285 }