mirror of
https://github.com/troglobit/finit.git
synced 2026-10-01 13:33:09 +07:00
233 lines
4.6 KiB
C
233 lines
4.6 KiB
C
/* External client API, using UNIX domain socket.
|
|
*
|
|
* Copyright (c) 2015-2023 Joachim Wiberg <troglobit@gmail.com>
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include <poll.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
|
|
#include "client.h"
|
|
#include "log.h"
|
|
|
|
static int sd = -1;
|
|
|
|
int client_connect(void)
|
|
{
|
|
struct sockaddr_un sun = {
|
|
.sun_family = AF_UNIX,
|
|
.sun_path = INIT_SOCKET,
|
|
};
|
|
|
|
sd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
|
|
if (-1 == sd) {
|
|
warn("Failed creating UNIX domain socket");
|
|
return -1;
|
|
}
|
|
|
|
if (connect(sd, (struct sockaddr*)&sun, sizeof(sun)) == -1) {
|
|
if (errno != ENOENT)
|
|
warnx("Failed connecting to finit");
|
|
close(sd);
|
|
sd = -1;
|
|
}
|
|
|
|
return sd;
|
|
}
|
|
|
|
int client_disconnect(void)
|
|
{
|
|
int rc;
|
|
|
|
if (sd < 0) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
rc = close(sd);
|
|
sd = -1;
|
|
|
|
return rc;
|
|
}
|
|
|
|
/* Only valid after client_connect() */
|
|
int client_socket(void)
|
|
{
|
|
return sd;
|
|
}
|
|
|
|
int client_request(struct init_request *rq, ssize_t len)
|
|
{
|
|
struct pollfd pfd = { 0 };
|
|
int rc;
|
|
|
|
if (client_connect() == -1)
|
|
return -1;
|
|
|
|
pfd.fd = sd;
|
|
pfd.events = POLLOUT;
|
|
if (poll(&pfd, 1, 2000) <= 0) {
|
|
warn("Timed out waiting for Finit, errno %d", errno);
|
|
return -1;
|
|
}
|
|
|
|
if (write(sd, rq, len) != len) {
|
|
warn("Failed communicating with Finit, errno %d", errno);
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
* Exception, only command that don't need to wait for a
|
|
* response from Finit. Registers the client socket as a
|
|
* readiness notification socket with Finit
|
|
*/
|
|
if (rq->cmd == INIT_CMD_NOTIFY_SOCKET) {
|
|
rq->cmd = INIT_CMD_ACK;
|
|
return 0;
|
|
}
|
|
|
|
pfd.fd = sd;
|
|
pfd.events = POLLIN | POLLERR | POLLHUP;
|
|
if ((rc = poll(&pfd, 1, 2000)) <= 0) {
|
|
if (rc) {
|
|
if (errno == EINTR) /* shutdown/reboot */
|
|
return -1;
|
|
warn("poll(), errno %d", errno);
|
|
} else
|
|
warnx("Timed out waiting for reply from Finit.");
|
|
return -1;
|
|
}
|
|
|
|
if (read(sd, rq, len) != len) {
|
|
warn("Failed reading reply from Finit, errno %d", errno);
|
|
return -1;
|
|
}
|
|
|
|
if (rq->cmd == INIT_CMD_NACK)
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int client_send(struct init_request *rq, ssize_t len)
|
|
{
|
|
int rc = 0;
|
|
|
|
if (client_request(rq, len))
|
|
rc = 255;
|
|
|
|
client_disconnect();
|
|
|
|
return rc;
|
|
}
|
|
|
|
int client_command(int cmd)
|
|
{
|
|
struct init_request rq = {
|
|
.magic = INIT_MAGIC,
|
|
.runlevel = getpid(),
|
|
.cmd = cmd,
|
|
};
|
|
|
|
return client_request(&rq, sizeof(rq));
|
|
}
|
|
|
|
svc_t *client_svc_iterator(int first)
|
|
{
|
|
struct init_request rq = {
|
|
.magic = INIT_MAGIC,
|
|
.cmd = INIT_CMD_SVC_ITER,
|
|
};
|
|
static svc_t svc;
|
|
|
|
if (client_connect() == -1)
|
|
return NULL;
|
|
|
|
if (first)
|
|
rq.runlevel = 1;
|
|
else
|
|
rq.runlevel = 0;
|
|
|
|
if (write(sd, &rq, sizeof(rq)) != sizeof(rq))
|
|
goto error;
|
|
if (read(sd, &svc, sizeof(svc)) != sizeof(svc))
|
|
goto error;
|
|
|
|
client_disconnect();
|
|
if (svc.pid < 0)
|
|
return NULL;
|
|
|
|
return &svc;
|
|
error:
|
|
warn("Failed communicating with finit, error %d", errno);
|
|
client_disconnect();
|
|
sd = -1;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static svc_t *do_find(int cmd, const char *arg)
|
|
{
|
|
struct init_request rq = {
|
|
.magic = INIT_MAGIC,
|
|
.cmd = cmd,
|
|
};
|
|
static svc_t svc;
|
|
|
|
if (client_connect() == -1)
|
|
return NULL;
|
|
|
|
strlcpy(rq.data, arg, sizeof(rq.data));
|
|
if (write(sd, &rq, sizeof(rq)) != sizeof(rq))
|
|
goto error;
|
|
if (read(sd, &svc, sizeof(svc)) != sizeof(svc))
|
|
goto error;
|
|
|
|
client_disconnect();
|
|
if (svc.pid < 0)
|
|
return NULL;
|
|
|
|
return &svc;
|
|
error:
|
|
client_disconnect();
|
|
warn("Failed communicating with finit, error %d", errno);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
svc_t *client_svc_find(const char *arg)
|
|
{
|
|
return do_find(INIT_CMD_SVC_FIND, arg);
|
|
}
|
|
|
|
svc_t *client_svc_find_by_cond(const char *arg)
|
|
{
|
|
return do_find(INIT_CMD_SVC_FIND_BYC, arg);
|
|
}
|
|
|
|
/**
|
|
* Local Variables:
|
|
* indent-tabs-mode: t
|
|
* c-file-style: "linux"
|
|
* End:
|
|
*/
|