Blame sock.c

Packit a94d48
/*
Packit a94d48
 *  sock.c - ACPI daemon socket interface
Packit a94d48
 *
Packit a94d48
 *  Portions Copyright (C) 2000 Andrew Henroid
Packit a94d48
 *  Portions Copyright (C) 2001 Sun Microsystems
Packit a94d48
 *  Portions Copyright (C) 2004 Tim Hockin (thockin@hockin.org)
Packit a94d48
 *
Packit a94d48
 *  This program is free software; you can redistribute it and/or modify
Packit a94d48
 *  it under the terms of the GNU General Public License as published by
Packit a94d48
 *  the Free Software Foundation; either version 2 of the License, or
Packit a94d48
 *  (at your option) any later version.
Packit a94d48
 *
Packit a94d48
 *  This program is distributed in the hope that it will be useful,
Packit a94d48
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a94d48
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit a94d48
 *  GNU General Public License for more details.
Packit a94d48
 *
Packit a94d48
 *  You should have received a copy of the GNU General Public License
Packit a94d48
 *  along with this program; if not, write to the Free Software
Packit a94d48
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit a94d48
 */
Packit a94d48
Packit a94d48
#ifdef HAVE_CONFIG_H
Packit a94d48
#include <config.h>
Packit a94d48
#endif
Packit a94d48
Packit a94d48
#include <unistd.h>
Packit a94d48
#include <sys/types.h>
Packit a94d48
#include <sys/stat.h>
Packit a94d48
#include <fcntl.h>
Packit a94d48
#include <stdio.h>
Packit a94d48
#include <stdlib.h>
Packit a94d48
#include <string.h>
Packit a94d48
#include <errno.h>
Packit a94d48
#include <grp.h>
Packit a94d48
Packit a94d48
#include "acpid.h"
Packit a94d48
#include "log.h"
Packit a94d48
#include "event.h"
Packit a94d48
#include "ud_socket.h"
Packit a94d48
#include "connection_list.h"
Packit a94d48
Packit a94d48
#include "sock.h"
Packit a94d48
Packit a94d48
const char *socketfile = ACPID_SOCKETFILE;
Packit a94d48
const char *socketgroup;
Packit a94d48
mode_t socketmode = ACPID_SOCKETMODE;
Packit a94d48
int clientmax = ACPID_CLIENTMAX;
Packit a94d48
Packit a94d48
/* the number of non-root clients that are connected */
Packit a94d48
int non_root_clients;
Packit a94d48
Packit a94d48
#ifndef HAVE_ISFDTYPE
Packit a94d48
static int
Packit a94d48
isfdtype(int fd, int fdtype)
Packit a94d48
{
Packit a94d48
	struct stat64 st;
Packit a94d48
	if (fstat64(fd, &st) != 0)
Packit a94d48
		return -1;
Packit a94d48
	return ((st.st_mode & S_IFMT) == (mode_t)fdtype);
Packit a94d48
}
Packit a94d48
#endif
Packit a94d48
Packit a94d48
/* determine if a file descriptor is in fact a socket */
Packit a94d48
int
Packit a94d48
is_socket(int fd)
Packit a94d48
{
Packit a94d48
    return (isfdtype(fd, S_IFSOCK) == 1);
Packit a94d48
}
Packit a94d48
Packit a94d48
/* accept a new client connection */
Packit a94d48
static void
Packit a94d48
process_sock(int fd)
Packit a94d48
{
Packit a94d48
	int cli_fd;
Packit a94d48
	struct ucred creds;
Packit a94d48
	char *buf;
Packit a94d48
	static int accept_errors;
Packit a94d48
Packit a94d48
	/* accept and add to our lists */
Packit a94d48
	cli_fd = ud_accept(fd, &creds);
Packit a94d48
	if (cli_fd < 0) {
Packit a94d48
		acpid_log(LOG_ERR, "can't accept client: %s",
Packit a94d48
			  strerror(errno));
Packit a94d48
		accept_errors++;
Packit a94d48
		if (accept_errors >= 5) {
Packit a94d48
			acpid_log(LOG_ERR, "giving up");
Packit a94d48
			clean_exit_with_status(EXIT_FAILURE);
Packit a94d48
		}
Packit a94d48
		return;
Packit a94d48
	}
Packit a94d48
	accept_errors = 0;
Packit a94d48
Packit a94d48
	/* don't allow too many non-root clients  */
Packit a94d48
	if (creds.uid != 0 && non_root_clients >= clientmax) {
Packit a94d48
		close(cli_fd);
Packit a94d48
		acpid_log(LOG_ERR, "too many non-root clients");
Packit a94d48
		return;
Packit a94d48
	}
Packit a94d48
	if (creds.uid != 0) {
Packit a94d48
		non_root_clients++;
Packit a94d48
	}
Packit a94d48
Packit a94d48
    if(asprintf(&buf, "%d[%d:%d]", creds.pid, creds.uid, creds.gid) < 0) {
Packit a94d48
        close(cli_fd);
Packit a94d48
        acpid_log(LOG_ERR, "asprintf: %s", strerror(errno));
Packit a94d48
        return;
Packit a94d48
     }
Packit a94d48
        acpid_add_client(cli_fd, buf);
Packit a94d48
        free(buf);
Packit a94d48
}
Packit a94d48
Packit a94d48
/* set up the socket for client connections */
Packit a94d48
void
Packit a94d48
open_sock()
Packit a94d48
{
Packit a94d48
	int fd;
Packit a94d48
	struct connection c;
Packit a94d48
Packit a94d48
	/* if this is a socket passed in via stdin by systemd */
Packit a94d48
	if (is_socket(STDIN_FILENO)) {
Packit a94d48
		fd = STDIN_FILENO;
Packit a94d48
		/* ??? Move CLOEXEC and NONBLOCK settings below up to here? */
Packit a94d48
	} else {
Packit a94d48
		/* create our own socket */
Packit a94d48
		fd = ud_create_socket(socketfile, socketmode);
Packit a94d48
		if (fd < 0) {
Packit a94d48
			acpid_log(LOG_ERR, "can't open socket %s: %s",
Packit a94d48
				socketfile, strerror(errno));
Packit a94d48
			exit(EXIT_FAILURE);
Packit a94d48
		}
Packit a94d48
Packit a94d48
		/* if we need to change the socket's group, do so */
Packit a94d48
		if (socketgroup) {
Packit a94d48
			struct group *gr;
Packit a94d48
			struct stat buf;
Packit a94d48
Packit a94d48
		    gr = getgrnam(socketgroup);
Packit a94d48
			if (!gr) {
Packit a94d48
				acpid_log(LOG_ERR, "group %s does not exist", socketgroup);
Packit a94d48
				exit(EXIT_FAILURE);
Packit a94d48
			}
Packit a94d48
			if (fstat(fd, &buf) < 0) {
Packit a94d48
				acpid_log(LOG_ERR, "can't stat %s: %s", 
Packit a94d48
		            socketfile, strerror(errno));
Packit a94d48
				exit(EXIT_FAILURE);
Packit a94d48
			}
Packit a94d48
			/* ??? I've tried using fchown(), however it doesn't work here.
Packit a94d48
			 *     It also doesn't work before bind().  The GNU docs seem to
Packit a94d48
			 *     indicate it isn't supposed to work with sockets. */
Packit a94d48
			/* if (fchown(fd, buf.st_uid, gr->gr_gid) < 0) { */
Packit a94d48
			if (chown(socketfile, buf.st_uid, gr->gr_gid) < 0) {
Packit a94d48
				acpid_log(LOG_ERR, "can't chown %s: %s", 
Packit a94d48
		            socketfile, strerror(errno));
Packit a94d48
				exit(EXIT_FAILURE);
Packit a94d48
			}
Packit a94d48
		}
Packit a94d48
	}
Packit a94d48
Packit a94d48
	/* Don't leak fds when execing.
Packit a94d48
	 * ud_create_socket() already does this, but there is no guarantee that
Packit a94d48
	 * a socket sent in via STDIN will have this set. */
Packit a94d48
	if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
Packit a94d48
		close(fd);
Packit a94d48
		acpid_log(LOG_ERR, "fcntl() on socket %s for FD_CLOEXEC: %s", 
Packit a94d48
		          socketfile, strerror(errno));
Packit a94d48
		return;
Packit a94d48
	}
Packit a94d48
Packit a94d48
	/* Avoid a potential hang.
Packit a94d48
	 * ud_create_socket() already does this, but there is no guarantee that
Packit a94d48
	 * a socket sent in via STDIN will have this set. */
Packit a94d48
	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
Packit a94d48
		close(fd);
Packit a94d48
		acpid_log(LOG_ERR, "fcntl() on socket %s for O_NONBLOCK: %s", 
Packit a94d48
		          socketfile, strerror(errno));
Packit a94d48
		return;
Packit a94d48
	}
Packit a94d48
	
Packit a94d48
	/* add a connection to the list */
Packit a94d48
	c.fd = fd;
Packit a94d48
	c.process = process_sock;
Packit a94d48
	c.pathname = NULL;
Packit a94d48
	c.kybd = 0;
Packit a94d48
Packit a94d48
	if (add_connection(&c) < 0) {
Packit a94d48
		close(fd);
Packit a94d48
		acpid_log(LOG_ERR, "can't add connection for socket %s",
Packit a94d48
		          socketfile);
Packit a94d48
		return;
Packit a94d48
	}
Packit a94d48
}