extern int MoreUNIXIgnoreSIGPIPE(void) // See comment in header. { int err; struct sigaction signalState; err = sigaction(SIGPIPE, NULL, &signalState); err = MoreUNIXErrno(err); if (err == 0) { signalState.sa_handler = SIG_IGN; err = sigaction(SIGPIPE, &signalState, NULL); err = MoreUNIXErrno(err); } return err; }
static int ConnectionOpen(ConnectionRef *connPtr) // Opens a connection to the server. // // On entry, connPtr must not be NULL // On entry, *connPtr must be NULL // Returns an errno-style error code // On success, *connPtr will not be NULL // On error, *connPtr will be NULL { int err; ConnectionRef conn; Boolean sayGoodbye; assert( connPtr != NULL); assert(*connPtr == NULL); sayGoodbye = false; // Allocate a ConnectionState structure and fill out some basic fields. err = 0; conn = (ConnectionRef) calloc(1, sizeof(*conn)); if (conn == NULL) { err = ENOMEM; } if (err == 0) { conn->fMagic = kConnectionStateMagic; // For clean up to work properly, we must make sure that, if // the connection record is allocated successfully, we always // set fSockFD to -1. So, while the following line is redundant // in the current code, it's present to press home this point. conn->fSockFD = -1; } // Create a UNIX domain socket and connect to the server. if (err == 0) { conn->fSockFD = socket(AF_UNIX, SOCK_STREAM, 0); err = MoreUNIXErrno(conn->fSockFD); } if (err == 0) { struct sockaddr_un connReq; connReq.sun_len = sizeof(connReq); connReq.sun_family = AF_UNIX; strcpy(connReq.sun_path, kServerSocketPath); err = connect(conn->fSockFD, (struct sockaddr *) &connReq, SUN_LEN(&connReq)); err = MoreUNIXErrno(err); sayGoodbye = (err == 0); } // Clean up. if (err != 0) { ConnectionCloseInternal(conn, sayGoodbye); conn = NULL; } *connPtr = conn; assert( (err == 0) == (*connPtr != NULL) ); return err; }