/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "pg_config.h" #include "socket.h" #include #include #include #include #include #include int connect_unix(const char *address) { struct sockaddr_un saddr; int sfd; DBG(syslog(LOG_DEBUG, "opening unix domain socket '%s'", address)); strncpy(saddr.sun_path, address, sizeof(saddr.sun_path)); saddr.sun_family = AF_UNIX; sfd = socket(AF_UNIX, SOCK_STREAM, 0); if(sfd < 0) { syslog(LOG_ERR, "socket: %m"); return FAIL; } #if defined(hpux) || defined(ultrix) || defined(sun) if(connect(sfd, (struct sockaddr*)&saddr, sizeof(saddr.sun_family) + strlen(saddr.sun_path)) < 0 ) #else if(connect(sfd, (struct sockaddr*)&saddr, sizeof(saddr.sun_len) + sizeof(saddr.sun_family) + strlen(saddr.sun_path)) < 0 ) #endif { syslog(LOG_ERR, "connect: %m"); close(sfd); return FAIL; } return sfd; } result shutdown_unix(int sfd) { if( close(sfd) != SUCCESS ) { syslog(LOG_ERR, "close: %m"); return FAIL; } return SUCCESS; } int bind_unix(const char *address) { int sfd; struct sockaddr_un saddr; DBG(syslog(LOG_DEBUG, "opening unix domain socket '%s'", address)); strncpy(saddr.sun_path, address, sizeof(saddr.sun_path)); saddr.sun_family = AF_UNIX; sfd = socket(AF_UNIX, SOCK_STREAM, 0); if(sfd < 0) { syslog(LOG_ERR, "socket: %m"); goto fail; } #if defined(hpux) || defined(ultrix) || defined(sun) if(bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr.sun_family) + strlen(saddr.sun_path)) < 0 ) #else if(bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr.sun_len) + sizeof(saddr.sun_family) + strlen(saddr.sun_path)) < 0 ) #endif { syslog(LOG_ERR, "bind: %m"); close(sfd); goto fail; } return sfd; fail: return FAIL; } int accept_unix(int sfd) { int cfd; struct sockaddr_un saddr; int len = sizeof(struct sockaddr_un); cfd = accept(sfd, (struct sockaddr *)&saddr, &len); if(cfd == FAIL) { syslog(LOG_ERR, "accept: %m"); goto fail; } return cfd; fail: return FAIL; } result peername_unix(int s, peername_t peer) { char host[MAXHOSTNAMELEN]; if(gethostname(host, MAXHOSTNAMELEN) == FAIL) { syslog(LOG_ERR, "gethostname: %m"); return FAIL; } DBG(syslog(LOG_DEBUG, "hostname: `%s'", host)); strncpy(peer, gethostbyname(host)->h_name, PEERNAME_SIZE); return SUCCESS; }