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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
/*
* Copyright (C) 2012 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: Radek Novacek <rnovacek@redhat.com>
*/
#include "power.h"
#include <stdlib.h>
#include <assert.h>
#include <glib.h>
#include "LMI_AssociatedPowerManagementService.h"
#include "LMI_ConcreteJob.h"
#ifdef HAS_UPOWER
#include <upower.h>
#endif
struct _Power {
unsigned int instances;
unsigned short requestedPowerState;
unsigned short transitioningToPowerState;
const CMPIBroker *broker;
CMPI_MUTEX_TYPE mutex;
GList *jobs; // list of PowerStateChangeJob
#ifdef HAS_UPOWER
UpClient *up;
#endif
};
#define MUTEX_LOCK(power) power->broker->xft->lockMutex(power->mutex)
#define MUTEX_UNLOCK(power) power->broker->xft->unlockMutex(power->mutex)
struct _PowerStateChangeJob {
const CMPIBroker *broker;
Power *power;
unsigned short requestedPowerState;
unsigned short jobState;
int timeOfLastChange;
int timeBeforeRemoval;
int cancelled;
int superseded; // There is another job that overrides this job
char *error;
CMPI_THREAD_TYPE thread;
CMPI_MUTEX_TYPE mutex;
};
Power *_power = NULL;
// This is just for debugging purposes, remove later
#include <execinfo.h>
#include <signal.h>
#include <unistd.h>
void print_backtrace(int signal)
{
fprintf(stderr, "BackTrace\n");
void *buffer[32];
int count = backtrace(buffer, 32);
fprintf(stderr, "Size: %d\n", count);
backtrace_symbols_fd(buffer, count, stderr->_fileno);
fprintf(stderr, "Segfault detected, process id: %d. Entering infinite loop.\n", getpid());
volatile int end = 0;
while (!end) {
sleep(1);
}
}
Power *power_new(const CMPIBroker *_cb)
{
signal(SIGSEGV, print_backtrace);
fprintf(stderr, "BackTrace handler registered\n");
Power *power = malloc(sizeof(Power));
power->broker = _cb;
power->instances = 0;
power->requestedPowerState = LMI_AssociatedPowerManagementService_RequestedPowerState_Unknown;
power->transitioningToPowerState = LMI_AssociatedPowerManagementService_TransitioningToPowerState_No_Change;
power->mutex = _cb->xft->newMutex(0);
power->jobs = NULL;
#ifdef HAS_UPOWER
g_type_init();
power->up = up_client_new();
#endif
return power;
}
void power_destroy(Power *power)
{
#ifdef HAS_UPOWER
free(power->up);
#endif
}
Power *power_ref(const CMPIBroker *_cb)
{
if (_power == NULL) {
_power = power_new(_cb);
}
MUTEX_LOCK(_power);
_power->instances++;
MUTEX_UNLOCK(_power);
return _power;
}
void power_unref(Power *power)
{
MUTEX_LOCK(power);
power->instances--;
MUTEX_UNLOCK(power);
if (power->instances == 0) {
power_destroy(power);
power = NULL;
_power = NULL;
}
}
unsigned short power_requested_power_state(Power *power)
{
return power->requestedPowerState;
}
unsigned short power_transitioning_to_power_state(Power *power)
{
return power->transitioningToPowerState;
}
void *state_change_thread(void *data)
{
PowerStateChangeJob *powerStateChangeJob = data;
MUTEX_LOCK(powerStateChangeJob);
powerStateChangeJob->jobState = LMI_ConcreteJob_JobState_Running;
powerStateChangeJob->timeOfLastChange = time(NULL);
MUTEX_UNLOCK(powerStateChangeJob);
// Check if the job was cancelled
if (powerStateChangeJob->cancelled) {
MUTEX_LOCK(powerStateChangeJob);
powerStateChangeJob->jobState = LMI_ConcreteJob_JobState_Terminated;
powerStateChangeJob->timeOfLastChange = time(NULL);
MUTEX_UNLOCK(powerStateChangeJob);
if (!powerStateChangeJob->superseded) {
// There is no job that replaced this job
MUTEX_LOCK(powerStateChangeJob->power);
powerStateChangeJob->power->transitioningToPowerState = LMI_AssociatedPowerManagementService_TransitioningToPowerState_No_Change;
MUTEX_UNLOCK(powerStateChangeJob->power);
}
fprintf(stderr, "state_change_thread cancelled\n");
return NULL;
}
// Execute the job
#ifdef HAS_UPOWER
GError *error = NULL;
#endif
int succeeded = 0;
switch (powerStateChangeJob->requestedPowerState) {
case LMI_AssociatedPowerManagementService_PowerState_Sleep__Deep:
// Sleep
#ifdef HAS_UPOWER
succeeded = up_client_suspend_sync(powerStateChangeJob->power->up, NULL, &error);
#else
succeeded = system("pm-suspend") == 0;
#endif
break;
case LMI_AssociatedPowerManagementService_PowerState_Power_Cycle_Off___Soft:
// Reboot (without shutting down programs)
#ifdef HAS_SYSTEMCTL
succeeded = system("systemctl --force reboot &") == 0;
#else
succeeded = system("reboot --force &") == 0;
#endif
break;
case LMI_AssociatedPowerManagementService_PowerState_Hibernate_Off___Soft:
// Hibernate
#ifdef HAS_UPOWER
succeeded = up_client_hibernate_sync(powerStateChangeJob->power->up, NULL, &error);
#else
succeeded = system("pm-hibernate") == 0;
#endif
break;
case LMI_AssociatedPowerManagementService_PowerState_Off___Soft:
// Poweroff (without shutting down programs)
#ifdef HAS_SYSTEMCTL
succeeded = system("systemctl --force poweroff &") == 0;
#else
succeeded = system("shutdown --halt now &") == 0;
#endif
break;
case LMI_AssociatedPowerManagementService_PowerState_Off___Soft_Graceful:
// Poweroff (shut down programs first)
#ifdef HAS_SYSTEMCTL
succeeded = system("systemctl poweroff &") == 0;
#else
succeeded = system("shutdown --poweroff now &") == 0;
#endif
break;
case LMI_AssociatedPowerManagementService_PowerState_Power_Cycle_Off___Soft_Graceful:
// Reboot (shut down programs first)
#ifdef HAS_SYSTEMCTL
succeeded = system("systemctl reboot &") == 0;
#else
succeeded = system("shutdown --reboot now &") == 0;
#endif
break;
}
MUTEX_LOCK(powerStateChangeJob->power);
powerStateChangeJob->power->transitioningToPowerState = LMI_AssociatedPowerManagementService_TransitioningToPowerState_No_Change;
MUTEX_UNLOCK(powerStateChangeJob->power);
MUTEX_LOCK(powerStateChangeJob);
if (succeeded) {
powerStateChangeJob->jobState = LMI_ConcreteJob_JobState_Completed;
} else {
powerStateChangeJob->jobState = LMI_ConcreteJob_JobState_Exception;
#ifdef HAS_UPOWER
if (error != NULL) {
powerStateChangeJob->error = error->message;
}
#endif
}
powerStateChangeJob->timeOfLastChange = time(NULL);
MUTEX_UNLOCK(powerStateChangeJob);
fprintf(stderr, "state_change_thread finished\n");
return NULL;
}
int power_request_power_state(Power *power, unsigned short state)
{
int rc = CMPI_RC_OK;
int count, found = 0;
unsigned short *states = power_available_requested_power_states(power, &count);
for (int i = 0; i < count; ++i) {
if (states[i] == state) {
found = 1;
break;
}
}
free(states);
if (!found) {
fprintf(stderr, "Invalid state: %d\n", state);
return CMPI_RC_ERR_INVALID_PARAMETER;
}
PowerStateChangeJob *powerStateChangeJob = malloc(sizeof(PowerStateChangeJob));
powerStateChangeJob->broker = power->broker;
powerStateChangeJob->power = power;
powerStateChangeJob->mutex = power->broker->xft->newMutex(0);
powerStateChangeJob->requestedPowerState = state;
powerStateChangeJob->jobState = LMI_ConcreteJob_JobState_New;
powerStateChangeJob->cancelled = 0;
powerStateChangeJob->superseded = 0;
powerStateChangeJob->timeOfLastChange = time(NULL);
powerStateChangeJob->timeBeforeRemoval = 300;
powerStateChangeJob->error = NULL;
MUTEX_LOCK(power);
power->requestedPowerState = state;
power->transitioningToPowerState = state;
PowerStateChangeJob *job;
GList *plist = power->jobs;
while (plist) {
job = plist->data;
MUTEX_LOCK(job);
if (job->jobState != LMI_ConcreteJob_JobState_Suspended &&
job->jobState != LMI_ConcreteJob_JobState_Killed &&
job->jobState != LMI_ConcreteJob_JobState_Terminated) {
job->cancelled = 1;
job->superseded = 1;
job->jobState = LMI_ConcreteJob_JobState_Shutting_Down;
job->timeOfLastChange = time(NULL);
}
MUTEX_UNLOCK(job);
plist = g_list_next(plist);
}
powerStateChangeJob->thread = power->broker->xft->newThread(state_change_thread, powerStateChangeJob, 1);
power->jobs = g_list_append(power->jobs, powerStateChangeJob);
MUTEX_UNLOCK(power);
fprintf(stderr, "State change thread started\n");
return rc;
}
unsigned short *power_available_requested_power_states(Power *power, int *count)
{
unsigned short *list = malloc(17 * sizeof(unsigned short));
int i = 0;
/* 1 Other
* LMI_AssociatedPowerManagementService_PowerState_Other
*/
/* 2 On
* corresponding to ACPI state G0 or S0 or D0.
*
* Bring system to full On from any state (Sleep, Hibernate, Off)
*
* LMI_AssociatedPowerManagementService_PowerState_On
*/
// not supported
/* 3 Sleep - Light
* corresponding to ACPI state G1, S1/S2, or D1.
*
* Standby
*
* LMI_AssociatedPowerManagementService_PowerState_Sleep___Light
*/
// not supported
/* 4 Sleep - Deep
* corresponding to ACPI state G1, S3, or D2.
*
* Suspend
*
* LMI_AssociatedPowerManagementService_PowerState_Sleep__Deep
*/
// Sleep
#ifdef HAS_UPOWER
if (up_client_get_can_suspend(power->up)) {
list[i++] = LMI_AssociatedPowerManagementService_PowerState_Sleep__Deep;
}
#else
if (system("pm-is-supported --suspend") == 0) {
list[i++] = LMI_AssociatedPowerManagementService_PowerState_Sleep__Deep;
}
#endif
/* 5 Power Cycle (Off - Soft)
* corresponding to ACPI state G2, S5, or D3, but where the managed
* element is set to return to power state "On" at a pre-determined time.
*
* Reset system without removing power
*
* LMI_AssociatedPowerManagementService_PowerState_Power_Cycle_Off___Soft
*/
// Reboot (without shutting down programs)
list[i++] = LMI_AssociatedPowerManagementService_PowerState_Power_Cycle_Off___Soft;
/* 6 Off - Hard
* corresponding to ACPI state G3, S5, or D3.
*
* Power Off performed through mechanical means like unplugging
* power cable or UPS On
*
* LMI_AssociatedPowerManagementService_PowerState_Off___Hard
*/
/* 7 Hibernate (Off - Soft)
* corresponding to ACPI state S4, where the state of the managed element
* is preserved and will be recovered upon powering on.
*
* System context and OS image written to non-volatile storage;
* system and devices powered off
*
* LMI_AssociatedPowerManagementService_PowerState_Hibernate_Off___Soft
*/
// Hibernate
#ifdef HAS_UPOWER
if (up_client_get_can_hibernate(power->up)) {
list[i++] = LMI_AssociatedPowerManagementService_PowerState_Hibernate_Off___Soft;
}
#else
if (system("pm-is-supported --hibernate") == 0) {
list[i++] = LMI_AssociatedPowerManagementService_PowerState_Hibernate_Off___Soft;
}
#endif
/* 8 Off - Soft
* corresponding to ACPI state G2, S5, or D3.
*
* System power off but auxiliary or flea power may be available
*
* LMI_AssociatedPowerManagementService_PowerState_Off___Soft
*/
// Poweroff (without shutting down programs)
list[i++] = LMI_AssociatedPowerManagementService_PowerState_Off___Soft;
/* 9 Power Cycle (Off-Hard)
* corresponds to the managed element reaching the ACPI state G3
* followed by ACPI state S0.
*
* Equivalent to Off–Hard followed by On
*
* LMI_AssociatedPowerManagementService_PowerState_Power_Cycle_Off_Hard
*/
// not implemented
/* 10 Master Bus Reset
* corresponds to the system reaching ACPI state S5 followed by ACPI
* state S0. This is used to represent system master bus reset.
*
* Hardware reset
*
* LMI_AssociatedPowerManagementService_PowerState_Master_Bus_Reset
*/
// not implemented
/* 11 Diagnostic Interrupt (NMI)
* corresponding to the system reaching ACPI state S5 followed by ACPI
* state S0. This is used to represent system non-maskable interrupt.
*
* Hardware reset
*
* LMI_AssociatedPowerManagementService_PowerState_Diagnostic_Interrupt_NMI
*/
// not implemented
/* 12 Off - Soft Graceful
* equivalent to Off Soft but preceded by a request to the managed element
* to perform an orderly shutdown.
*
* System power off but auxiliary or flea power may be available but preceded
* by a request to the managed element to perform an orderly shutdown.
*
* LMI_AssociatedPowerManagementService_PowerState_Off___Soft_Graceful
*/
// Poweroff (shut down programs first)
list[i++] = LMI_AssociatedPowerManagementService_PowerState_Off___Soft_Graceful;
/* 13 Off - Hard Graceful
* equivalent to Off Hard but preceded by a request to the managed element
* to perform an orderly shutdown.
*
* Power Off performed through mechanical means like unplugging power cable
* or UPS On but preceded by a request to the managed element to perform
* an orderly shutdown.
*
* LMI_AssociatedPowerManagementService_PowerState_Off___Hard_Graceful
*/
// not implemented
/* 14 Master Bus Rest Graceful
* equivalent to Master Bus Reset but preceded by a request to the managed
* element to perform an orderly shutdown.
*
* Hardware reset but preceded by a request to the managed element
* to perform an orderly shutdown.
*
* LMI_AssociatedPowerManagementService_PowerState_Master_Bus_Reset_Graceful
*/
// not implemented
/* 15 Power Cycle (Off - Soft Graceful)
* equivalent to Power Cycle (Off - Soft) but preceded by a request
* to the managed element to perform an orderly shutdown.
*
* Reset system without removing power but preceded by a request
* to the managed element to perform an orderly shutdown.
*
* LMI_AssociatedPowerManagementService_PowerState_Power_Cycle_Off___Soft_Graceful
*/
// Reboot (shut down programs first)
list[i++] = LMI_AssociatedPowerManagementService_PowerState_Power_Cycle_Off___Soft_Graceful;
/* 16 Power Cycle (Off - Hard Graceful)
* equivalent to Power Cycle (Off - Hard) but preceded by a request
* to the managed element to perform an orderly shutdown.
*
* Equivalent to Off–Hard followed by On but preceded by a request
* to the managed element to perform an orderly shutdown.
*
* LMI_AssociatedPowerManagementService_PowerState_Power_Cycle_Off___Hard_Graceful
*/
// not implemented
*count = i;
return list;
}
void job_free(PowerStateChangeJob *job)
{
job->broker->xft->destroyMutex(job->mutex);
}
GList *power_get_jobs(Power *power)
{
PowerStateChangeJob *powerStateChangeJob;
GList *plist = power->jobs;
while (plist) {
powerStateChangeJob = plist->data;
MUTEX_LOCK(powerStateChangeJob);
if ((powerStateChangeJob->jobState == LMI_ConcreteJob_JobState_Completed ||
powerStateChangeJob->jobState == LMI_ConcreteJob_JobState_Killed ||
powerStateChangeJob->jobState == LMI_ConcreteJob_JobState_Terminated) &&
time(NULL) - powerStateChangeJob->timeOfLastChange > powerStateChangeJob->timeBeforeRemoval) {
MUTEX_LOCK(power);
power->jobs = g_list_remove_link(power->jobs, plist);
MUTEX_UNLOCK(power);
job_free(powerStateChangeJob);
}
MUTEX_UNLOCK(powerStateChangeJob);
plist = g_list_next(plist);
}
return power->jobs;
}
unsigned short job_state(PowerStateChangeJob *state)
{
return state->jobState;
}
int job_timeOfLastChange(PowerStateChangeJob *state)
{
return state->timeOfLastChange;
}
int job_timeBeforeRemoval(PowerStateChangeJob *state)
{
return state->timeBeforeRemoval;
}
|