/[meta-cvs]/meta-cvs/F-83F0E6A90D5BBCDFB58440970E850925.c
ViewVC logotype

Contents of /meta-cvs/F-83F0E6A90D5BBCDFB58440970E850925.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations)
Thu Feb 13 06:16:42 2003 UTC (11 years, 2 months ago) by kaz
Branch: MAIN
CVS Tags: mcvs-1-0-3
Changes since 1.10: +7 -0 lines
File MIME type: text/plain
Merging from mcvs-1-0-branch.

* code/unix-bindings/wrap.c (mcvs-spawn): In the child process,
reset the signal handler for SIGCHLD to SIG_DFL before exec-ing
the new image. This is needed because CLISP set it to SIG_IGN,
which causes child reaping problems in spawned programs.
This should fix the ``No child processes'' problem when running
CVS from Meta-CVS.
1 kaz 1.1 #include <stdio.h>
2 kaz 1.3 #include <stdlib.h>
3 kaz 1.1 #include <string.h>
4     #include <errno.h>
5 kaz 1.11 #include <signal.h>
6 kaz 1.1 #include <dirent.h>
7 kaz 1.3 #include <unistd.h>
8 kaz 1.1 #include <sys/types.h>
9     #include <sys/stat.h>
10 kaz 1.5 #include <sys/wait.h>
11 kaz 1.1
12     /*
13 kaz 1.2 * Null pointer test
14     */
15    
16 kaz 1.8 int mcvs_null_pointer_p(void *ptr)
17 kaz 1.2 {
18     return ptr == 0;
19     }
20    
21     /*
22 kaz 1.1 * <errno.h> stuff
23     */
24    
25 kaz 1.8 int mcvs_get_errno(void)
26 kaz 1.1 {
27     return errno;
28     }
29    
30 kaz 1.8 int mcvs_set_errno(int value)
31 kaz 1.1 {
32     return errno = value;
33     }
34    
35     /*
36     * <dirent.h> stuff
37     */
38    
39     typedef struct {
40     unsigned long d_ino;
41     char d_name[1024];
42 kaz 1.8 } mcvs_dirent;
43 kaz 1.1
44 kaz 1.8 mcvs_dirent *mcvs_readdir(DIR *dir)
45 kaz 1.1 {
46 kaz 1.8 static mcvs_dirent dw;
47 kaz 1.1 struct dirent *de = readdir(dir);
48     if (de != 0) {
49     strncpy(dw.d_name, de->d_name, sizeof dw.d_name - 1);
50     dw.d_ino = de->d_ino;
51     return &dw;
52     }
53     return 0;
54     }
55    
56     /*
57 kaz 1.4 * <unistd.h> stuff
58     */
59    
60 kaz 1.8 char *mcvs_readlink(const char *path)
61 kaz 1.4 {
62     size_t size = 256;
63     char *str = malloc(size);
64     char *temp;
65    
66     if (str == 0)
67     goto bail;
68    
69     for (;;) {
70     int result = readlink(path, str, size);
71     if (result == -1)
72     goto bail;
73     if (result < size) {
74     str[result] = 0;
75     break;
76     }
77     if (size * 2 < size)
78     goto bail;
79     size *= 2;
80     if ((temp = realloc(str, size)) == 0)
81     goto bail;
82     str = temp;
83     }
84    
85     /* No need to realloc to actual size, since CLISP will free this anyway */
86     return str;
87    
88     bail:
89     free(str);
90     return 0;
91     }
92    
93     /*
94 kaz 1.1 * <sys/stat.h> stuff
95     */
96    
97 kaz 1.8 struct mcvs_stat {
98 kaz 1.1 unsigned long dev;
99     unsigned long ino;
100     unsigned long mode;
101     unsigned int nlink;
102     unsigned int uid;
103     unsigned int gid;
104     unsigned long rdev;
105     unsigned long blksize;
106     unsigned long blocks;
107     unsigned long atime;
108     unsigned long mtime;
109     unsigned long ctime;
110     };
111    
112 kaz 1.8 static void stat_to_wrap(struct mcvs_stat *out, const struct stat *in)
113 kaz 1.1 {
114     out->dev = in->st_dev;
115     out->ino = in->st_ino;
116     out->mode = in->st_mode;
117     out->nlink = in->st_nlink;
118     out->uid = in->st_uid;
119     out->gid = in->st_gid;
120     out->rdev = in->st_rdev;
121     out->blksize = in->st_blksize;
122     out->blocks = in->st_blocks;
123     out->atime = in->st_atime;
124     out->mtime = in->st_mtime;
125     out->ctime = in->st_ctime;
126     }
127    
128     #define IMPL_STAT(FUNC, ARGTYPE) \
129 kaz 1.8 int mcvs_ ## FUNC(ARGTYPE arg, struct mcvs_stat *buf) \
130 kaz 1.1 { \
131     struct stat sbuf; \
132     int retval = FUNC(arg, &sbuf); \
133     if (retval == 0) \
134 kaz 1.8 stat_to_wrap(buf, &sbuf); \
135 kaz 1.1 return retval; \
136     }
137    
138     IMPL_STAT(stat, const char *)
139     IMPL_STAT(lstat, const char *)
140     IMPL_STAT(fstat, int)
141 kaz 1.3
142     /*
143     * <unistd.h> -- getcwd
144     */
145    
146 kaz 1.8 const char *mcvs_getcwd(void)
147 kaz 1.3 {
148     size_t size = 256;
149     char *str = malloc(size);
150     char *temp;
151    
152     if (str == 0)
153     goto bail;
154    
155     while (getcwd(str, size) == 0) {
156 kaz 1.9 if (errno != ERANGE)
157     goto bail;
158 kaz 1.3 if (size * 2 < size)
159     goto bail;
160     size *= 2;
161     if ((temp = realloc(str, size)) == 0)
162     goto bail;
163     str = temp;
164     }
165    
166     /* No need to realloc to actual size, since CLISP will free this anyway */
167     return str;
168    
169     bail:
170     free(str);
171     return 0;
172 kaz 1.5 }
173    
174     /*
175     * <unistd.h> -- fork, wait*, exec*
176     */
177    
178 kaz 1.7 #ifdef __CYGWIN__
179    
180     /*
181     * waitpid is broken garbage on Cygwin, so we use its spawnvp function
182     * instead!
183     */
184    
185     #include <process.h>
186 kaz 1.8 int mcvs_spawn(const char *name, const char *const *argv)
187 kaz 1.7 {
188     return spawnvp(_P_WAIT, name, argv);
189     }
190     #else
191 kaz 1.8 int mcvs_spawn(const char *name, char *const *argv)
192 kaz 1.5 {
193 kaz 1.7 int result = -1;
194     int status = 0;
195    
196 kaz 1.5 pid_t child = fork();
197    
198     if (child == -1)
199     goto out;
200    
201     if (child == 0) {
202 kaz 1.11 /*
203     * We need this because CLISP sets it to SIG_IGN, which is
204     * inherited by the exec'd image, and causes the wait()
205     * functions to have behavior that programs don't expect.
206     */
207     signal(SIGCHLD, SIG_DFL);
208 kaz 1.5 execvp(name, argv);
209     _exit(EXIT_FAILURE);
210     }
211    
212 kaz 1.10 do {
213     result = waitpid(child, &status, 0);
214     } while (result == -1 && errno == EINTR);
215    
216     if (result == -1)
217 kaz 1.5 goto out;
218    
219     if (WIFEXITED(status))
220     result = WEXITSTATUS(status);
221    
222     out:
223 kaz 1.6 free((void *) argv);
224 kaz 1.5 return result;
225 kaz 1.3 }
226 kaz 1.7 #endif

  ViewVC Help
Powered by ViewVC 1.1.5