1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <errno.h>
|
---|
30 | #include <ipc/sysman.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 |
|
---|
33 | #include "configuration.h"
|
---|
34 | #include "connection_ctl.h"
|
---|
35 | #include "job.h"
|
---|
36 | #include "log.h"
|
---|
37 | #include "sysman.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | static ipc_callid_t *box_callid(ipc_callid_t iid)
|
---|
41 | {
|
---|
42 | ipc_callid_t *result = malloc(sizeof(ipc_callid_t));
|
---|
43 | if (result) {
|
---|
44 | *result = iid;
|
---|
45 | }
|
---|
46 | return result;
|
---|
47 | }
|
---|
48 |
|
---|
49 | static void answer_callback(void *object, void *arg)
|
---|
50 | {
|
---|
51 | job_t *job = object;
|
---|
52 | assert(job->state == JOB_FINISHED);
|
---|
53 | assert(job->retval != JOB_UNDEFINED_);
|
---|
54 |
|
---|
55 | ipc_callid_t *iid_ptr = arg;
|
---|
56 | // TODO use descriptive return value (probably refactor job retval)
|
---|
57 | sysarg_t retval = (job->retval == JOB_OK) ? EOK : EIO;
|
---|
58 | async_answer_0(*iid_ptr, retval);
|
---|
59 | free(iid_ptr);
|
---|
60 | job_del_ref(&job);
|
---|
61 | }
|
---|
62 |
|
---|
63 | static void sysman_unit_start(ipc_callid_t iid, ipc_call_t *icall)
|
---|
64 | {
|
---|
65 | char *unit_name = NULL;
|
---|
66 | sysarg_t retval;
|
---|
67 |
|
---|
68 | int rc = async_data_write_accept((void **) &unit_name, true,
|
---|
69 | 0, 0, 0, NULL);
|
---|
70 | if (rc != EOK) {
|
---|
71 | retval = rc;
|
---|
72 | goto answer;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int flags = IPC_GET_ARG1(*icall);
|
---|
76 | sysman_log(LVL_DEBUG2, "%s(%s, %x)", __func__, unit_name, flags);
|
---|
77 |
|
---|
78 | unit_t *unit = configuration_find_unit_by_name(unit_name);
|
---|
79 | if (unit == NULL) {
|
---|
80 | sysman_log(LVL_NOTE, "Unit '%s' not found.", unit_name);
|
---|
81 | retval = ENOENT;
|
---|
82 | goto answer;
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (!(flags & IPC_FLAG_BLOCKING)) {
|
---|
86 | retval = sysman_queue_job(unit, STATE_STARTED, NULL, NULL);
|
---|
87 | goto answer;
|
---|
88 | }
|
---|
89 |
|
---|
90 | ipc_callid_t *iid_ptr = box_callid(iid);
|
---|
91 | if (iid_ptr == NULL) {
|
---|
92 | retval = ENOMEM;
|
---|
93 | goto answer;
|
---|
94 | }
|
---|
95 | retval = sysman_queue_job(unit, STATE_STARTED, &answer_callback,
|
---|
96 | iid_ptr);
|
---|
97 | if (retval != EOK) {
|
---|
98 | goto answer;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* Answer asynchronously from callback */
|
---|
102 | goto finish;
|
---|
103 |
|
---|
104 | answer:
|
---|
105 | async_answer_0(iid, retval);
|
---|
106 | finish:
|
---|
107 | free(unit_name);
|
---|
108 | }
|
---|
109 |
|
---|
110 | void sysman_connection_ctl(ipc_callid_t iid, ipc_call_t *icall)
|
---|
111 | {
|
---|
112 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
113 | /* First, accept connection */
|
---|
114 | async_answer_0(iid, EOK);
|
---|
115 |
|
---|
116 | while (true) {
|
---|
117 | ipc_call_t call;
|
---|
118 | ipc_callid_t callid = async_get_call(&call);
|
---|
119 |
|
---|
120 | if (!IPC_GET_IMETHOD(call)) {
|
---|
121 | /* Client disconnected */
|
---|
122 | break;
|
---|
123 | }
|
---|
124 |
|
---|
125 | switch (IPC_GET_IMETHOD(call)) {
|
---|
126 | case SYSMAN_CTL_UNIT_START:
|
---|
127 | sysman_unit_start(callid, &call);
|
---|
128 | break;
|
---|
129 | default:
|
---|
130 | async_answer_0(callid, ENOENT);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|