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