| 1 | /* Communication subprocess for GNU Emacs acting as server. |
| 2 | Copyright (C) 1986, 1987, 1992, 1994, 1999 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Emacs. |
| 5 | |
| 6 | GNU Emacs is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GNU Emacs is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Emacs; 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 | |
| 22 | /* The GNU Emacs edit server process is run as a subprocess of Emacs |
| 23 | under control of the file lisp/server.el. |
| 24 | This program accepts communication from client (program emacsclient.c) |
| 25 | and passes their commands (consisting of keyboard characters) |
| 26 | up to the Emacs which then executes them. */ |
| 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 /* HAVE_SOCKETS or HAVE_SYSVIPC */ |
| 45 | |
| 46 | void perror_1 (); |
| 47 | void fatal_error (); |
| 48 | |
| 49 | #if defined (HAVE_SOCKETS) && ! defined (NO_SOCKETS_IN_FILE_SYSTEM) |
| 50 | /* BSD code is very different from SYSV IPC code */ |
| 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 | /* Copied from src/process.c */ |
| 69 | #ifdef FD_SET |
| 70 | /* We could get this from param.h, but better not to depend on finding that. |
| 71 | And better not to risk that it might define other symbols used in this |
| 72 | file. */ |
| 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 /* no FD_SET */ |
| 80 | #define MAXDESC 32 |
| 81 | #define SELECT_TYPE int |
| 82 | |
| 83 | /* Define the macros to access a single-int bitmap of descriptors. */ |
| 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 /* no FD_SET */ |
| 89 | |
| 90 | /* This is the file name of the socket that we made. */ |
| 91 | |
| 92 | char *socket_name; |
| 93 | |
| 94 | /* Name of this program. */ |
| 95 | |
| 96 | char *progname; |
| 97 | \f |
| 98 | /* Handle fatal signals. */ |
| 99 | |
| 100 | /* This is the handler. */ |
| 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 | /* Set up to handle all the signals. */ |
| 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 | /* This is missing on some systems - OS/2, for example. */ |
| 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 /* SIGXFSZ */ |
| 164 | |
| 165 | #ifdef AIX |
| 166 | /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */ |
| 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 /* AIX */ |
| 176 | } |
| 177 | \f |
| 178 | /* Print error message. `s1' is printf control string, `s2' is arg for it. */ |
| 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 | /* Print error message and exit. */ |
| 189 | void |
| 190 | fatal (s1, s2) |
| 191 | char *s1, *s2; |
| 192 | { |
| 193 | error (s1, s2); |
| 194 | exit (1); |
| 195 | } |
| 196 | |
| 197 | /* Like malloc but get fatal error if memory is exhausted. */ |
| 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 | \f |
| 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 | char *homedir; |
| 224 | char *str, string[BUFSIZ], code[BUFSIZ]; |
| 225 | FILE *infile; |
| 226 | FILE **openfiles; |
| 227 | int openfiles_size; |
| 228 | struct stat statbuf; |
| 229 | |
| 230 | #ifndef convex |
| 231 | char *getenv (); |
| 232 | #endif |
| 233 | |
| 234 | progname = argv[0]; |
| 235 | |
| 236 | openfiles_size = 20; |
| 237 | openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *)); |
| 238 | if (openfiles == 0) |
| 239 | abort (); |
| 240 | |
| 241 | /* |
| 242 | * Open up an AF_UNIX socket in this person's home directory |
| 243 | */ |
| 244 | |
| 245 | if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
| 246 | { |
| 247 | perror_1 ("socket"); |
| 248 | exit (1); |
| 249 | } |
| 250 | server.sun_family = AF_UNIX; |
| 251 | |
| 252 | system_name_length = 32; |
| 253 | while (1) |
| 254 | { |
| 255 | system_name = (char *) xmalloc (system_name_length + 1); |
| 256 | |
| 257 | /* system_name must be null-terminated string. */ |
| 258 | system_name[system_name_length] = '\0'; |
| 259 | |
| 260 | if (gethostname (system_name, system_name_length) == 0) |
| 261 | break; |
| 262 | |
| 263 | free (system_name); |
| 264 | system_name_length *= 2; |
| 265 | } |
| 266 | |
| 267 | #ifndef SERVER_HOME_DIR |
| 268 | sprintf (server.sun_path, "/tmp/esrv%d-%s", geteuid (), system_name); |
| 269 | |
| 270 | if (unlink (server.sun_path) == -1 && errno != ENOENT) |
| 271 | { |
| 272 | perror_1 ("unlink"); |
| 273 | exit (1); |
| 274 | } |
| 275 | #else |
| 276 | if ((homedir = getenv ("HOME")) == NULL) |
| 277 | fatal_error ("No home directory\n"); |
| 278 | |
| 279 | strcpy (server.sun_path, homedir); |
| 280 | strcat (server.sun_path, "/.emacs-server-"); |
| 281 | strcat (server.sun_path, system_name); |
| 282 | /* Delete anyone else's old server. */ |
| 283 | unlink (server.sun_path); |
| 284 | #endif |
| 285 | |
| 286 | /* Save the socket name so we can delete it. */ |
| 287 | socket_name = (char *) xmalloc (strlen (server.sun_path) + 1); |
| 288 | strcpy (socket_name, server.sun_path); |
| 289 | |
| 290 | handle_signals (); |
| 291 | |
| 292 | if (bind (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) < 0) |
| 293 | { |
| 294 | perror_1 ("bind"); |
| 295 | exit (1); |
| 296 | } |
| 297 | /* Only this user can send commands to this Emacs. */ |
| 298 | if (stat (server.sun_path, &statbuf) < 0) |
| 299 | { |
| 300 | perror_1 ("bind"); |
| 301 | exit (1); |
| 302 | } |
| 303 | |
| 304 | chmod (server.sun_path, statbuf.st_mode & 0600); |
| 305 | /* |
| 306 | * Now, just wait for everything to come in.. |
| 307 | */ |
| 308 | if (listen (s, 5) < 0) |
| 309 | { |
| 310 | perror_1 ("listen"); |
| 311 | exit (1); |
| 312 | } |
| 313 | |
| 314 | /* Disable sigpipes in case luser kills client... */ |
| 315 | signal (SIGPIPE, SIG_IGN); |
| 316 | for (;;) |
| 317 | { |
| 318 | SELECT_TYPE rmask; |
| 319 | FD_ZERO (&rmask); |
| 320 | FD_SET (0, &rmask); |
| 321 | FD_SET (s, &rmask); |
| 322 | if (select (s + 1, &rmask, 0, 0, 0) < 0) |
| 323 | perror_1 ("select"); |
| 324 | if (FD_ISSET (s, &rmask)) /* client sends list of filenames */ |
| 325 | { |
| 326 | fromlen = sizeof (fromunix); |
| 327 | fromunix.sun_family = AF_UNIX; |
| 328 | infd = accept (s, (struct sockaddr *) &fromunix, &fromlen); |
| 329 | if (infd < 0) |
| 330 | { |
| 331 | if (errno == EMFILE || errno == ENFILE) |
| 332 | fprintf (stderr, "Error: too many clients.\n"); |
| 333 | else |
| 334 | perror_1 ("accept"); |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | if (infd >= openfiles_size) |
| 339 | { |
| 340 | openfiles_size *= 2; |
| 341 | openfiles = (FILE **) realloc (openfiles, |
| 342 | openfiles_size * sizeof (FILE *)); |
| 343 | if (openfiles == 0) |
| 344 | abort (); |
| 345 | } |
| 346 | |
| 347 | infile = fdopen (infd, "r+"); /* open stream */ |
| 348 | if (infile == NULL) |
| 349 | { |
| 350 | fprintf (stderr, "Error: too many clients.\n"); |
| 351 | write (infd, "Too many clients.\n", 18); |
| 352 | close (infd); /* Prevent descriptor leak.. */ |
| 353 | continue; |
| 354 | } |
| 355 | str = fgets (string, BUFSIZ, infile); |
| 356 | if (str == NULL) |
| 357 | { |
| 358 | perror_1 ("fgets"); |
| 359 | close (infd); /* Prevent descriptor leak.. */ |
| 360 | continue; |
| 361 | } |
| 362 | openfiles[infd] = infile; |
| 363 | printf ("Client: %d %s", infd, string); |
| 364 | /* If what we read did not end in a newline, |
| 365 | it means there is more. Keep reading from the socket |
| 366 | and outputting to Emacs, until we get the newline. */ |
| 367 | while (string[strlen (string) - 1] != '\n') |
| 368 | { |
| 369 | if (fgets (string, BUFSIZ, infile) == 0) |
| 370 | break; |
| 371 | printf ("%s", string); |
| 372 | } |
| 373 | fflush (stdout); |
| 374 | fflush (infile); |
| 375 | continue; |
| 376 | } |
| 377 | else if (FD_ISSET (0, &rmask)) /* emacs sends codeword, fd, and string message */ |
| 378 | { |
| 379 | /* Read command codeword and fd */ |
| 380 | clearerr (stdin); |
| 381 | scanf ("%s %d%*c", code, &infd); |
| 382 | if (ferror (stdin) || feof (stdin)) |
| 383 | fatal_error ("server: error reading from standard input\n"); |
| 384 | |
| 385 | /* Transfer text from Emacs to the client, up to a newline. */ |
| 386 | infile = openfiles[infd]; |
| 387 | rewind (infile); |
| 388 | while (1) |
| 389 | { |
| 390 | if (fgets (string, BUFSIZ, stdin) == 0) |
| 391 | break; |
| 392 | fprintf (infile, "%s", string); |
| 393 | if (string[strlen (string) - 1] == '\n') |
| 394 | break; |
| 395 | } |
| 396 | fflush (infile); |
| 397 | |
| 398 | /* If command is close, close connection to client. */ |
| 399 | if (strncmp (code, "Close:", 6) == 0) |
| 400 | if (infd > 2) |
| 401 | { |
| 402 | fclose (infile); |
| 403 | close (infd); |
| 404 | } |
| 405 | continue; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | #else /* This is the SYSV IPC section */ |
| 411 | |
| 412 | #include <sys/types.h> |
| 413 | #include <sys/ipc.h> |
| 414 | #include <sys/msg.h> |
| 415 | #include <setjmp.h> |
| 416 | #include <errno.h> |
| 417 | #include <sys/utsname.h> |
| 418 | |
| 419 | struct utsname system_name; |
| 420 | |
| 421 | #ifndef errno |
| 422 | extern int errno; |
| 423 | #endif |
| 424 | |
| 425 | jmp_buf msgenv; |
| 426 | |
| 427 | SIGTYPE |
| 428 | msgcatch () |
| 429 | { |
| 430 | longjmp (msgenv, 1); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /* "THIS has to be fixed. Remember, stderr may not exist...-rlk." |
| 435 | Incorrect. This program runs as an inferior of Emacs. |
| 436 | Its stderr always exists--rms. */ |
| 437 | #include <stdio.h> |
| 438 | |
| 439 | int |
| 440 | main () |
| 441 | { |
| 442 | int s, infd, fromlen, ioproc; |
| 443 | key_t key; |
| 444 | struct msgbuf * msgp = |
| 445 | (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ); |
| 446 | struct msqid_ds msg_st; |
| 447 | int p; |
| 448 | char *homedir, *getenv (); |
| 449 | char string[BUFSIZ]; |
| 450 | FILE *infile; |
| 451 | |
| 452 | /* |
| 453 | * Create a message queue using ~/.emacs-server as the path for ftok |
| 454 | */ |
| 455 | if ((homedir = getenv ("HOME")) == NULL) |
| 456 | fatal_error ("No home directory\n"); |
| 457 | |
| 458 | strcpy (string, homedir); |
| 459 | #ifndef HAVE_LONG_FILE_NAMES |
| 460 | /* If file names are short, we can't fit the host name. */ |
| 461 | strcat (string, "/.emacs-server"); |
| 462 | #else |
| 463 | strcat (string, "/.emacs-server-"); |
| 464 | uname (&system_name); |
| 465 | strcat (string, system_name.nodename); |
| 466 | #endif |
| 467 | creat (string, 0600); |
| 468 | key = ftok (string, 1); /* unlikely to be anyone else using it */ |
| 469 | s = msgget (key, 0600 | IPC_CREAT); |
| 470 | if (s == -1) |
| 471 | { |
| 472 | perror_1 ("msgget"); |
| 473 | exit (1); |
| 474 | } |
| 475 | |
| 476 | /* Fork so we can close connection even if parent dies */ |
| 477 | p = fork (); |
| 478 | if (setjmp (msgenv)) |
| 479 | { |
| 480 | msgctl (s, IPC_RMID, 0); |
| 481 | if (p > 0) |
| 482 | kill (p, SIGKILL); |
| 483 | exit (0); |
| 484 | } |
| 485 | signal (SIGTERM, msgcatch); |
| 486 | signal (SIGINT, msgcatch); |
| 487 | signal (SIGHUP, msgcatch); |
| 488 | if (p > 0) |
| 489 | { |
| 490 | /* This is executed in the original process that did the fork above. */ |
| 491 | /* Get pid of Emacs itself. */ |
| 492 | p = getppid (); |
| 493 | setpgrp (); /* Gnu kills process group on exit */ |
| 494 | while (1) |
| 495 | { |
| 496 | /* Is Emacs still alive? */ |
| 497 | if (kill (p, 0) < 0) |
| 498 | { |
| 499 | msgctl (s, IPC_RMID, 0); |
| 500 | exit (0); |
| 501 | } |
| 502 | sleep (10); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* This is executed in the child made by forking above. |
| 507 | Call it c1. Make another process, ioproc. */ |
| 508 | |
| 509 | ioproc = fork (); |
| 510 | if (ioproc == 0) |
| 511 | { |
| 512 | /* In process ioproc, wait for text from Emacs, |
| 513 | and send it to the process c1. |
| 514 | This way, c1 only has to wait for one source of input. */ |
| 515 | while (fgets (msgp->mtext, BUFSIZ, stdin)) |
| 516 | { |
| 517 | msgp->mtype = 1; |
| 518 | msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0); |
| 519 | } |
| 520 | exit (1); |
| 521 | } |
| 522 | |
| 523 | /* In the process c1, |
| 524 | listen for messages from clients and pass them to Emacs. */ |
| 525 | while (1) |
| 526 | { |
| 527 | if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0) |
| 528 | { |
| 529 | #ifdef EINTR |
| 530 | if (errno == EINTR) |
| 531 | continue; |
| 532 | #endif |
| 533 | perror_1 ("msgrcv"); |
| 534 | exit (1); |
| 535 | } |
| 536 | else |
| 537 | { |
| 538 | msgctl (s, IPC_STAT, &msg_st); |
| 539 | |
| 540 | /* Distinguish whether the message came from a client, or from |
| 541 | ioproc. */ |
| 542 | if (msg_st.msg_lspid == ioproc) |
| 543 | { |
| 544 | char code[BUFSIZ]; |
| 545 | int inproc; |
| 546 | |
| 547 | /* Message from ioproc: tell a client we are done. */ |
| 548 | msgp->mtext[strlen (msgp->mtext)-1] = 0; |
| 549 | sscanf (msgp->mtext, "%s %d", code, &inproc); |
| 550 | msgp->mtype = inproc; |
| 551 | msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0); |
| 552 | continue; |
| 553 | } |
| 554 | |
| 555 | /* This is a request from a client: copy to stdout |
| 556 | so that Emacs will get it. Include msg_lspid |
| 557 | so server.el can tell us where to send the reply. */ |
| 558 | strncpy (string, msgp->mtext, fromlen); |
| 559 | string[fromlen] = 0; /* make sure */ |
| 560 | /* Newline is part of string.. */ |
| 561 | printf ("Client: %d %s", msg_st.msg_lspid, string); |
| 562 | fflush (stdout); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | #endif /* HAVE_SYSVIPC */ |
| 568 | |
| 569 | \f |
| 570 | /* This is like perror but puts `Error: ' at the beginning. */ |
| 571 | |
| 572 | void |
| 573 | perror_1 (string) |
| 574 | char *string; |
| 575 | { |
| 576 | char *copy = (char *) malloc (strlen (string) + 8); |
| 577 | if (copy == 0) |
| 578 | fatal_error ("Virtual memory exhausted"); |
| 579 | |
| 580 | strcpy (copy, "Error: "); |
| 581 | strcat (copy, string); |
| 582 | perror (copy); |
| 583 | } |
| 584 | |
| 585 | void |
| 586 | fatal_error (string) |
| 587 | char *string; |
| 588 | { |
| 589 | fprintf (stderr, "%s", "Error: "); |
| 590 | fprintf (stderr, string); |
| 591 | exit (1); |
| 592 | } |
| 593 | #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */ |