1 7736 Andrew /* 2 7736 Andrew * CDDL HEADER START 3 7736 Andrew * 4 7736 Andrew * The contents of this file are subject to the terms of the 5 7736 Andrew * Common Development and Distribution License (the "License"). 6 7736 Andrew * You may not use this file except in compliance with the License. 7 7736 Andrew * 8 7736 Andrew * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 7736 Andrew * or http://www.opensolaris.org/os/licensing. 10 7736 Andrew * See the License for the specific language governing permissions 11 7736 Andrew * and limitations under the License. 12 7736 Andrew * 13 7736 Andrew * When distributing Covered Code, include this CDDL HEADER in each 14 7736 Andrew * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 7736 Andrew * If applicable, add the following below this CDDL HEADER, with the 16 7736 Andrew * fields enclosed by brackets "[]" replaced with your own identifying 17 7736 Andrew * information: Portions Copyright [yyyy] [name of copyright owner] 18 7736 Andrew * 19 7736 Andrew * CDDL HEADER END 20 7736 Andrew */ 21 7736 Andrew /* 22 7736 Andrew * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 7736 Andrew * Use is subject to license terms. 24 7736 Andrew */ 25 7736 Andrew 26 7736 Andrew 27 7736 Andrew #include "filebench.h" 28 7736 Andrew #include "multi_client_sync.h" 29 7736 Andrew #include <netdb.h> 30 7736 Andrew #include <netinet/in.h> 31 7736 Andrew #include <arpa/inet.h> 32 7736 Andrew #include <errno.h> 33 7736 Andrew 34 7736 Andrew #define MCS_NAMELENGTH 128 35 7736 Andrew #define MCS_MSGLENGTH (MCS_NAMELENGTH * 8) 36 7736 Andrew 37 7736 Andrew static int mc_sync_sock_id; 38 7736 Andrew static char this_client_name[MCS_NAMELENGTH]; 39 7736 Andrew 40 7736 Andrew /* 41 7736 Andrew * Open a socket to the master synchronization host 42 7736 Andrew */ 43 7736 Andrew int 44 7736 Andrew mc_sync_open_sock(char *master_name, int master_port, char *my_name) 45 7736 Andrew { 46 7736 Andrew struct sockaddr_in client_in; 47 7736 Andrew struct sockaddr_in master_in; 48 7736 Andrew struct hostent master_info; 49 7736 Andrew int error_num; 50 7736 Andrew char buffer[MCS_MSGLENGTH]; 51 7736 Andrew 52 7736 Andrew (void) strncpy(this_client_name, my_name, MCS_NAMELENGTH); 53 7736 Andrew if ((mc_sync_sock_id = socket(PF_INET, SOCK_STREAM, 0)) == -1) { 54 7736 Andrew filebench_log(LOG_ERROR, "could not create a client socket"); 55 7736 Andrew return (FILEBENCH_ERROR); 56 7736 Andrew } 57 7736 Andrew 58 7736 Andrew client_in.sin_family = AF_INET; 59 7736 Andrew client_in.sin_port = INADDR_ANY; 60 7736 Andrew client_in.sin_addr.s_addr = INADDR_ANY; 61 7736 Andrew 62 7736 Andrew if (bind(mc_sync_sock_id, (struct sockaddr *)&client_in, 63 7736 Andrew sizeof (client_in)) == -1) { 64 7736 Andrew filebench_log(LOG_ERROR, "could not bind to client socket"); 65 7736 Andrew return (FILEBENCH_ERROR); 66 7736 Andrew } 67 7736 Andrew 68 7736 Andrew if (gethostbyname_r(master_name, &master_info, buffer, MCS_MSGLENGTH, 69 7736 Andrew &error_num) == NULL) { 70 7736 Andrew filebench_log(LOG_ERROR, "could not locate sync master"); 71 7736 Andrew return (FILEBENCH_ERROR); 72 7736 Andrew } 73 7736 Andrew 74 7736 Andrew master_in.sin_family = AF_INET; 75 7736 Andrew master_in.sin_port = htons((uint16_t)master_port); 76 7736 Andrew (void) memcpy(&master_in.sin_addr.s_addr, *master_info.h_addr_list, 77 7736 Andrew sizeof (master_in.sin_addr.s_addr)); 78 7736 Andrew 79 7736 Andrew if (connect(mc_sync_sock_id, (struct sockaddr *)&master_in, 80 7736 Andrew sizeof (master_in)) == -1) { 81 7736 Andrew filebench_log(LOG_ERROR, 82 7736 Andrew "connection refused to sync master, error %d", errno); 83 7736 Andrew return (FILEBENCH_ERROR); 84 7736 Andrew } 85 7736 Andrew 86 7736 Andrew return (FILEBENCH_OK); 87 7736 Andrew } 88 7736 Andrew 89 7736 Andrew /* 90 7736 Andrew * Send a synchronization message and wait for a reply 91 7736 Andrew */ 92 7736 Andrew int 93 7736 Andrew mc_sync_synchronize(int sync_point) 94 7736 Andrew { 95 7736 Andrew char msg[MCS_MSGLENGTH]; 96 7736 Andrew int amnt; 97 7736 Andrew 98 7736 Andrew (void) snprintf(msg, MCS_MSGLENGTH, 99 7736 Andrew "cmd=SYNC,id=xyzzy,name=%s,sample=%d\n", 100 7736 Andrew this_client_name, sync_point); 101 7736 Andrew (void) send(mc_sync_sock_id, msg, strlen(msg), 0); 102 7736 Andrew 103 7736 Andrew amnt = 0; 104 7736 Andrew msg[0] = 0; 105 7736 Andrew 106 7736 Andrew while (strchr(msg, '\n') == NULL) 107 7736 Andrew amnt += recv(mc_sync_sock_id, msg, sizeof (msg), 0); 108 7736 Andrew 109 7736 Andrew filebench_log(LOG_INFO, "sync point %d succeeded!\n", sync_point); 110 7736 Andrew return (FILEBENCH_OK); 111 7736 Andrew } 112