Hi ,

I am trying out a simple c client for zookeeper, made a copy of 
zookeeper-3.4.5/src/c/src/cli.c just to understand how the c APIs works, the 
program is attached.

I see errors/coredumps being generated, I am surely doing something wrong, any 
help appreciated.

case 1:

.../zookeeper-3.4.5/src/c> ./myt localhost:2181
Watcher SESSION_EVENT state = CONNECTED_STATE
Connected ...
Watcher SESSION_EVENT state = CONNECTED_STATE
Connected ...
Segmentation fault (core dumped)

starting it under gdb:
(gdb) r localhost:2181
Starting program: .../zookeeper-3.4.5/src/c/myt localhost:2181
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff70d9700 (LWP 9489)]
[New Thread 0x7ffff68d8700 (LWP 9490)]
Watcher SESSION_EVENT state = CONNECTED_STATE
Connected ...
Watcher SESSION_EVENT state = CONNECTED_STATE
Connected ...
2013-05-21 
15:58:03,210:9485(0x7ffff7fd7700):ZOO_ERROR@handle_socket_error_msg@1643: 
Socket [127.0.0.1:2181] zk retcode=-7, errno=110(Connection timed out): 
connection to 127.0.0.1:2181 timed out (exceeded timeout by 1ms)
2013-05-21 
15:58:03,210:9485(0x7ffff70d9700):ZOO_ERROR@handle_socket_error_msg@1643: 
Socket [127.0.0.1:2181] zk retcode=-7, errno=110(Connection timed out): 
connection to 127.0.0.1:2181 timed out (exceeded timeout by 2ms)
Watcher SESSION_EVENT state = CONNECTING_STATE
Watcher SESSION_EVENT state = CONNECTING_STATE
2013-05-21 15:58:13,212:9485(0x7ffff7fd7700):ZOO_WARN@zookeeper_interest@1557: 
Exceeded deadline by 10003ms
Watcher SESSION_EVENT state = CONNECTED_STATE
Connected ...

Program received signal SIGSEGV, Segmentation fault.
queue_buffer (list=0x6030b8, b=0x0, add_to_front=0) at src/zookeeper.c:970
970         b->next = 0;
(gdb) bt
#0  queue_buffer (list=0x6030b8, b=0x0, add_to_front=0) at src/zookeeper.c:970
#1  0x00007ffff7bc939d in check_events (events=<optimized out>, zh=0x603040) at 
src/zookeeper.c:1727
#2  zookeeper_process (zh=0x603040, events=<optimized out>) at 
src/zookeeper.c:2170
#3  0x0000000000401448 in main (argc=2, argv=0x7fffffffeab8) at mytest.c:542

case 2:

2013-05-21 
15:49:25,811:2493(0x7fae0ec17700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
2013-05-21 
15:49:25,812:2493(0x7fae0fb16700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
2013-05-21 
15:49:25,813:2493(0x7fae0ec17700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
2013-05-21 
15:49:25,814:2493(0x7fae0fb16700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
2013-05-21 
15:49:25,814:2493(0x7fae0ec17700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
2013-05-21 
15:49:25,815:2493(0x7fae0fb16700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
2013-05-21 
15:49:25,816:2493(0x7fae0ec17700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
Segmentation fault (core dumped)

under gdb:
2013-05-21 
16:05:13,598:10413(0x7ffff7fd7700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
2013-05-21 
16:05:13,598:10413(0x7ffff70d9700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
2013-05-21 
16:05:13,599:10413(0x7ffff7fd7700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed
2013-05-21 
16:05:13,599:10413(0x7ffff70d9700):ZOO_ERROR@handle_socket_error_msg@1579: 
Socket [:0] zk retcode=-1, errno=97(Address family not supported by protocol): 
socket() call failed

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff70d9700 (LWP 10415)]
zookeeper_interest (zh=0x603040, fd=0x7ffff70d8e38, interest=0x7ffff70d8e3c, 
tv=0x7ffff70d8e20)
    at src/zookeeper.c:1577
1577                zh->fd = socket(zh->addrs[zh->connect_index].ss_family, 
SOCK_STREAM, 0);
(gdb) bt
#0  zookeeper_interest (zh=0x603040, fd=0x7ffff70d8e38, 
interest=0x7ffff70d8e3c, tv=0x7ffff70d8e20)
    at src/zookeeper.c:1577
#1  0x00007ffff7bd1361 in do_io (v=0x603040) at src/mt_adaptor.c:378
#2  0x00007ffff72eee9a in start_thread () from 
/lib/x86_64-linux-gnu/libpthread.so.0
#3  0x00007ffff78f3cbd in clone () from /lib/x86_64-linux-gnu/libc.so.6
#4  0x0000000000000000 in ?? ()


case 3
zookeeper-3.4.5/src/c> ./myt localhost:2181
Watcher SESSION_EVENT state = CONNECTED_STATE
Connected ...
2013-05-21 
15:54:04,078:7030(0x7f36727cf700):ZOO_ERROR@handle_socket_error_msg@1643: 
Socket [127.0.0.1:2181] zk retcode=-7, errno=110(Connection timed out): 
connection to 127.0.0.1:2181 timed out (exceeded timeout by 7ms)
Watcher SESSION_EVENT state = CONNECTING_STATE
Watcher SESSION_EVENT state = CONNECTED_STATE
Connected ...
2013-05-21 
15:54:24,085:7030(0x7f36736ce700):ZOO_ERROR@handle_socket_error_msg@2245: 
Socket [127.0.0.1:2181] zk retcode=-2, errno=115(Operation now in progress): 
unexpected server response: expected 0x14584484, but received 0xfffffffe
Watcher SESSION_EVENT state = CONNECTING_STATE
2013-05-21 
15:54:34,088:7030(0x7f36727cf700):ZOO_ERROR@handle_socket_error_msg@1721: 
Socket [127.0.0.1:2181] zk retcode=-4, errno=112(Host is down): failed while 
receiving a server response
2013-05-21 
15:54:34,091:7030(0x7f36736ce700):ZOO_ERROR@handle_socket_error_msg@1721: 
Socket [127.0.0.1:2181] zk retcode=-4, errno=112(Host is down): failed while 
receiving a server response
2013-05-21 
15:54:44,099:7030(0x7f36727cf700):ZOO_ERROR@handle_socket_error_msg@1721: 
Socket [127.0.0.1:2181] zk retcode=-4, errno=112(Host is down): failed while 
receiving a server response
2013-05-21 
15:54:44,099:7030(0x7f36736ce700):ZOO_ERROR@handle_socket_error_msg@1721: 
Socket [127.0.0.1:2181] zk retcode=-4, errno=112(Host is down): failed while 
receiving a server response

The java client worked perfectly fine and the zkCli.sh works fine as well.
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <zookeeper.h>
#include <proto.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <sys/time.h>
#include <unistd.h>
#include <sys/select.h>

#include <time.h>
#include <errno.h>
#include <assert.h>


#define _LL_CAST_ (long long)

static zhandle_t *zh;
static clientid_t myid;
static const char *clientIdFile = 0;
struct timeval startTime;
static char cmd[1024];
static int batchMode=0;

static int to_send=0;
static int sent=0;
static int recvd=0;

static int shutdownThisThing=0;

static const char *hostPort;
static int verbose = 0;

static __attribute__ ((unused)) void 
printProfileInfo(struct timeval start, struct timeval end, int thres,
                 const char* msg)
{
  int delay=(end.tv_sec*1000+end.tv_usec/1000)-
    (start.tv_sec*1000+start.tv_usec/1000);
  if(delay>thres)
    fprintf(stderr,"%s: execution time=%dms\n",msg,delay);
}

static const char* state2String(int state){
  if (state == 0)
    return "CLOSED_STATE";
  if (state == ZOO_CONNECTING_STATE)
    return "CONNECTING_STATE";
  if (state == ZOO_ASSOCIATING_STATE)
    return "ASSOCIATING_STATE";
  if (state == ZOO_CONNECTED_STATE)
    return "CONNECTED_STATE";
  if (state == ZOO_EXPIRED_SESSION_STATE)
    return "EXPIRED_SESSION_STATE";
  if (state == ZOO_AUTH_FAILED_STATE)
    return "AUTH_FAILED_STATE";

  return "INVALID_STATE";
}

static const char* type2String(int state){
  if (state == ZOO_CREATED_EVENT)
    return "CREATED_EVENT";
  if (state == ZOO_DELETED_EVENT)
    return "DELETED_EVENT";
  if (state == ZOO_CHANGED_EVENT)
    return "CHANGED_EVENT";
  if (state == ZOO_CHILD_EVENT)
    return "CHILD_EVENT";
  if (state == ZOO_SESSION_EVENT)
    return "SESSION_EVENT";
  if (state == ZOO_NOTWATCHING_EVENT)
    return "NOTWATCHING_EVENT";

  return "UNKNOWN_EVENT_TYPE";
}

void watcher(zhandle_t *zzh, int type, int state, const char *path,
             void* context)
{
    /* Be careful using zh here rather than zzh - as this may be mt code
     * the client lib may call the watcher before zookeeper_init returns */

    fprintf(stderr, "Watcher %s state = %s", type2String(type), 
state2String(state));
    if (path && strlen(path) > 0) {
      fprintf(stderr, " for path %s", path);
    }
    fprintf(stderr, "\n");

    if (type == ZOO_SESSION_EVENT) {
        if (state == ZOO_CONNECTED_STATE) {
            fprintf(stderr, "Connected ...\n");
        } else if (state == ZOO_AUTH_FAILED_STATE) {
            fprintf(stderr, "Authentication failure. Shutting down...\n");
            zookeeper_close(zzh);
            shutdownThisThing=1;
            zh=0;
        } else if (state == ZOO_EXPIRED_SESSION_STATE) {
            fprintf(stderr, "Session expired. Shutting down...\n");
            zookeeper_close(zzh);
            shutdownThisThing=1;
            zh=0;
        }
    }
}

int main(int argc, char **argv) {
    fd_set rfds, wfds, efds;
    int processed=0;
    char buffer[4096];
    char p[2048];
    int bufoff = 0;
    FILE *fh;

    if (argc < 2) {
        fprintf(stderr,
                "USAGE %s hostport\n", argv[0]);
        return 2;
    }
    verbose = 0;
    zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
    zoo_deterministic_conn_order(1); // enable deterministic order
    hostPort = argv[1];
    zh = zookeeper_init(hostPort, watcher, 30000, &myid, 0, 0);
    if (!zh) {
        return errno;
    }

    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    FD_ZERO(&efds);
    while (!shutdownThisThing) {
        int fd;
        int interest;
        int events;
        struct timeval tv;
        int rc;
        zookeeper_interest(zh, &fd, &interest, &tv);
        if (fd != -1) {
            if (interest&ZOOKEEPER_READ) {
                FD_SET(fd, &rfds);
            } else {
                FD_CLR(fd, &rfds);
            }
            if (interest&ZOOKEEPER_WRITE) {
                FD_SET(fd, &wfds);
            } else {
                FD_CLR(fd, &wfds);
            }
        } else {
            fd = 0;
        }
        FD_SET(0, &rfds);
        rc = select(fd+1, &rfds, &wfds, &efds, &tv);
        events = 0;
        if (rc > 0) {
            if (FD_ISSET(fd, &rfds)) {
                events |= ZOOKEEPER_READ;
            }
            if (FD_ISSET(fd, &wfds)) {
                events |= ZOOKEEPER_WRITE;
            }
        }
        zookeeper_process(zh, events);
    }
    zookeeper_close(zh);
    return 0;
}

Reply via email to