00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <eyedbconfig.h>
00025
00026 #include <sys/time.h>
00027 #include <sys/types.h>
00028 #include <sys/socket.h>
00029 #include <sys/uio.h>
00030 #include <sys/un.h>
00031 #include <netdb.h>
00032 #include <netinet/in.h>
00033 #include <unistd.h>
00034 #include <sys/wait.h>
00035 #include <sys/types.h>
00036 #include <sys/select.h>
00037 #include <signal.h>
00038 #include <unistd.h>
00039 #include <fcntl.h>
00040 #include <sys/stat.h>
00041 #include <errno.h>
00042
00043 #include <eyedblib/rpc_lib.h>
00044
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include "eyedbsm_p.h"
00048 #include <eyedbsm/smd.h>
00049 #include "lib/compile_builtin.h"
00050
00051 smdcli_conn_t *
00052 smdcli_open(const char *port)
00053 {
00054 struct sockaddr_un sock_un_name;
00055 struct sockaddr *sock_addr;
00056 int sock_fd;
00057
00058 sock_un_name.sun_family = AF_UNIX;
00059 strcpy(sock_un_name.sun_path, port);
00060 sock_addr = (struct sockaddr *)&sock_un_name;
00061 if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
00062 {
00063
00064 return 0;
00065 }
00066
00067 if (connect(sock_fd, sock_addr, sizeof(sock_un_name)) < 0)
00068 {
00069
00070
00071 return 0;
00072 }
00073
00074 smdcli_conn_t *conn = new smdcli_conn_t();
00075 conn->sock_fd = sock_fd;
00076 return conn;
00077 }
00078
00079 void
00080 smdcli_close(smdcli_conn_t *conn)
00081 {
00082 close(conn->sock_fd);
00083 delete conn;
00084 conn = 0;
00085 }
00086
00087 int
00088 smdcli_init_getsems(smdcli_conn_t *conn, const char *dbfile, int sm[])
00089 {
00090 int msg = SMD_INIT_GETSEMS;
00091 if (rpc_socketWrite(conn->sock_fd, &msg, sizeof(msg)) != sizeof(msg))
00092 {
00093 perror("write");
00094 return 1;
00095 }
00096 int len = strlen(dbfile)+1;
00097 if (rpc_socketWrite(conn->sock_fd, &len, sizeof(len)) != sizeof(len))
00098 {
00099 perror("write");
00100 return 1;
00101 }
00102
00103 if (rpc_socketWrite(conn->sock_fd, (void *)dbfile, len) != len)
00104 {
00105 perror("write");
00106 return 1;
00107 }
00108
00109 if (rpc_socketRead(conn->sock_fd, sm, sizeof(int)*ESM_NSEMS) !=
00110 sizeof(int)*ESM_NSEMS)
00111 {
00112 perror("read");
00113 return 1;
00114 }
00115
00116 return 0;
00117 }
00118
00119 int
00120 smdcli_release(smdcli_conn_t *conn, const char *dbfile)
00121 {
00122 int msg = SMD_RELEASE;
00123 if (rpc_socketWrite(conn->sock_fd, &msg, sizeof(msg)) != sizeof(msg))
00124 {
00125 perror("write");
00126 return 1;
00127 }
00128 int len = strlen(dbfile)+1;
00129 if (rpc_socketWrite(conn->sock_fd, &len, sizeof(len)) != sizeof(len))
00130 {
00131 perror("write");
00132 return 1;
00133 }
00134
00135 if (rpc_socketWrite(conn->sock_fd, (void *)dbfile, len) != len)
00136 {
00137 perror("write");
00138 return 1;
00139 }
00140
00141 return 0;
00142 }
00143
00144 int
00145 smdcli_stop(smdcli_conn_t *conn)
00146 {
00147 int msg = SMD_STOP;
00148 if (rpc_socketWrite(conn->sock_fd, &msg, sizeof(msg)) != sizeof(msg))
00149 {
00150 perror("write");
00151 return 1;
00152 }
00153
00154 return 0;
00155 }
00156
00157 int
00158 smdcli_status(smdcli_conn_t *conn)
00159 {
00160 int msg = SMD_STATUS;
00161 if (rpc_socketWrite(conn->sock_fd, &msg, sizeof(msg)) != sizeof(msg))
00162 {
00163 perror("write");
00164 return 1;
00165 }
00166
00167 return 0;
00168 }
00169
00170 int
00171 smdcli_declare(smdcli_conn_t *conn)
00172 {
00173 int msg = SMD_DECL;
00174 if (rpc_socketWrite(conn->sock_fd, &msg, sizeof(msg)) != sizeof(msg))
00175 {
00176 perror("write");
00177 return 1;
00178 }
00179
00180 return 0;
00181 }
00182
00183 int
00184 smdcli_undeclare(smdcli_conn_t *conn)
00185 {
00186 int msg = SMD_UNDECL;
00187 if (rpc_socketWrite(conn->sock_fd, &msg, sizeof(msg)) != sizeof(msg))
00188 {
00189 perror("write");
00190 return 1;
00191 }
00192
00193 return 0;
00194 }
00195
00196 int
00197 smdcli_init(smdcli_conn_t *conn, const char *dbfile)
00198 {
00199 int msg = SMD_INIT;
00200 if (rpc_socketWrite(conn->sock_fd, &msg, sizeof(msg)) != sizeof(msg))
00201 {
00202 perror("write");
00203 return 1;
00204 }
00205
00206 int len = strlen(dbfile)+1;
00207 if (rpc_socketWrite(conn->sock_fd, &len, sizeof(len)) != sizeof(len))
00208 {
00209 perror("write");
00210 return 1;
00211 }
00212
00213 if (rpc_socketWrite(conn->sock_fd, (void *)dbfile, len) != len)
00214 {
00215 perror("write");
00216 return 1;
00217 }
00218
00219 if (rpc_socketRead(conn->sock_fd, &msg, sizeof(msg)) != sizeof(msg))
00220 {
00221 perror("read");
00222 return 1;
00223 }
00224
00225 return 0;
00226 }
00227
00228 #define SMD_PORT_ENV "EYEDBSV_SMDPORT"
00229 #define SMD_PORT "/smd"
00230
00231 static std::string smd_port;
00232
00233 static const char *smd_get_port_p()
00234 {
00235 if (smd_port.length())
00236 return smd_port.c_str();
00237
00238 const char *s = getenv(SMD_PORT_ENV);
00239 if (s)
00240 return s;
00241
00242 static std::string path = eyedblib::CompileBuiltin::getPipedir();
00243 path += SMD_PORT;
00244
00245 return path.c_str();
00246 }
00247
00248 const char *smd_get_port()
00249 {
00250 static struct sockaddr_un sock_un_name;
00251 static unsigned sun_path_len = sizeof(sock_un_name.sun_path);
00252
00253 const char *port = smd_get_port_p();
00254 if (strlen(port) <= sun_path_len)
00255 return port;
00256
00257 static std::string stpath = port;
00258 stpath[sun_path_len] = 0;
00259
00260 return stpath.c_str();
00261 }
00262
00263 void smd_set_port(const char *port)
00264 {
00265 smd_port = std::string(port);
00266 }