int sanlock_write_lockspace(struct sanlk_lockspace *ls, int max_hosts, uint32_t flags, uint32_t io_timeout) { int rv, fd; if (!ls || !ls->host_id_disk.path[0]) return -EINVAL; rv = connect_socket(&fd); if (rv < 0) return rv; rv = send_header(fd, SM_CMD_WRITE_LOCKSPACE, flags, sizeof(struct sanlk_lockspace), max_hosts, io_timeout); if (rv < 0) goto out; rv = send_data(fd, ls, sizeof(struct sanlk_lockspace), 0); if (rv < 0) { rv = -errno; goto out; } rv = recv_result(fd); out: close(fd); return rv; }
int sanlock_request(uint32_t flags, uint32_t force_mode, struct sanlk_resource *res) { int fd, rv, datalen; datalen = sizeof(struct sanlk_resource) + sizeof(struct sanlk_disk) * res->num_disks; rv = connect_socket(&fd); if (rv < 0) return rv; rv = send_header(fd, SM_CMD_REQUEST, flags, datalen, force_mode, 0); if (rv < 0) goto out; rv = send(fd, res, sizeof(struct sanlk_resource), 0); if (rv < 0) { rv = -errno; goto out; } rv = send(fd, res->disks, sizeof(struct sanlk_disk) * res->num_disks, 0); if (rv < 0) { rv = -errno; goto out; } rv = recv_result(fd); out: close(fd); return rv; }
int sanlock_restrict(int sock, uint32_t flags) { int rv; rv = send_header(sock, SM_CMD_RESTRICT, flags, 0, 0, -1); if (rv < 0) return rv; rv = recv_result(sock); return rv; }
int sanlock_init(struct sanlk_lockspace *ls, struct sanlk_resource *res, int max_hosts, int num_hosts) { int rv, fd, cmd, datalen; if (!ls && !res) return -EINVAL; rv = connect_socket(&fd); if (rv < 0) return rv; if (ls && ls->host_id_disk.path[0]) { cmd = SM_CMD_INIT_LOCKSPACE; datalen = sizeof(struct sanlk_lockspace); } else { cmd = SM_CMD_INIT_RESOURCE; datalen = sizeof(struct sanlk_resource) + sizeof(struct sanlk_disk) * res->num_disks; } rv = send_header(fd, cmd, 0, datalen, max_hosts, num_hosts); if (rv < 0) goto out; if (ls) { rv = send(fd, ls, sizeof(struct sanlk_lockspace), 0); if (rv < 0) { rv = -errno; goto out; } } else { rv = send(fd, res, sizeof(struct sanlk_resource), 0); if (rv < 0) { rv = -errno; goto out; } rv = send(fd, res->disks, sizeof(struct sanlk_disk) * res->num_disks, 0); if (rv < 0) { rv = -errno; goto out; } } rv = recv_result(fd); out: close(fd); return rv; }
bool c_chls::get_channel_inf(int ichannel) { sprintf(buf, "chinf n %d", ichannel); int ret = send(sock, buf, CMD_LEN, 0); if(ret == -1){ cerr << "Failed to send command." << endl; return false; } const char * res; if(res = recv_result()){ cout << res << endl; return true; } return false; }
int sanlock_release(int sock, int pid, uint32_t flags, int res_count, struct sanlk_resource *res_args[]) { int fd, rv, i, data2, datalen; if (sock == -1) { /* connect to daemon and ask it to acquire a lease for another registered pid */ data2 = pid; rv = connect_socket(&fd); if (rv < 0) return rv; } else { /* use our own existing registered connection and ask daemon to acquire a lease for self */ data2 = -1; fd = sock; } datalen = res_count * sizeof(struct sanlk_resource); rv = send_header(fd, SM_CMD_RELEASE, flags, datalen, res_count, data2); if (rv < 0) goto out; for (i = 0; i < res_count; i++) { rv = send(fd, res_args[i], sizeof(struct sanlk_resource), 0); if (rv < 0) { rv = -1; goto out; } } rv = recv_result(fd); out: if (sock == -1) close(fd); return rv; }
int c_chls::get_num_channels() { // generating command string sprintf(buf, "chinf"); // sending command int ret = send(sock, buf, CMD_LEN, 0); if(ret == -1){ cerr << "Failed to send command." << endl; return -1; } // recieving result const char * res; if(res = recv_result()){ int num_channels = atoi(res); return num_channels; } return -1; }
int sanlock_examine(uint32_t flags, struct sanlk_lockspace *ls, struct sanlk_resource *res) { char *data; int rv, fd, cmd, datalen; if (!ls && !res) return -EINVAL; rv = connect_socket(&fd); if (rv < 0) return rv; if (ls && ls->host_id_disk.path[0]) { cmd = SM_CMD_EXAMINE_LOCKSPACE; datalen = sizeof(struct sanlk_lockspace); data = (char *)ls; } else { cmd = SM_CMD_EXAMINE_RESOURCE; datalen = sizeof(struct sanlk_resource); data = (char *)res; } rv = send_header(fd, cmd, flags, datalen, 0, 0); if (rv < 0) goto out; rv = send(fd, data, datalen, 0); if (rv < 0) { rv = -errno; goto out; } rv = recv_result(fd); out: close(fd); return rv; }
int sanlock_align(struct sanlk_disk *disk) { int rv, fd; rv = connect_socket(&fd); if (rv < 0) return rv; rv = send_header(fd, SM_CMD_ALIGN, 0, sizeof(struct sanlk_disk), 0, 0); if (rv < 0) goto out; rv = send_data(fd, (void *)disk, sizeof(struct sanlk_disk), 0); if (rv < 0) { rv = -errno; goto out; } rv = recv_result(fd); out: close(fd); return rv; }
static int cmd_lockspace(int cmd, struct sanlk_lockspace *ls, uint32_t flags, uint32_t data) { int rv, fd; rv = connect_socket(&fd); if (rv < 0) return rv; rv = send_header(fd, cmd, flags, sizeof(struct sanlk_lockspace), data, 0); if (rv < 0) goto out; rv = send_data(fd, (void *)ls, sizeof(struct sanlk_lockspace), 0); if (rv < 0) { rv = -errno; goto out; } rv = recv_result(fd); out: close(fd); return rv; }
int sanlock_write_resource(struct sanlk_resource *res, int max_hosts, int num_hosts, uint32_t flags) { int rv, fd; if (!res || !res->num_disks || res->num_disks > SANLK_MAX_DISKS || !res->disks[0].path[0]) return -EINVAL; rv = connect_socket(&fd); if (rv < 0) return rv; rv = send_header(fd, SM_CMD_WRITE_RESOURCE, flags, sizeof(struct sanlk_resource) + sizeof(struct sanlk_disk) * res->num_disks, max_hosts, num_hosts); if (rv < 0) goto out; rv = send_data(fd, res, sizeof(struct sanlk_resource), 0); if (rv < 0) { rv = -errno; goto out; } rv = send_data(fd, res->disks, sizeof(struct sanlk_disk) * res->num_disks, 0); if (rv < 0) { rv = -errno; goto out; } rv = recv_result(fd); out: close(fd); return rv; }
int sanlock_acquire(int sock, int pid, uint32_t flags, int res_count, struct sanlk_resource *res_args[], struct sanlk_options *opt_in) { struct sanlk_resource *res; struct sanlk_options opt; int rv, i, fd, data2; int datalen = 0; if (res_count > SANLK_MAX_RESOURCES) return -EINVAL; for (i = 0; i < res_count; i++) { res = res_args[i]; datalen += sizeof(struct sanlk_resource); if (res->num_disks > SANLK_MAX_DISKS) return -EINVAL; datalen += (res->num_disks * sizeof(struct sanlk_disk)); } datalen += sizeof(struct sanlk_options); if (opt_in) { memcpy(&opt, opt_in, sizeof(struct sanlk_options)); datalen += opt_in->len; } else { memset(&opt, 0, sizeof(opt)); } if (sock == -1) { /* connect to daemon and ask it to acquire a lease for another registered pid */ data2 = pid; rv = connect_socket(&fd); if (rv < 0) return rv; } else { /* use our own existing registered connection and ask daemon to acquire a lease for self */ data2 = -1; fd = sock; } rv = send_header(fd, SM_CMD_ACQUIRE, flags, datalen, res_count, data2); if (rv < 0) return rv; for (i = 0; i < res_count; i++) { res = res_args[i]; rv = send(fd, res, sizeof(struct sanlk_resource), 0); if (rv < 0) { rv = -1; goto out; } rv = send(fd, res->disks, sizeof(struct sanlk_disk) * res->num_disks, 0); if (rv < 0) { rv = -1; goto out; } } rv = send(fd, &opt, sizeof(struct sanlk_options), 0); if (rv < 0) { rv = -1; goto out; } if (opt.len) { rv = send(fd, opt_in->str, opt.len, 0); if (rv < 0) { rv = -1; goto out; } } rv = recv_result(fd); out: if (sock == -1) close(fd); return rv; }
bool c_awsevt::send_cmd(int argc, char ** argv) { int iarg = 1; host = NULL; port = 0; evtstr = NULL; num_itrs = 1; // default iteration is 1 tout.tv_sec = 5; // default tout is 5sec tout.tv_usec = 0; while(iarg < argc){ if(strcmp(argv[iarg], "-f") == 0){ iarg++; if(iarg == argc){ return false; } fname = argv[iarg]; iarg++; }else if(strcmp(argv[iarg], "-t") == 0){// absolute time in [] form iarg++; if(iarg == argc){ // argument is not specified return false; } tstr = argv[iarg]; evtstr = "time"; iarg++; }else if(strcmp(argv[iarg], "-p") == 0){ // period in second iarg++; if(iarg == argc){ return false; } tstr = argv[iarg]; evtstr = "period"; iarg++; }else if(strcmp(argv[iarg], "-n") == 0){ // number of iteration iarg++; if(iarg == argc){ return false; } num_itrs = atoi(argv[iarg]); iarg++; }else if(strcmp(argv[iarg], "-sock") == 0){ // socket host and port iarg++; if(iarg == argc){ // host address is not specified return false; } host = argv[iarg]; iarg++; if(iarg == argc){ // port is not specified. return false; } port = (unsigned short) atoi(argv[iarg]); iarg++; }else if(strcmp(argv[iarg], "-to") == 0){ // time out value in second iarg++; if(iarg == argc){ return false; } double to = atof(argv[iarg]); tout.tv_sec = (long) to; tout.tv_usec = (long)((to - (double) tout.tv_sec) * 1e6); iarg++; } } // waiting socket is not configured if(host == NULL || port == 0) return false; // invoke wait thread pthread_create(&thwait, NULL, wait, (void*) this); if(evtstr == NULL){ // if no event is specifed simply wait the event pthread_join(thwait, NULL); return true; } sprintf(buf, "fset %s type %s tstr %s itrs %d host %s port %d breg y", fname, evtstr, tstr, num_itrs, host, (int)port); // cout << buf << endl; int ret = send(sock, buf, CMD_LEN, 0); if(ret == -1){ cerr << "Failed to send command." << endl; pthread_join(thwait, NULL); return false; } const char * res = recv_result(); if(res == NULL){ close_session(); pthread_join(thwait, NULL); return false; } close_session(); pthread_join(thwait, NULL); cout << evt << endl; return true; }