mirror of
https://github.com/troglobit/finit.git
synced 2026-11-07 16:01:08 +00:00
initctl: Refactor, reduce code duplication
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
af3e560d79
commit
fcdb5c2965
1 file changed
+22
-14
+22
-14
@@ -53,10 +53,9 @@ typedef struct {
|
|||||||
int verbose = 0;
|
int verbose = 0;
|
||||||
int runlevel = 0;
|
int runlevel = 0;
|
||||||
|
|
||||||
|
static int do_connect(void)
|
||||||
static int do_send(struct init_request *rq, ssize_t len)
|
|
||||||
{
|
{
|
||||||
int sd, result = 255;
|
int sd;
|
||||||
struct sockaddr_un sun = {
|
struct sockaddr_un sun = {
|
||||||
.sun_family = AF_UNIX,
|
.sun_family = AF_UNIX,
|
||||||
.sun_path = INIT_SOCKET,
|
.sun_path = INIT_SOCKET,
|
||||||
@@ -64,11 +63,27 @@ static int do_send(struct init_request *rq, ssize_t len)
|
|||||||
|
|
||||||
sd = socket(AF_UNIX, SOCK_STREAM, 0);
|
sd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
if (-1 == sd)
|
if (-1 == sd)
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (connect(sd, (struct sockaddr*)&sun, sizeof(sun)) == -1)
|
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
|
if (connect(sd, (struct sockaddr*)&sun, sizeof(sun)) == -1) {
|
||||||
|
close(sd);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sd;
|
||||||
|
error:
|
||||||
|
perror("Failed connecting to finit");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int do_send(struct init_request *rq, ssize_t len)
|
||||||
|
{
|
||||||
|
int sd, result = 255;
|
||||||
|
|
||||||
|
sd = do_connect();
|
||||||
|
if (-1 == sd)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (write(sd, rq, len) != len)
|
if (write(sd, rq, len) != len)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
@@ -88,22 +103,15 @@ static int runlevel_get(void)
|
|||||||
{
|
{
|
||||||
int sd, result = 255;
|
int sd, result = 255;
|
||||||
struct init_request rq;
|
struct init_request rq;
|
||||||
struct sockaddr_un sun = {
|
|
||||||
.sun_family = AF_UNIX,
|
|
||||||
.sun_path = INIT_SOCKET,
|
|
||||||
};
|
|
||||||
|
|
||||||
memset(&rq, 0, sizeof(rq));
|
memset(&rq, 0, sizeof(rq));
|
||||||
rq.cmd = INIT_CMD_GET_RUNLEVEL;
|
rq.cmd = INIT_CMD_GET_RUNLEVEL;
|
||||||
rq.magic = INIT_MAGIC;
|
rq.magic = INIT_MAGIC;
|
||||||
|
|
||||||
sd = socket(AF_UNIX, SOCK_STREAM, 0);
|
sd = do_connect();
|
||||||
if (-1 == sd)
|
if (-1 == sd)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (connect(sd, (struct sockaddr*)&sun, sizeof(sun)) == -1)
|
|
||||||
goto error;
|
|
||||||
|
|
||||||
if (write(sd, &rq, sizeof(rq)) != sizeof(rq))
|
if (write(sd, &rq, sizeof(rq)) != sizeof(rq))
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user