From b8609f826ce04f84c2d0df76674e938ad7112a6b Mon Sep 17 00:00:00 2001 From: Noah Watkins Date: Sat, 30 Aug 2014 10:30:16 -0700 Subject: [PATCH] lib: fix parse commnad line arguments Adds a placeholder string at argv[0] position because Ceph will start parsing the provided arguments at position argv[1] (skipping what would normally be the executable name). Signed-off-by: Noah Watkins --- conn.go | 14 +++++++++++--- rados_test.go | 11 ++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/conn.go b/conn.go index e0a8637..61dcae0 100644 --- a/conn.go +++ b/conn.go @@ -179,12 +179,20 @@ func (c *Conn) GetClusterStats() (stat ClusterStat, err error) { // ParseCmdLineArgs configures the connection from command line arguments. func (c *Conn) ParseCmdLineArgs(args []string) error { - argc := C.int(len(args)) + // add an empty element 0 -- Ceph treats the array as the actual contents + // of argv and skips the first element (the executable name) + argc := C.int(len(args) + 1) argv := make([]*C.char, argc) + + // make the first element a string just in case it is ever examined + argv[0] = C.CString("placeholder") + defer C.free(unsafe.Pointer(argv[0])) + for i, arg := range args { - argv[i] = C.CString(arg) - defer C.free(unsafe.Pointer(argv[i])) + argv[i+1] = C.CString(arg) + defer C.free(unsafe.Pointer(argv[i+1])) } + ret := C.rados_conf_parse_argv(c.cluster, argc, &argv[0]) if ret < 0 { return RadosError(int(ret)) diff --git a/rados_test.go b/rados_test.go index dafcc53..9eb329c 100644 --- a/rados_test.go +++ b/rados_test.go @@ -56,16 +56,17 @@ func TestParseDefaultConfigEnv(t *testing.T) { func TestParseCmdLineArgs(t *testing.T) { conn, _ := rados.NewConn() + conn.ReadDefaultConfigFile() - log_file_val, _ := conn.GetConfigOption("mon_host") - assert.NotEqual(t, log_file_val, "127.0.0.1") + mon_host_val, _ := conn.GetConfigOption("mon_host") + assert.NotEqual(t, mon_host_val, "1.1.1.1") - args := []string{ "--mon-host 127.0.0.1" } + args := []string{ "--mon-host", "1.1.1.1" } err := conn.ParseCmdLineArgs(args) assert.NoError(t, err) - log_file_val, _ = conn.GetConfigOption("mon_host") - assert.Equal(t, log_file_val, "127.0.0.1") + mon_host_val, _ = conn.GetConfigOption("mon_host") + assert.Equal(t, mon_host_val, "1.1.1.1") } func TestGetClusterStats(t *testing.T) {