|
| 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 | +} |
0 commit comments