summaryrefslogtreecommitdiffstats
path: root/src/software-dbus/LMI_SoftwareInstallationServiceProvider.c
blob: 18648636a970e4542417d4d0f0b80eae7e865475 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * Copyright (C) 2013-2014 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Michal Minar <miminar@redhat.com>
 */

#include <konkret/konkret.h>
#include "LMI_SoftwareIdentity.h"
#include "LMI_SoftwareInstallationService.h"
#include "job_manager.h"
#include "sw-utils.h"
#include "lmi_sw_job.h"

static const CMPIBroker* _cb = NULL;

static gboolean is_power_of_two(guint32 x) {
     return ((x != 0) && !(x & (x - 1)));
}

static void LMI_SoftwareInstallationServiceInitialize(const CMPIContext *ctx)
{
    software_init(provider_name, _cb, ctx, provider_config_defaults);
}

static CMPIStatus LMI_SoftwareInstallationServiceCleanup(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    CMPIBoolean term)
{
    return software_cleanup();
}

static CMPIStatus LMI_SoftwareInstallationServiceEnumInstanceNames(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop)
{
    return KDefaultEnumerateInstanceNames(
            _cb, mi, cc, cr, cop);
}

static CMPIStatus LMI_SoftwareInstallationServiceEnumInstances(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char** properties)
{
    char instance_id[BUFLEN] = "";

    create_instance_id(LMI_SoftwareInstallationService_ClassName, NULL, instance_id,
                    BUFLEN);

    LMI_SoftwareInstallationService w;
    LMI_SoftwareInstallationService_Init(&w, _cb, KNameSpace(cop));
    LMI_SoftwareInstallationService_Set_CreationClassName(&w,
            LMI_SoftwareInstallationService_ClassName);
    LMI_SoftwareInstallationService_Set_SystemCreationClassName(&w,
            lmi_get_system_creation_class_name());
    LMI_SoftwareInstallationService_Set_SystemName(&w,
            lmi_get_system_name_safe(cc));
    LMI_SoftwareInstallationService_Set_Name(&w, instance_id);
    LMI_SoftwareInstallationService_Set_Caption(&w,
            "Software installation service for this system.");
    LMI_SoftwareInstallationService_Set_CommunicationStatus_Not_Available(&w);
    LMI_SoftwareInstallationService_Set_Description(&w,
            "Software installation service using YUM package manager.");
    LMI_SoftwareInstallationService_Set_DetailedStatus_Not_Available(&w);
    LMI_SoftwareInstallationService_Set_EnabledDefault_Not_Applicable(&w);
    LMI_SoftwareInstallationService_Set_EnabledState_Not_Applicable(&w);
    LMI_SoftwareInstallationService_Set_HealthState_OK(&w);
    LMI_SoftwareInstallationService_Set_InstanceID(&w, instance_id);
    if (LMI_SoftwareInstallationService_Init_OperationalStatus(&w, 1))
        LMI_SoftwareInstallationService_Set_OperationalStatus_OK(&w, 0);
    LMI_SoftwareInstallationService_Set_OperatingStatus_Servicing(&w);
    LMI_SoftwareInstallationService_Set_PrimaryStatus_OK(&w);
    LMI_SoftwareInstallationService_Set_RequestedState_Not_Applicable(&w);
    LMI_SoftwareInstallationService_Set_Started(&w, TRUE);
    LMI_SoftwareInstallationService_Set_TransitioningToState_Not_Applicable(&w);
    KReturnInstance(cr, w);

    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_SoftwareInstallationServiceGetInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char** properties)
{
    return KDefaultGetInstance(
        _cb, mi, cc, cr, cop, properties);
}

static CMPIStatus LMI_SoftwareInstallationServiceCreateInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const CMPIInstance* ci)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

static CMPIStatus LMI_SoftwareInstallationServiceModifyInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const CMPIInstance* ci,
    const char** properties)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

static CMPIStatus LMI_SoftwareInstallationServiceDeleteInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

static CMPIStatus LMI_SoftwareInstallationServiceExecQuery(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char* lang,
    const char* query)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

CMInstanceMIStub(
    LMI_SoftwareInstallationService,
    LMI_SoftwareInstallationService,
    _cb,
    LMI_SoftwareInstallationServiceInitialize(ctx))

static CMPIStatus LMI_SoftwareInstallationServiceMethodCleanup(
    CMPIMethodMI* mi,
    const CMPIContext* cc,
    CMPIBoolean term)
{
    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_SoftwareInstallationServiceInvokeMethod(
    CMPIMethodMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char* meth,
    const CMPIArgs* in,
    CMPIArgs* out)
{
    return LMI_SoftwareInstallationService_DispatchMethod(
        _cb, mi, cc, cr, cop, meth, in, out);
}

CMMethodMIStub(
    LMI_SoftwareInstallationService,
    LMI_SoftwareInstallationService,
    _cb,
    LMI_SoftwareInstallationServiceInitialize(ctx))

KUint32 LMI_SoftwareInstallationService_RequestStateChange(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    const KUint16* RequestedState,
    KRef* Job,
    const KDateTime* TimeoutPeriod,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_SoftwareInstallationService_StartService(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_SoftwareInstallationService_StopService(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_SoftwareInstallationService_ChangeAffectedElementsAssignedSequence(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    const KRefA* ManagedElements,
    const KUint16A* AssignedSequence,
    KRef* Job,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_SoftwareInstallationService_CheckSoftwareIdentity(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    const KRef* Source,
    const KRef* Target,
    const KRef* Collection,
    KUint16A* InstallCharacteristics,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KUint32_Set(&result, INSTALL_METHOD_RESULT_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_SoftwareInstallationService_InstallFromSoftwareIdentity(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    KRef* Job,
    const KUint16A* InstallOptions,
    const KStringA* InstallOptionsValues,
    const KRef* Source,
    const KRef* Target,
    const KRef* Collection,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;
    KUint16 opt;
    guint32 op = 0;
    LmiJob *job = NULL;
    GVariant *variant;
    gchar *jobid;
    CMPIObjectPath *cop;
    LMI_SoftwareIdentityRef pkgop;

    if (!KHasValue(Source)) {
        lmi_error("Missing Source argument!");
        KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
        return result;
    }

    if (KHasValue(InstallOptions)) {
        for (size_t i=0; i < InstallOptions->count; ++i) {
            opt = KUint16A_Get(InstallOptions, i);
            switch (opt.value) {
                case INSTALL_OPTION_INSTALL:
                    op |= INSTALLATION_OPERATION_INSTALL;
                    break;
                case INSTALL_OPTION_UPDATE:
                    op |= INSTALLATION_OPERATION_UPDATE;
                    break;
                case INSTALL_OPTION_UNINSTALL:
                    op |= INSTALLATION_OPERATION_REMOVE;
                    break;
                case INSTALL_OPTION_FORCE_INSTALLATION:
                    op |= INSTALLATION_OPERATION_FORCE;
                    break;
                case INSTALL_OPTION_REPAIR:
                    op |= INSTALLATION_OPERATION_REPAIR;
                    break;
                default:
                    lmi_error("Given unsupported install option %u!", opt);
                    KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
                    return result;
            }
        }
        if (!is_power_of_two(op & INSTALLATION_OPERATION_EXCLUSIVE_GROUP)) {
            lmi_error("Conflicting install options given!"
                   " Choose one of Install, Remove, Update.");
            KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
            return result;
        }
    }
    if (!(op & INSTALLATION_OPERATION_EXCLUSIVE_GROUP)) {
        op |= INSTALLATION_OPERATION_INSTALL;
    }

    if (KHasValue(InstallOptionsValues) && InstallOptionsValues->count) {
        if (InstallOptionsValues->count > InstallOptions->count) {
            lmi_error("InstallOptionsValues can not have more"
                   " values than InstallOptions!");
            KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
            return result;
        }
        for (size_t i=0; i < InstallOptionsValues->count; ++i) {
            if (KHasValue(KStringA_Get(InstallOptionsValues, i))) {
                lmi_error("None of supported install option can have associated"
                        " install option value!");
                KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
                return result;
            }
        }
    }

    if (KHasValue(Target)) {
        if (!lmi_check_computer_system(context, Target->value)) {
            lmi_error("Target does not match this system!");
            KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
            return result;
        }
    }
    if (KHasValue(Collection)) {
        if (!check_system_software_collection(cb, Collection->value)) {
            lmi_error("Invalid system software collection!");
            KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
            return result;
        }
    }
    if (KHasValue(Target) && KHasValue(Collection)) {
        lmi_error("Only one of Target and Collection parameters cen be given"
                " at the same time!");
        KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
        return result;
    }

    if (!KHasValue(Target) && !KHasValue(Collection)) {
        lmi_error("Either Target or Collection parameter must be specified!");
        KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
        return result;
    }

    *status = LMI_SoftwareIdentityRef_InitFromObjectPath(
            &pkgop, cb, Source->value);
    if (status->rc) {
        lmi_error("Invalid Source parameter!");
        KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
        return result;
    }
    if (g_ascii_strncasecmp(pkgop.InstanceID.chars,
                SW_IDENTITY_INSTANCE_ID_PREFIX,
                SW_IDENTITY_INSTANCE_ID_PREFIX_LEN) != 0)
    {
        lmi_error("Invalid InstanceID of Source!");
        KUint32_Set(&result, INSTALL_METHOD_RESULT_INVALID_PARAMETER);
        return result;
    }

    if ((job = jobmgr_new_job(LMI_TYPE_SW_INSTALLATION_JOB)) == NULL) {
        lmi_error("Memory allocation failed!");
        KUint32_Set(&result, INSTALL_METHOD_RESULT_NOT_ENOUGH_MEMORY);
        return result;
    }

    lmi_job_set_method_name(job, "InstallFromSoftwareIdentity");

    variant = g_variant_new_boolean(KHasValue(Target));
    lmi_job_set_in_param(job, IN_PARAM_TARGET_NAME, variant);
    variant = g_variant_new_boolean(KHasValue(Collection));
    lmi_job_set_in_param(job, IN_PARAM_COLLECTION_NAME, variant);
    variant = g_variant_new_string(pkgop.InstanceID.chars
            + SW_IDENTITY_INSTANCE_ID_PREFIX_LEN);
    lmi_job_set_in_param(job, IN_PARAM_SOURCE_NAME, variant);
    variant = g_variant_new_uint32(op);
    lmi_job_set_in_param(job, IN_PARAM_INSTALL_OPTIONS_NAME, variant);

    if ((*status = jobmgr_run_job(job)).rc) {
        lmi_error("Failed to run job on SoftwareIdentity \"%s\"!",
                pkgop.InstanceID.chars);
        KUint32_Set(&result, INSTALL_METHOD_RESULT_UNSPECIFIED_ERROR);
        return result;
    }

    if ((*status = jobmgr_job_to_cim_op(job, &cop)).rc)
        return result;

    KRef_SetObjectPath(Job, cop);
    jobid = lmi_job_get_jobid(job);
    lmi_info("Installation job \"%s\" started on SoftwareIdentity \"%s\".",
            jobid, pkgop.InstanceID.chars);
    KUint32_Set(&result, INSTALL_METHOD_RESULT_JOB_STARTED);
    g_free(jobid);

    return result;
}

KUint32 LMI_SoftwareInstallationService_InstallFromByteStream(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    KRef* Job,
    const KUint8A* Image,
    const KRef* Target,
    const KUint16A* InstallOptions,
    const KStringA* InstallOptionsValues,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KUint32_Set(&result, INSTALL_METHOD_RESULT_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_SoftwareInstallationService_InstallFromURI(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    KRef* Job,
    const KString* URI,
    const KRef* Target,
    const KUint16A* InstallOptions,
    const KStringA* InstallOptionsValues,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_SoftwareInstallationService_VerifyInstalledIdentity(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    const KRef* Source,
    const KRef* Target,
    KRef* Job,
    KRefA* Failed,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_SoftwareInstallationService_FindIdentity(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_SoftwareInstallationServiceRef* self,
    const KString* Name,
    const KUint32* Epoch,
    const KString* Version,
    const KString* Release,
    const KString* Architecture,
    const KRef* Repository,
    const KBoolean* AllowDuplicates,
    const KBoolean* ExactMatch,
    KRefA* Matches,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KONKRET_REGISTRATION(
    "root/cimv2",
    "LMI_SoftwareInstallationService",
    "LMI_SoftwareInstallationService",
    "instance method")