-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor memcr-client code to create libmemcrclient.so licensed as LG…
…PL2.1
- Loading branch information
Showing
9 changed files
with
778 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright 2023 Comcast Cable Communications Management, LLC | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
#include <sys/types.h> | ||
#include <sys/un.h> | ||
#include <unistd.h> | ||
#include <getopt.h> | ||
#include <stdlib.h> | ||
|
||
#include "memcrclient_proto.h" | ||
#include "libmemcrclient.h" | ||
|
||
static int xconnect(struct sockaddr *addr, socklen_t addrlen) | ||
{ | ||
int cd, ret; | ||
|
||
cd = socket(addr->sa_family, SOCK_STREAM, 0); | ||
if (cd < 0) { | ||
fprintf(stderr, "socket() failed: %m\n"); | ||
return -1; | ||
} | ||
|
||
ret = connect(cd, addr, addrlen); | ||
if (ret < 0) { | ||
fprintf(stderr, "connect() failed: %m\n"); | ||
close(cd); | ||
return ret; | ||
} | ||
|
||
return cd; | ||
} | ||
|
||
static int connect_unix(const char *path) | ||
{ | ||
struct sockaddr_un addr = { | ||
.sun_family = AF_UNIX, | ||
}; | ||
|
||
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path); | ||
|
||
return xconnect((struct sockaddr *)&addr, sizeof(addr)); | ||
} | ||
|
||
static int connect_tcp(int port) | ||
{ | ||
struct sockaddr_in addr = { | ||
.sin_family = AF_INET, | ||
.sin_addr.s_addr = inet_addr("127.0.0.1"), | ||
.sin_port = htons(port), | ||
}; | ||
|
||
return xconnect((struct sockaddr *)&addr, sizeof(addr)); | ||
} | ||
|
||
static int send_cmd(int cd, struct service_command cmd) | ||
{ | ||
int ret; | ||
struct service_response resp = {0}; | ||
|
||
ret = write(cd, &cmd, sizeof(struct service_command)); | ||
if (ret != sizeof(struct service_command)) { | ||
fprintf(stderr, "%s() write request failed: ret %d, errno %m\n", __func__, ret); | ||
return -1; | ||
} | ||
|
||
ret = read(cd, &resp, sizeof(struct service_response)); | ||
if (ret != sizeof(struct service_response)) { | ||
fprintf(stderr, "%s() read response failed: ret %d, errno %m\n", __func__, ret); | ||
return -1; | ||
} | ||
|
||
fprintf(stdout, "Procedure finished with %s status.\n", MEMCR_OK == resp.resp_code ? "OK" : "ERROR"); | ||
|
||
return resp.resp_code; | ||
} | ||
|
||
|
||
int memcr_client_connect(const char* comm_location) | ||
{ | ||
int cd; | ||
int port = atoi(comm_location); | ||
|
||
if (port > 0) | ||
cd = connect_tcp(port); | ||
else | ||
cd = connect_unix(comm_location); | ||
|
||
if (cd < 0) { | ||
fprintf(stderr, "Connection creation failed!\n"); | ||
} | ||
return cd; | ||
} | ||
|
||
void memcr_client_disconnect(const int cd) | ||
{ | ||
close(cd); | ||
} | ||
|
||
int memcr_client_checkpoint(const int cd, const unsigned int pid) | ||
{ | ||
struct service_command cmd = {0}; | ||
|
||
cmd.cmd = MEMCR_CHECKPOINT; | ||
cmd.pid = pid; | ||
|
||
return send_cmd(cd, cmd); | ||
} | ||
|
||
int memcr_client_restore(const int cd, const unsigned int pid) | ||
{ | ||
struct service_command cmd = {0}; | ||
|
||
cmd.cmd = MEMCR_RESTORE; | ||
cmd.pid = pid; | ||
|
||
return send_cmd(cd, cmd); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2023 Comcast Cable Communications Management, LLC | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#ifndef __MEMCR_CLIENT_LIB_H__ | ||
#define __MEMCR_CLIENT_LIB_H__ | ||
|
||
/* open connection to memcr daemon | ||
* params: | ||
* comm_location - string containing the TCP socket port, or path to UNIX domain socket file | ||
* result: | ||
* connection descryptor | ||
*/ | ||
int memcr_client_connect(const char* comm_location); | ||
|
||
/* close connection to memcr daemon | ||
* params: | ||
* cd - connection descriptor returned by memcr_client_connect | ||
*/ | ||
void memcr_client_disconnect(const int cd); | ||
|
||
/* suspend process | ||
* params: | ||
* cd - connection descriptor returned by memcr_client_connect | ||
* pid - pid of process to suspend | ||
* result: | ||
* 0 on success, <0 on error | ||
*/ | ||
int memcr_client_checkpoint(const int cd, const unsigned int pid); | ||
|
||
/* restore process | ||
* params: | ||
* cd - connection descriptor returned by memcr_client_connect | ||
* pid - pid of process to suspend | ||
* result: | ||
* 0 on success, <0 on error | ||
*/ | ||
int memcr_client_restore(const int cd, const unsigned int pid); | ||
|
||
#endif /* __MEMCR_CLIENT_LIB_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.