-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvdb.c
103 lines (84 loc) · 2.75 KB
/
kvdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "clientconst.h"
void print_usage();
int main(int argc, char* argv[])
{
int sock;
int bytes_read = 0;
int rc;
struct sockaddr_un server;
// buffers to read/write data from/to the daemon
char commandbuff[BUFF_SIZE]; // user command buffer
char resbuff[BUFF_SIZE]; // response from the server buffer
memset(commandbuff, 0, BUFF_SIZE * sizeof(char)); // clean the command buffer
memset(resbuff, 0, BUFF_SIZE * sizeof(char)); // clean the response buffer
if(argc < 2) {
print_usage();
exit(1);
}
// compose the command
strncpy(commandbuff, argv[1], BUFF_SIZE);
strcat(commandbuff, " "); // "get "
strncat(commandbuff, argv[2], BUFF_SIZE); // "get key"
if(strncmp(argv[1], "set", BUFF_SIZE) == 0) {
if(argc < 4) {
fputs("Incorrect usage of 'set' command.\n\n", stdout);
print_usage();
exit(1);
}
strcat(commandbuff, " "); // "set key "
strncat(commandbuff, argv[3], BUFF_SIZE); // "set key value"
}
commandbuff[BUFF_SIZE - 1] = '\0';
/* SOCKET INITIALIZATION */
// creating a new socket
memset(&server, 0, sizeof(struct sockaddr_un));
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
fputs("Error opening stream socket.\n", stdout);
exit(1);
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, SOCKET_NAME);
// connecting to the socket created in the deamon
// the file name is used to specify the correct socket
rc = connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un));
if (rc < 0) {
fputs("Error while connecting socket.\nMaybe kvdbd is not running...\n", stdout);
close(sock);
exit(1);
}
/* COMMAND SEND */
// send command to the daemon
rc = write(sock, commandbuff, BUFF_SIZE);
if(rc == -1) {
perror("Error while sending command to the daemon.");
close(sock);
exit(1);
}
// wait for the response
bytes_read = read(sock, resbuff, BUFF_SIZE);
if(bytes_read == -1)
perror("Error while reading response from the daemon.");
else
puts(resbuff);
close(sock);
return 0;
}
void print_usage() {
fputs("Usage: kvdb <command>\n", stdout);
fputs("Possible commands:\n", stdout);
fputs(" set {key} {value}\n", stdout);
fputs(" get {key}\n", stdout);
fputs(" del {key}\n", stdout);
fputs(" ts {key}\n", stdout);
fputs("\nExample:\n", stdout);
fputs(" kvdb get pen\n", stdout);
}