| 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 |
#define NO_SHORTNAMES |
|---|
| 29 |
#include <../src/config.h> |
|---|
| 30 |
#include <signal.h> |
|---|
| 31 |
#undef signal |
|---|
| 32 |
|
|---|
| 33 |
#if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC) |
|---|
| 34 |
#include <stdio.h> |
|---|
| 35 |
|
|---|
| 36 |
int |
|---|
| 37 |
main () |
|---|
| 38 |
{ |
|---|
| 39 |
fprintf (stderr, "Sorry, the Emacs server is supported only on systems\n"); |
|---|
| 40 |
fprintf (stderr, "with Berkeley sockets or System V IPC.\n"); |
|---|
| 41 |
exit (1); |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
#else |
|---|
| 45 |
|
|---|
| 46 |
void perror_1 (); |
|---|
| 47 |
void fatal_error (); |
|---|
| 48 |
|
|---|
| 49 |
#if defined (HAVE_SOCKETS) && ! defined (NO_SOCKETS_IN_FILE_SYSTEM) |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
#include <sys/types.h> |
|---|
| 53 |
#include <sys/file.h> |
|---|
| 54 |
#include <sys/socket.h> |
|---|
| 55 |
#include <sys/un.h> |
|---|
| 56 |
#include <stdio.h> |
|---|
| 57 |
#include <errno.h> |
|---|
| 58 |
#include <sys/stat.h> |
|---|
| 59 |
|
|---|
| 60 |
#ifdef HAVE_UNISTD_H |
|---|
| 61 |
#include <unistd.h> |
|---|
| 62 |
#endif |
|---|
| 63 |
|
|---|
| 64 |
#ifndef errno |
|---|
| 65 |
extern int errno; |
|---|
| 66 |
#endif |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
#ifdef FD_SET |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
#ifdef FD_SETSIZE |
|---|
| 74 |
#define MAXDESC FD_SETSIZE |
|---|
| 75 |
#else |
|---|
| 76 |
#define MAXDESC 64 |
|---|
| 77 |
#endif |
|---|
| 78 |
#define SELECT_TYPE fd_set |
|---|
| 79 |
#else |
|---|
| 80 |
#define MAXDESC 32 |
|---|
| 81 |
#define SELECT_TYPE int |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
#define FD_SET(n, p) (*(p) |= (1 << (n))) |
|---|
| 85 |
#define FD_CLR(n, p) (*(p) &= ~(1 << (n))) |
|---|
| 86 |
#define FD_ISSET(n, p) (*(p) & (1 << (n))) |
|---|
| 87 |
#define FD_ZERO(p) (*(p) = 0) |
|---|
| 88 |
#endif |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
char *socket_name; |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
char *progname; |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
SIGTYPE |
|---|
| 103 |
delete_socket (sig) |
|---|
| 104 |
int sig; |
|---|
| 105 |
{ |
|---|
| 106 |
signal (sig, SIG_DFL); |
|---|
| 107 |
unlink (socket_name); |
|---|
| 108 |
kill (getpid (), sig); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
void |
|---|
| 114 |
handle_signals () |
|---|
| 115 |
{ |
|---|
| 116 |
signal (SIGHUP, delete_socket); |
|---|
| 117 |
signal (SIGINT, delete_socket); |
|---|
| 118 |
signal (SIGQUIT, delete_socket); |
|---|
| 119 |
signal (SIGILL, delete_socket); |
|---|
| 120 |
signal (SIGTRAP, delete_socket); |
|---|
| 121 |
#ifdef SIGABRT |
|---|
| 122 |
signal (SIGABRT, delete_socket); |
|---|
| 123 |
#endif |
|---|
| 124 |
#ifdef SIGHWE |
|---|
| 125 |
signal (SIGHWE, delete_socket); |
|---|
| 126 |
#endif |
|---|
| 127 |
#ifdef SIGPRE |
|---|
| 128 |
signal (SIGPRE, delete_socket); |
|---|
| 129 |
#endif |
|---|
| 130 |
#ifdef SIGORE |
|---|
| 131 |
signal (SIGORE, delete_socket); |
|---|
| 132 |
#endif |
|---|
| 133 |
#ifdef SIGUME |
|---|
| 134 |
signal (SIGUME, delete_socket); |
|---|
| 135 |
#endif |
|---|
| 136 |
#ifdef SIGDLK |
|---|
| 137 |
signal (SIGDLK, delete_socket); |
|---|
| 138 |
#endif |
|---|
| 139 |
#ifdef SIGCPULIM |
|---|
| 140 |
signal (SIGCPULIM, delete_socket); |
|---|
| 141 |
#endif |
|---|
| 142 |
#ifdef SIGIOT |
|---|
| 143 |
|
|---|
| 144 |
signal (SIGIOT, delete_socket); |
|---|
| 145 |
#endif |
|---|
| 146 |
#ifdef SIGEMT |
|---|
| 147 |
signal (SIGEMT, delete_socket); |
|---|
| 148 |
#endif |
|---|
| 149 |
signal (SIGFPE, delete_socket); |
|---|
| 150 |
#ifdef SIGBUS |
|---|
| 151 |
signal (SIGBUS, delete_socket); |
|---|
| 152 |
#endif |
|---|
| 153 |
signal (SIGSEGV, delete_socket); |
|---|
| 154 |
#ifdef SIGSYS |
|---|
| 155 |
signal (SIGSYS, delete_socket); |
|---|
| 156 |
#endif |
|---|
| 157 |
signal (SIGTERM, delete_socket); |
|---|
| 158 |
#ifdef SIGXCPU |
|---|
| 159 |
signal (SIGXCPU, delete_socket); |
|---|
| 160 |
#endif |
|---|
| 161 |
#ifdef SIGXFSZ |
|---|
| 162 |
signal (SIGXFSZ, delete_socket); |
|---|
| 163 |
#endif |
|---|
| 164 |
|
|---|
| 165 |
#ifdef AIX |
|---|
| 166 |
|
|---|
| 167 |
signal (SIGXCPU, delete_socket); |
|---|
| 168 |
#ifndef _I386 |
|---|
| 169 |
signal (SIGIOINT, delete_socket); |
|---|
| 170 |
#endif |
|---|
| 171 |
signal (SIGGRANT, delete_socket); |
|---|
| 172 |
signal (SIGRETRACT, delete_socket); |
|---|
| 173 |
signal (SIGSOUND, delete_socket); |
|---|
| 174 |
signal (SIGMSG, delete_socket); |
|---|
| 175 |
#endif |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
void |
|---|
| 180 |
error (s1, s2) |
|---|
| 181 |
char *s1, *s2; |
|---|
| 182 |
{ |
|---|
| 183 |
fprintf (stderr, "%s: ", progname); |
|---|
| 184 |
fprintf (stderr, s1, s2); |
|---|
| 185 |
fprintf (stderr, "\n"); |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
void |
|---|
| 190 |
fatal (s1, s2) |
|---|
| 191 |
char *s1, *s2; |
|---|
| 192 |
{ |
|---|
| 193 |
error (s1, s2); |
|---|
| 194 |
exit (1); |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
long * |
|---|
| 200 |
xmalloc (size) |
|---|
| 201 |
unsigned int size; |
|---|
| 202 |
{ |
|---|
| 203 |
long *result = (long *) malloc (size); |
|---|
| 204 |
if (result == NULL) |
|---|
| 205 |
fatal ("virtual memory exhausted", 0); |
|---|
| 206 |
return result; |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
int |
|---|
| 210 |
main (argc, argv) |
|---|
| 211 |
int argc; |
|---|
| 212 |
char **argv; |
|---|
| 213 |
{ |
|---|
| 214 |
char *system_name; |
|---|
| 215 |
int system_name_length; |
|---|
| 216 |
int s, infd; |
|---|
| 217 |
#ifdef SOCKLEN_TYPE |
|---|
| 218 |
SOCKLEN_TYPE fromlen; |
|---|
| 219 |
#else |
|---|
| 220 |
size_t fromlen; |
|---|
| 221 |
#endif |
|---|
| 222 |
struct sockaddr_un server, fromunix; |
|---|
| 223 |
#ifdef SERVER_HOME_DIR |
|---|
| 224 |
char *homedir; |
|---|
| 225 |
#endif |
|---|
| 226 |
char *str, string[BUFSIZ], code[BUFSIZ]; |
|---|
| 227 |
FILE *infile; |
|---|
| 228 |
FILE **openfiles; |
|---|
| 229 |
int openfiles_size; |
|---|
| 230 |
struct stat statbuf; |
|---|
| 231 |
|
|---|
| 232 |
#ifndef convex |
|---|
| 233 |
char *getenv (); |
|---|
| 234 |
#endif |
|---|
| 235 |
|
|---|
| 236 |
progname = argv[0]; |
|---|
| 237 |
|
|---|
| 238 |
openfiles_size = 20; |
|---|
| 239 |
openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *)); |
|---|
| 240 |
if (openfiles == 0) |
|---|
| 241 |
abort (); |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
|---|
| 248 |
{ |
|---|
| 249 |
perror_1 ("socket"); |
|---|
| 250 |
exit (1); |
|---|
| 251 |
} |
|---|
| 252 |
server.sun_family = AF_UNIX; |
|---|
| 253 |
|
|---|
| 254 |
system_name_length = 32; |
|---|
| 255 |
while (1) |
|---|
| 256 |
{ |
|---|
| 257 |
system_name = (char *) xmalloc (system_name_length + 1); |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
system_name[system_name_length] = '\0'; |
|---|
| 261 |
|
|---|
| 262 |
if (gethostname (system_name, system_name_length) == 0) |
|---|
| 263 |
break; |
|---|
| 264 |
|
|---|
| 265 |
free (system_name); |
|---|
| 266 |
system_name_length *= 2; |
|---|
| 267 |
} |
|---|
| 268 |
|
|---|
| 269 |
#ifndef SERVER_HOME_DIR |
|---|
| 270 |
sprintf (server.sun_path, "/tmp/esrv%d-%s", (int) geteuid (), system_name); |
|---|
| 271 |
|
|---|
| 272 |
if (unlink (server.sun_path) == -1 && errno != ENOENT) |
|---|
| 273 |
{ |
|---|
| 274 |
perror_1 ("unlink"); |
|---|
| 275 |
exit (1); |
|---|
| 276 |
} |
|---|
| 277 |
#else |
|---|
| 278 |
if ((homedir = getenv ("HOME")) == NULL) |
|---|
| 279 |
fatal_error ("No home directory\n"); |
|---|
| 280 |
|
|---|
| 281 |
strcpy (server.sun_path, homedir); |
|---|
| 282 |
strcat (server.sun_path, "/.emacs-server-"); |
|---|
| 283 |
strcat (server.sun_path, system_name); |
|---|
| 284 |
|
|---|
| 285 |
unlink (server.sun_path); |
|---|
| 286 |
#endif |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
socket_name = (char *) xmalloc (strlen (server.sun_path) + 1); |
|---|
| 290 |
strcpy (socket_name, server.sun_path); |
|---|
| 291 |
|
|---|
| 292 |
handle_signals (); |
|---|
| 293 |
|
|---|
| 294 |
if (bind (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) < 0) |
|---|
| 295 |
{ |
|---|
| 296 |
perror_1 ("bind"); |
|---|
| 297 |
exit (1); |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
if (stat (server.sun_path, &statbuf) < 0) |
|---|
| 301 |
{ |
|---|
| 302 |
perror_1 ("bind"); |
|---|
| 303 |
exit (1); |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
chmod (server.sun_path, statbuf.st_mode & 0600); |
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
if (listen (s, 5) < 0) |
|---|
| 311 |
{ |
|---|
| 312 |
perror_1 ("listen"); |
|---|
| 313 |
exit (1); |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
signal (SIGPIPE, SIG_IGN); |
|---|
| 318 |
for (;;) |
|---|
| 319 |
{ |
|---|
| 320 |
SELECT_TYPE rmask; |
|---|
| 321 |
FD_ZERO (&rmask); |
|---|
| 322 |
FD_SET (0, &rmask); |
|---|
| 323 |
FD_SET (s, &rmask); |
|---|
| 324 |
if (select (s + 1, &rmask, 0, 0, 0) < 0) |
|---|
| 325 |
perror_1 ("select"); |
|---|
| 326 |
if (FD_ISSET (s, &rmask)) |
|---|
| 327 |
{ |
|---|
| 328 |
fromlen = sizeof (fromunix); |
|---|
| 329 |
fromunix.sun_family = AF_UNIX; |
|---|
| 330 |
infd = accept (s, (struct sockaddr *) &fromunix, &fromlen); |
|---|
| 331 |
if (infd < 0) |
|---|
| 332 |
{ |
|---|
| 333 |
if (errno == EMFILE || errno == ENFILE) |
|---|
| 334 |
fprintf (stderr, "Error: too many clients.\n"); |
|---|
| 335 |
else |
|---|
| 336 |
perror_1 ("accept"); |
|---|
| 337 |
continue; |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
if (infd >= openfiles_size) |
|---|
| 341 |
{ |
|---|
| 342 |
openfiles_size *= 2; |
|---|
| 343 |
openfiles = (FILE **) realloc (openfiles, |
|---|
| 344 |
openfiles_size * sizeof (FILE *)); |
|---|
| 345 |
if (openfiles == 0) |
|---|
| 346 |
abort (); |
|---|
| 347 |
} |
|---|
| 348 |
|
|---|
| 349 |
infile = fdopen (infd, "r+"); |
|---|
| 350 |
if (infile == NULL) |
|---|
| 351 |
{ |
|---|
| 352 |
fprintf (stderr, "Error: too many clients.\n"); |
|---|
| 353 |
write (infd, "Too many clients.\n", 18); |
|---|
| 354 |
close (infd); |
|---|
| 355 |
continue; |
|---|
| 356 |
} |
|---|
| 357 |
str = fgets (string, BUFSIZ, infile); |
|---|
| 358 |
if (str == NULL) |
|---|
| 359 |
{ |
|---|
| 360 |
perror_1 ("fgets"); |
|---|
| 361 |
close (infd); |
|---|
| 362 |
continue; |
|---|
| 363 |
} |
|---|
| 364 |
openfiles[infd] = infile; |
|---|
| 365 |
printf ("Client: %d %s", infd, string); |
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
while (string[strlen (string) - 1] != '\n') |
|---|
| 370 |
{ |
|---|
| 371 |
if (fgets (string, BUFSIZ, infile) == 0) |
|---|
| 372 |
break; |
|---|
| 373 |
printf ("%s", string); |
|---|
| 374 |
} |
|---|
| 375 |
fflush (stdout); |
|---|
| 376 |
fflush (infile); |
|---|
| 377 |
continue; |
|---|
| 378 |
} |
|---|
| 379 |
else if (FD_ISSET (0, &rmask)) |
|---|
| 380 |
{ |
|---|
| 381 |
|
|---|
| 382 |
clearerr (stdin); |
|---|
| 383 |
scanf ("%s %d%*c", code, &infd); |
|---|
| 384 |
if (ferror (stdin) || feof (stdin)) |
|---|
| 385 |
fatal_error ("server: error reading from standard input\n"); |
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
infile = openfiles[infd]; |
|---|
| 389 |
rewind (infile); |
|---|
| 390 |
while (1) |
|---|
| 391 |
{ |
|---|
| 392 |
if (fgets (string, BUFSIZ, stdin) == 0) |
|---|
| 393 |
break; |
|---|
| 394 |
fprintf (infile, "%s", string); |
|---|
| 395 |
if (string[strlen (string) - 1] == '\n') |
|---|
| 396 |
break; |
|---|
| 397 |
} |
|---|
| 398 |
fflush (infile); |
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
if (strncmp (code, "Close:", 6) == 0) |
|---|
| 402 |
if (infd > 2) |
|---|
| 403 |
{ |
|---|
| 404 |
fclose (infile); |
|---|
| 405 |
close (infd); |
|---|
| 406 |
} |
|---|
| 407 |
continue; |
|---|
| 408 |
} |
|---|
| 409 |
} |
|---|
| 410 |
} |
|---|
| 411 |
|
|---|
| 412 |
#else |
|---|
| 413 |
|
|---|
| 414 |
#include <sys/types.h> |
|---|
| 415 |
#include <sys/ipc.h> |
|---|
| 416 |
#include <sys/msg.h> |
|---|
| 417 |
#include <setjmp.h> |
|---|
| 418 |
#include <errno.h> |
|---|
| 419 |
#include <sys/utsname.h> |
|---|
| 420 |
|
|---|
| 421 |
struct utsname system_name; |
|---|
| 422 |
|
|---|
| 423 |
#ifndef errno |
|---|
| 424 |
extern int errno; |
|---|
| 425 |
#endif |
|---|
| 426 |
|
|---|
| 427 |
jmp_buf msgenv; |
|---|
| 428 |
|
|---|
| 429 |
SIGTYPE |
|---|
| 430 |
msgcatch () |
|---|
| 431 |
{ |
|---|
| 432 |
longjmp (msgenv, 1); |
|---|
| 433 |
} |
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
#include <stdio.h> |
|---|
| 440 |
|
|---|
| 441 |
int |
|---|
| 442 |
main () |
|---|
| 443 |
{ |
|---|
| 444 |
int s, infd, fromlen, ioproc; |
|---|
| 445 |
key_t key; |
|---|
| 446 |
struct msgbuf * msgp = |
|---|
| 447 |
(struct msgbuf *) malloc (sizeof *msgp + BUFSIZ); |
|---|
| 448 |
struct msqid_ds msg_st; |
|---|
| 449 |
int p; |
|---|
| 450 |
char *homedir, *getenv (); |
|---|
| 451 |
char string[BUFSIZ]; |
|---|
| 452 |
FILE *infile; |
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
if ((homedir = getenv ("HOME")) == NULL) |
|---|
| 458 |
fatal_error ("No home directory\n"); |
|---|
| 459 |
|
|---|
| 460 |
strcpy (string, homedir); |
|---|
| 461 |
#ifndef HAVE_LONG_FILE_NAMES |
|---|
| 462 |
|
|---|
| 463 |
strcat (string, "/.emacs-server"); |
|---|
| 464 |
#else |
|---|
| 465 |
strcat (string, "/.emacs-server-"); |
|---|
| 466 |
uname (&system_name); |
|---|
| 467 |
strcat (string, system_name.nodename); |
|---|
| 468 |
#endif |
|---|
| 469 |
creat (string, 0600); |
|---|
| 470 |
key = ftok (string, 1); |
|---|
| 471 |
s = msgget (key, 0600 | IPC_CREAT); |
|---|
| 472 |
if (s == -1) |
|---|
| 473 |
{ |
|---|
| 474 |
perror_1 ("msgget"); |
|---|
| 475 |
exit (1); |
|---|
| 476 |
} |
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 |
p = fork (); |
|---|
| 480 |
if (setjmp (msgenv)) |
|---|
| 481 |
{ |
|---|
| 482 |
msgctl (s, IPC_RMID, 0); |
|---|
| 483 |
if (p > 0) |
|---|
| 484 |
kill (p, SIGKILL); |
|---|
| 485 |
exit (0); |
|---|
| 486 |
} |
|---|
| 487 |
signal (SIGTERM, msgcatch); |
|---|
| 488 |
signal (SIGINT, msgcatch); |
|---|
| 489 |
signal (SIGHUP, msgcatch); |
|---|
| 490 |
if (p > 0) |
|---|
| 491 |
{ |
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
p = getppid (); |
|---|
| 495 |
setpgrp (); |
|---|
| 496 |
while (1) |
|---|
| 497 |
{ |
|---|
| 498 |
|
|---|
| 499 |
if (kill (p, 0) < 0) |
|---|
| 500 |
{ |
|---|
| 501 |
msgctl (s, IPC_RMID, 0); |
|---|
| 502 |
exit (0); |
|---|
| 503 |
} |
|---|
| 504 |
sleep (10); |
|---|
| 505 |
} |
|---|
| 506 |
} |
|---|
| 507 |
|
|---|
| 508 |
|
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
ioproc = fork (); |
|---|
| 512 |
if (ioproc == 0) |
|---|
| 513 |
{ |
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
while (fgets (msgp->mtext, BUFSIZ, stdin)) |
|---|
| 518 |
{ |
|---|
| 519 |
msgp->mtype = 1; |
|---|
| 520 |
msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0); |
|---|
| 521 |
} |
|---|
| 522 |
exit (1); |
|---|
| 523 |
} |
|---|
| 524 |
|
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
while (1) |
|---|
| 528 |
{ |
|---|
| 529 |
if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0) |
|---|
| 530 |
{ |
|---|
| 531 |
#ifdef EINTR |
|---|
| 532 |
if (errno == EINTR) |
|---|
| 533 |
continue; |
|---|
| 534 |
#endif |
|---|
| 535 |
perror_1 ("msgrcv"); |
|---|
| 536 |
exit (1); |
|---|
| 537 |
} |
|---|
| 538 |
else |
|---|
| 539 |
{ |
|---|
| 540 |
msgctl (s, IPC_STAT, &msg_st); |
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
if (msg_st.msg_lspid == ioproc) |
|---|
| 545 |
{ |
|---|
| 546 |
char code[BUFSIZ]; |
|---|
| 547 |
int inproc; |
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
msgp->mtext[strlen (msgp->mtext)-1] = 0; |
|---|
| 551 |
sscanf (msgp->mtext, "%s %d", code, &inproc); |
|---|
| 552 |
msgp->mtype = inproc; |
|---|
| 553 |
msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0); |
|---|
| 554 |
continue; |
|---|
| 555 |
} |
|---|
| 556 |
|
|---|
| 557 |
|
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
strncpy (string, msgp->mtext, fromlen); |
|---|
| 561 |
string[fromlen] = 0; |
|---|
| 562 |
|
|---|
| 563 |
printf ("Client: %d %s", msg_st.msg_lspid, string); |
|---|
| 564 |
fflush (stdout); |
|---|
| 565 |
} |
|---|
| 566 |
} |
|---|
| 567 |
} |
|---|
| 568 |
|
|---|
| 569 |
#endif |
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
|
|---|
| 573 |
|
|---|
| 574 |
void |
|---|
| 575 |
perror_1 (string) |
|---|
| 576 |
char *string; |
|---|
| 577 |
{ |
|---|
| 578 |
char *copy = (char *) malloc (strlen (string) + 8); |
|---|
| 579 |
if (copy == 0) |
|---|
| 580 |
fatal_error ("Virtual memory exhausted"); |
|---|
| 581 |
|
|---|
| 582 |
strcpy (copy, "Error: "); |
|---|
| 583 |
strcat (copy, string); |
|---|
| 584 |
perror (copy); |
|---|
| 585 |
} |
|---|
| 586 |
|
|---|
| 587 |
void |
|---|
| 588 |
fatal_error (string) |
|---|
| 589 |
char *string; |
|---|
| 590 |
{ |
|---|
| 591 |
fprintf (stderr, "%s", "Error: "); |
|---|
| 592 |
fprintf (stderr, string); |
|---|
| 593 |
exit (1); |
|---|
| 594 |
} |
|---|
| 595 |
#endif |
|---|