1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "unit_sysmgmt.h"

#include "gate/system/management.h"
#include "gate/results.h"
#include "gate/threading.h"
#include "gate/times.h"
#include "gate/utilities.h"

#if !defined(GATE_SYS_DOS)

static gate_bool_t print_management_object(gate_management_object_t const* obj, void* param)
{
    gate_stream_t* stream = (gate_stream_t*)param;
    gate_map_iterator_t iter = NULL;
    gate_string_t const* key = NULL;
    gate_value_t const* val = NULL;
    gate_string_t str_val = GATE_STRING_INIT_EMPTY;

    gate_stream_print(stream,
        GATE_PRINT_CSTR, "Path: ", GATE_PRINT_STRING, &obj->path, GATE_PRINT_NEWLINE,
        GATE_PRINT_CSTR, "Name: ", GATE_PRINT_STRING, &obj->name, GATE_PRINT_NEWLINE,
        GATE_PRINT_CSTR, "Type: ", GATE_PRINT_STRING, &obj->type, GATE_PRINT_NEWLINE,
        GATE_PRINT_UI32, gate_map_count(&obj->properties), GATE_PRINT_CSTR, " Properties:", GATE_PRINT_NEWLINE,
        GATE_PRINT_END);

    iter = gate_map_first(&obj->properties);<--- Variable 'iter' is reassigned a value before the old one has been used.
    for (iter = gate_map_first(&obj->properties); gate_map_iterator_valid(iter); iter = gate_map_iterator_next(iter))<--- Variable 'iter' is reassigned a value before the old one has been used.
    {
        key = (gate_string_t const*)gate_map_iterator_key(iter);
        val = (gate_value_t const*)gate_map_iterator_value(iter);
        if (val)
        {
            gate_value_load_string(val->value_type, &val->content.ptr_value, &str_val);
        }
        else
        {
            gate_string_create_empty(&str_val);
        }

        gate_stream_print(stream,
            GATE_PRINT_CSTR, "    ", GATE_PRINT_STRING, key, GATE_PRINT_CSTR, " = ", GATE_PRINT_STRING, &str_val, GATE_PRINT_NEWLINE,
            GATE_PRINT_END);
        gate_string_release(&str_val);
    }

    gate_stream_print(stream,
        GATE_PRINT_CSTR, "----------------------------------------------------------------------", GATE_PRINT_NEWLINE,
        GATE_PRINT_END);

    return true;
}

static gate_bool_t print_management_path(gate_management_object_t const* obj, void* param)
{
    gate_stream_t* stream = (gate_stream_t*)param;
    gate_map_iterator_t iter = NULL;
    gate_string_t const* key = NULL;
    gate_value_t const* val = NULL;
    gate_string_t str_val = GATE_STRING_INIT_EMPTY;

    gate_stream_print(stream, GATE_PRINT_STRING, &obj->path, GATE_PRINT_NEWLINE,
        GATE_PRINT_END);
    return true;
}



/************************
 *  sysmgmg get-object  *
 ************************/

struct
{
    gate_string_t   path;
    gate_string_t   host;
    gate_string_t	name_space;
    gate_string_t   subsystem;
    gate_bool_t		path_only;
} sysmgmt_getobject_params = GATE_INIT_EMPTY;

static gatecli_paramter_t unit_sysmgmt_getobject_params[] = {
    { "--path",		    GATE_TYPE_STRING,		&sysmgmt_getobject_params.path, false, "Path of object" },
    { "--host",         GATE_TYPE_STRING,       &sysmgmt_getobject_params.host, true, "Name or address of host to connect to"},
    { "--namespace",	GATE_TYPE_STRING,		&sysmgmt_getobject_params.name_space, true, "Namespace where query is executed"},
    { "--subsystem",    GATE_TYPE_STRING,       &sysmgmt_getobject_params.subsystem, true, "Subsystem to connect"},
    { "--shortinfo",	GATE_TYPE_BOOL,			&sysmgmt_getobject_params.path_only, true, "Short info output, print only path"}
};


static gate_result_t unit_sysmgmt_getobject(gate_console_t* con, char const* const* params, gate_size_t paramcount, gate_uint32_t flags)
{
    gate_result_t ret;
    gate_stream_t* stream = (gate_stream_t*)con;

    gate_management_t mgmt = NULL;
    gate_management_object_t obj = GATE_INIT_EMPTY;
    gate_management_callback_t cb = &print_management_object;

    do
    {
        if (sysmgmt_getobject_params.path_only)
        {
            cb = &print_management_path;
        }

        ret = gate_management_open(&sysmgmt_getobject_params.host, &sysmgmt_getobject_params.subsystem,
            &sysmgmt_getobject_params.name_space, 0, &mgmt);
        GATE_BREAK_IF_FAILED(ret);

        ret = gate_management_get_object(mgmt, &sysmgmt_getobject_params.path, &obj);
        GATE_BREAK_IF_FAILED(ret);

        cb(&obj, stream);
        gate_management_object_release(&obj);
    } while (0);

    if (mgmt != NULL)
    {
        gate_management_close(mgmt);
        mgmt = NULL;
    }

    return ret;
}


/***************************
 *  sysmgmg query-objects  *
 **************************/

struct
{
    gate_string_t   path;
    gate_string_t	query;
    gate_string_t   host;
    gate_string_t	name_space;
    gate_string_t   subsystem;
    gate_bool_t		path_only;
} sysmgmt_queryobjects_params = GATE_INIT_EMPTY;

static gatecli_paramter_t unit_sysmgmt_queryobjects_params[] = {
    { "--query",		GATE_TYPE_STRING,	&sysmgmt_queryobjects_params.query, false, "Query to be executed" },
    { "--path",			GATE_TYPE_STRING,	&sysmgmt_queryobjects_params.path, false, "Path of object" },
    { "--host",			GATE_TYPE_STRING,	&sysmgmt_queryobjects_params.host, true, "Name or address of host to connect to"},
    { "--namespace",	GATE_TYPE_STRING,	&sysmgmt_queryobjects_params.name_space, true, "Namespace where query is executed"},
    { "--subsystem",	GATE_TYPE_STRING,	&sysmgmt_queryobjects_params.subsystem, true, "Subsystem to connect"},
    { "--shortinfo",	GATE_TYPE_BOOL,		&sysmgmt_queryobjects_params.path_only, true, "Short info output, print only path"}
};

static gate_result_t unit_sysmgmt_queryobjects(gate_console_t* con, char const* const* params, gate_size_t paramcount, gate_uint32_t flags)
{
    gate_result_t ret;
    gate_stream_t* stream = (gate_stream_t*)con;

    gate_management_t mgmt = NULL;
    gate_management_callback_t cb = &print_management_object;

    do
    {
        if (sysmgmt_queryobjects_params.path_only)
        {
            cb = &print_management_path;
        }

        ret = gate_management_open(&sysmgmt_queryobjects_params.host, &sysmgmt_queryobjects_params.subsystem,
            &sysmgmt_queryobjects_params.name_space, 0, &mgmt);
        GATE_BREAK_IF_FAILED(ret);

        ret = gate_management_query(mgmt, &sysmgmt_queryobjects_params.path, &sysmgmt_queryobjects_params.query,
            cb, stream);
        GATE_BREAK_IF_FAILED(ret);
    } while (0);

    if (mgmt != NULL)
    {
        gate_management_close(mgmt);
        mgmt = NULL;
    }

    return ret;
}




static gatecli_function_t sysmgmt_functions[] =
{
    GATECLI_FUNCTION_PARAMS("get-object", &unit_sysmgmt_getobject, unit_sysmgmt_getobject_params, "Accesses an object by its path and returns its properties"),
    GATECLI_FUNCTION_PARAMS("query-objects", &unit_sysmgmt_queryobjects, unit_sysmgmt_queryobjects_params, "Executes a query to find specific management objects")
};


static gatecli_unit_t global_unit_sysmgmt =
{
    "sysmgmt",
    "System management functions",
    &sysmgmt_functions[0],
    sizeof(sysmgmt_functions) / sizeof(sysmgmt_functions[0])
};

gatecli_unit_t* unit_sysmgmt()
{
    return &global_unit_sysmgmt;
}

#endif