Skip to content

Commit c0542f4

Browse files
committed
Refactor memcr-client code to create libmemcrclient.so licensed as LGPL2.1
1 parent 9de7b7d commit c0542f4

File tree

9 files changed

+778
-103
lines changed

9 files changed

+778
-103
lines changed

COPYING

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,28 @@ General Public License for more details.
3131
You should have received a copy of the GNU General Public License
3232
along with this library; if not, write to the Free Software Foundation,
3333
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
34+
35+
================================================================================
36+
37+
memcrclient_proto.h and libmemcrclient shared libarary are licensed by
38+
Liberty Global Service B.V. under the GNU Lesser General Public License
39+
version 2.1 only, as per COPYING.LGPL2.1.
40+
41+
Please use the following copyright for source for libmemcrlient shared library
42+
source code:
43+
44+
Copyright (C) <YEAR> <NAME>
45+
46+
This library is free software; you can redistribute it and/or
47+
modify it under the terms of the GNU Lesser General Public
48+
License as published by the Free Software Foundation; either
49+
version 2.1 of the License, or (at your option) any later version.
50+
51+
This library is distributed in the hope that it will be useful,
52+
but WITHOUT ANY WARRANTY; without even the implied warranty of
53+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
54+
Lesser General Public License for more details.
55+
56+
You should have received a copy of the GNU Lesser General Public
57+
License along with this library; if not, see <https://www.gnu.org/licenses/>.
58+

COPYING.LGPL2.1

Lines changed: 501 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ GOFF = ./gen-offsets.sh
111111
B ?= .
112112

113113

114-
all: $(B)/memcr $(B)/memcr-client
114+
all: $(B)/memcr $(B)/memcr-client $(B)/libmemcrclient.so
115115

116116
ifeq ($(ENCRYPT), 1)
117117
all: $(B)/libencrypt.so
@@ -150,11 +150,23 @@ $(B)/memcr: $(B)/memcr.o $(B)/cpu.o $(B)/enter.o
150150
@stat -c "-> %n: %s bytes <-" $@
151151
@size $@
152152

153+
$(B)/libmemcrclient.o: libmemcrclient.c
154+
$(CC) $(CFLAGS) -fPIC -c $< -o $@
155+
156+
$(B)/libmemcrclient.a: $(B)/libmemcrclient.o
157+
ar rcs $@ $^
158+
@stat -c "-> %n: %s bytes <-" $@
159+
@size $@
160+
161+
$(B)/libmemcrclient.so: $(B)/libmemcrclient.o
162+
$(CC) $(CFLAGS) -shared -Wl,-soname,$(@F) $^ -o $@
163+
@stat -c "-> %n: %s bytes <-" $@
164+
@size $@
153165

154166
$(B)/memcr-client.o: memcr-client.c
155167
$(CC) $(CFLAGS) -DGIT_VERSION='"$(GIT_VERSION)"' -I$(B) -c $< -o $@
156168

157-
$(B)/memcr-client: $(B)/memcr-client.o
169+
$(B)/memcr-client: $(B)/memcr-client.o $(B)/libmemcrclient.a
158170
$(CC) $(CFLAGS) $^ -o $@
159171
@stat -c "-> %n: %s bytes <-" $@
160172
@size $@
@@ -175,7 +187,7 @@ endif
175187

176188

177189
clean:
178-
rm -f $(B)/*.o $(B)/*.s $(B)/*.bin $(B)/parasite-blob.h $(B)/memcr $(B)/memcr-client $(B)/libencrypt.so
190+
rm -f $(B)/*.o $(B)/*.s $(B)/*.bin $(B)/parasite-blob.h $(B)/memcr $(B)/memcr-client $(B)/*.so $(B)/*.a
179191
$(MAKE) -C tests clean
180192

181193

libmemcrclient.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright 2023 Comcast Cable Communications Management, LLC
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
19+
#include <stdio.h>
20+
#include <sys/socket.h>
21+
#include <arpa/inet.h>
22+
#include <sys/types.h>
23+
#include <sys/un.h>
24+
#include <unistd.h>
25+
#include <getopt.h>
26+
#include <stdlib.h>
27+
28+
#include "memcrclient_proto.h"
29+
#include "libmemcrclient.h"
30+
31+
static int xconnect(struct sockaddr *addr, socklen_t addrlen)
32+
{
33+
int cd, ret;
34+
35+
cd = socket(addr->sa_family, SOCK_STREAM, 0);
36+
if (cd < 0) {
37+
fprintf(stderr, "socket() failed: %m\n");
38+
return -1;
39+
}
40+
41+
ret = connect(cd, addr, addrlen);
42+
if (ret < 0) {
43+
fprintf(stderr, "connect() failed: %m\n");
44+
close(cd);
45+
return ret;
46+
}
47+
48+
return cd;
49+
}
50+
51+
static int connect_unix(const char *path)
52+
{
53+
struct sockaddr_un addr = {
54+
.sun_family = AF_UNIX,
55+
};
56+
57+
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
58+
59+
return xconnect((struct sockaddr *)&addr, sizeof(addr));
60+
}
61+
62+
static int connect_tcp(int port)
63+
{
64+
struct sockaddr_in addr = {
65+
.sin_family = AF_INET,
66+
.sin_addr.s_addr = inet_addr("127.0.0.1"),
67+
.sin_port = htons(port),
68+
};
69+
70+
return xconnect((struct sockaddr *)&addr, sizeof(addr));
71+
}
72+
73+
static int send_cmd(int cd, struct service_command cmd)
74+
{
75+
int ret;
76+
struct service_response resp = {0};
77+
78+
ret = write(cd, &cmd, sizeof(struct service_command));
79+
if (ret != sizeof(struct service_command)) {
80+
fprintf(stderr, "%s() write request failed: ret %d, errno %m\n", __func__, ret);
81+
return -1;
82+
}
83+
84+
ret = read(cd, &resp, sizeof(struct service_response));
85+
if (ret != sizeof(struct service_response)) {
86+
fprintf(stderr, "%s() read response failed: ret %d, errno %m\n", __func__, ret);
87+
return -1;
88+
}
89+
90+
fprintf(stdout, "Procedure finished with %s status.\n", MEMCR_OK == resp.resp_code ? "OK" : "ERROR");
91+
92+
return resp.resp_code;
93+
}
94+
95+
96+
int memcr_client_connect(const char* comm_location)
97+
{
98+
int cd;
99+
int port = atoi(comm_location);
100+
101+
if (port > 0)
102+
cd = connect_tcp(port);
103+
else
104+
cd = connect_unix(comm_location);
105+
106+
if (cd < 0) {
107+
fprintf(stderr, "Connection creation failed!\n");
108+
}
109+
return cd;
110+
}
111+
112+
void memcr_client_disconnect(const int cd)
113+
{
114+
close(cd);
115+
}
116+
117+
int memcr_client_checkpoint(const int cd, const unsigned int pid)
118+
{
119+
struct service_command cmd = {0};
120+
121+
cmd.cmd = MEMCR_CHECKPOINT;
122+
cmd.pid = pid;
123+
124+
return send_cmd(cd, cmd);
125+
}
126+
127+
int memcr_client_restore(const int cd, const unsigned int pid)
128+
{
129+
struct service_command cmd = {0};
130+
131+
cmd.cmd = MEMCR_RESTORE;
132+
cmd.pid = pid;
133+
134+
return send_cmd(cd, cmd);
135+
}

libmemcrclient.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2023 Comcast Cable Communications Management, LLC
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
19+
#ifndef __MEMCR_CLIENT_LIB_H__
20+
#define __MEMCR_CLIENT_LIB_H__
21+
22+
/* open connection to memcr daemon
23+
* params:
24+
* comm_location - string containing the TCP socket port, or path to UNIX domain socket file
25+
* result:
26+
* connection descryptor
27+
*/
28+
int memcr_client_connect(const char* comm_location);
29+
30+
/* close connection to memcr daemon
31+
* params:
32+
* cd - connection descriptor returned by memcr_client_connect
33+
*/
34+
void memcr_client_disconnect(const int cd);
35+
36+
/* suspend process
37+
* params:
38+
* cd - connection descriptor returned by memcr_client_connect
39+
* pid - pid of process to suspend
40+
* result:
41+
* 0 on success, <0 on error
42+
*/
43+
int memcr_client_checkpoint(const int cd, const unsigned int pid);
44+
45+
/* restore process
46+
* params:
47+
* cd - connection descriptor returned by memcr_client_connect
48+
* pid - pid of process to suspend
49+
* result:
50+
* 0 on success, <0 on error
51+
*/
52+
int memcr_client_restore(const int cd, const unsigned int pid);
53+
54+
#endif /* __MEMCR_CLIENT_LIB_H__ */

memcr-client.c

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -26,70 +26,7 @@
2626
#include <stdlib.h>
2727

2828
#include "memcr.h"
29-
30-
static int xconnect(struct sockaddr *addr, socklen_t addrlen)
31-
{
32-
int cd, ret;
33-
34-
cd = socket(addr->sa_family, SOCK_STREAM, 0);
35-
if (cd < 0) {
36-
fprintf(stderr, "socket() failed: %m\n");
37-
return -1;
38-
}
39-
40-
ret = connect(cd, addr, addrlen);
41-
if (ret < 0) {
42-
fprintf(stderr, "connect() failed: %m\n");
43-
close(cd);
44-
return ret;
45-
}
46-
47-
return cd;
48-
}
49-
50-
static int connect_unix(const char *path)
51-
{
52-
struct sockaddr_un addr = {
53-
.sun_family = AF_UNIX,
54-
};
55-
56-
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
57-
58-
return xconnect((struct sockaddr *)&addr, sizeof(addr));
59-
}
60-
61-
static int connect_tcp(int port)
62-
{
63-
struct sockaddr_in addr = {
64-
.sin_family = AF_INET,
65-
.sin_addr.s_addr = inet_addr("127.0.0.1"),
66-
.sin_port = htons(port),
67-
};
68-
69-
return xconnect((struct sockaddr *)&addr, sizeof(addr));
70-
}
71-
72-
static int send_cmd(int cd, struct service_command cmd)
73-
{
74-
int ret;
75-
struct service_response resp = {0};
76-
77-
ret = write(cd, &cmd, sizeof(struct service_command));
78-
if (ret != sizeof(struct service_command)) {
79-
fprintf(stderr, "%s() write request failed: ret %d, errno %m\n", __func__, ret);
80-
return -1;
81-
}
82-
83-
ret = read(cd, &resp, sizeof(struct service_response));
84-
if (ret != sizeof(struct service_response)) {
85-
fprintf(stderr, "%s() read response failed: ret %d, errno %m\n", __func__, ret);
86-
return -1;
87-
}
88-
89-
fprintf(stdout, "Procedure finished with %s status.\n", MEMCR_OK == resp.resp_code ? "OK" : "ERROR");
90-
91-
return resp.resp_code;
92-
}
29+
#include "libmemcrclient.h"
9330

9431
static void print_version(void)
9532
{
@@ -117,9 +54,7 @@ int main(int argc, char *argv[])
11754
int ret, cd, opt;
11855
int checkpoint = 0;
11956
int restore = 0;
120-
int port = -1;
12157
int option_index;
122-
struct service_command cmd = {0};
12358
char *comm_location = NULL;
12459
int pid = 0;
12560

@@ -171,12 +106,7 @@ int main(int argc, char *argv[])
171106
return -1;
172107
}
173108

174-
port = atoi(comm_location);
175-
176-
if (port > 0)
177-
cd = connect_tcp(port);
178-
else
179-
cd = connect_unix(comm_location);
109+
cd = memcr_client_connect(comm_location);
180110

181111
if (cd < 0) {
182112
fprintf(stderr, "Connection creation failed!\n");
@@ -185,21 +115,16 @@ int main(int argc, char *argv[])
185115

186116
if (checkpoint) {
187117
fprintf(stdout, "Will checkpoint %d.\n", pid);
188-
cmd.cmd = MEMCR_CHECKPOINT;
189-
cmd.pid = pid;
190-
ret = send_cmd(cd, cmd);
118+
ret = memcr_client_checkpoint(cd, pid);
191119
}
192120

193121
if (restore) {
194122
fprintf(stdout, "Will restore %d.\n", pid);
195-
cmd.cmd = MEMCR_RESTORE;
196-
cmd.pid = pid;
197-
ret = send_cmd(cd, cmd);
123+
ret = memcr_client_restore(cd, pid);
198124
}
199125

200126
fprintf(stdout, "Command executed, exiting.\n");
201-
close(cd);
127+
memcr_client_disconnect(cd);
202128

203129
return ret;
204130
}
205-

memcr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#endif
6666

6767
#include "memcr.h"
68+
#include "memcrclient_proto.h"
6869
#include "arch/cpu.h"
6970
#include "arch/enter.h"
7071
#include "parasite-blob.h"

0 commit comments

Comments
 (0)