Skip to content

Commit 70e4651

Browse files
committed
Implementation of put state function
1 parent 9277e14 commit 70e4651

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ccflags-y += -D_LUNATIK -D_KERNEL -I$(src) -D_CONFIG_FULL_PANIC -DLUNATIK_UNUSED \
1+
ccflags-y += -D_LUNATIK -D_KERNEL -I$(src) -D_CONFIG_FULL_PANIC -DLUNATIK_UNUSED -DDEBUG\
22
-I$(src)/lua -I$(src)/deps/lua-memory/src
33
asflags-y += -D_LUNATIK -D_KERNEL
44

lib/lunatik.c

+24
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,9 @@ static int response_state_handler(struct nl_msg *msg, void *arg)
546546
} else if (attrs_tb[OP_ERROR] && nla_get_u8(attrs_tb[OP_ERROR])) {
547547
state->cb_result = CB_ERROR;
548548
return NL_OK;
549+
} else if (attrs_tb[NOT_IN_USE] && nla_get_u8(attrs_tb[NOT_IN_USE])) {
550+
state->cb_result = CB_ERROR;
551+
return NL_OK;
549552
}
550553

551554
if (attrs_tb[CURR_ALLOC]) {
@@ -810,3 +813,24 @@ int lunatik_getcurralloc(struct lunatik_nl_state *state)
810813
printf("Failed to put attribute to get current alloc of state %s\n", state->name);
811814
return -1;
812815
}
816+
817+
int lunatik_putstate(struct lunatik_nl_state *state)
818+
{
819+
struct nl_msg *msg;
820+
821+
int err = -1;
822+
823+
if ((msg = prepare_message(PUT_STATE, 0)) == NULL)
824+
return err;
825+
826+
NLA_PUT_STRING(msg, STATE_NAME, state->name);
827+
828+
if ((err = nl_send_auto(state->control_sock, msg)) < 0)
829+
return err;
830+
831+
return receive_state_op_result(state);
832+
833+
nla_put_failure:
834+
printf("Failed to put attribute to put state\n");
835+
return -1;
836+
}

lib/lunatik.h

+2
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,6 @@ int lunatik_datasend(struct lunatik_nl_state *state,
113113

114114
int lunatik_getcurralloc(struct lunatik_nl_state *state);
115115

116+
int lunatik_putstate(struct lunatik_nl_state *state);
117+
116118
#endif /* LUNATIK_H */

lib/lunatik_module.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static int lstate_close(lua_State *L)
158158
lua_pushnil(L);
159159
return 1;
160160
}
161-
161+
free(state);
162162
lua_pushboolean(L, true);
163163
return 1;
164164
}
@@ -319,6 +319,21 @@ static int lstate_getcurralloc(lua_State *L)
319319
return 1;
320320
}
321321

322+
static int lstate_put(lua_State *L)
323+
{
324+
struct lunatik_nl_state *state = getnlstate(L);
325+
326+
if (lunatik_putstate(state)) {
327+
lua_pushnil(L);
328+
return 1;
329+
}
330+
331+
free(state);
332+
lua_pushboolean(L, true);
333+
334+
return 1;
335+
}
336+
322337
static const luaL_Reg session_mt[] = {
323338
{"close", lsession_close},
324339
{"getfd", lsession_getfd},
@@ -337,6 +352,7 @@ static const luaL_Reg state_mt[] = {
337352
{"close", lstate_close},
338353
{"send", lstate_datasend},
339354
{"receive", lstate_datareceive},
355+
{"put", lstate_put},
340356
{NULL, NULL}
341357
};
342358

netlink.c

+37
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static int lunatikN_data(struct sk_buff *buff, struct genl_info *info);
5151
static int lunatikN_datainit(struct sk_buff *buff, struct genl_info *info);
5252
static int lunatikN_sendstate(struct sk_buff *buff, struct genl_info *info);
5353
static int lunatikN_getcurralloc(struct sk_buff *buff, struct genl_info *info);
54+
static int lunatikN_putstate(struct sk_buff *buff, struct genl_info *info);
5455

5556
struct nla_policy lunatik_policy[ATTRS_COUNT] = {
5657
[STATE_NAME] = { .type = NLA_STRING },
@@ -123,8 +124,16 @@ static const struct genl_ops l_ops[] = {
123124
.doit = lunatikN_getcurralloc,
124125
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
125126
.policy = lunatik_policy
127+
#endif
128+
},
129+
{
130+
.cmd = PUT_STATE,
131+
.doit = lunatikN_putstate,
132+
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
133+
.policy = lunatik_policy
126134
#endif
127135
}
136+
128137
};
129138

130139
struct genl_family lunatik_family = {
@@ -714,6 +723,34 @@ static int lunatikN_getcurralloc(struct sk_buff *buff, struct genl_info *info)
714723
return 0;
715724
}
716725

726+
static int lunatikN_putstate(struct sk_buff *buff, struct genl_info *info)
727+
{
728+
struct lunatik_state *s;
729+
char *state_name;
730+
731+
pr_debug("Received a PUT_STATE command\n");
732+
733+
state_name = (char*)nla_data(info->attrs[STATE_NAME]);
734+
s = lunatik_netstatelookup(state_name, genl_info_net(info));
735+
736+
if (s == NULL)
737+
goto error;
738+
739+
if (!s->inuse) {
740+
reply_with(NOT_IN_USE, PUT_STATE, info);
741+
return 0;
742+
}
743+
744+
s->inuse = false;
745+
reply_with(OP_SUCESS, PUT_STATE, info);
746+
747+
return 0;
748+
749+
error:
750+
reply_with(OP_ERROR, PUT_STATE, info);
751+
return 0;
752+
}
753+
717754
/* Note: Most of the function below is copied from NFLua: https://github.com/cujoai/nflua
718755
* Copyright (C) 2017-2019 CUJO LLC
719756
*

netlink_common.h

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ enum lunatik_operations {
4444
DATA_INIT,
4545
GET_STATE,
4646
GET_CURRALLOC,
47+
PUT_STATE,
4748
};
4849

4950
enum lunatik_attrs {
@@ -63,6 +64,7 @@ enum lunatik_attrs {
6364
LUNATIK_DATA_LEN,
6465
CURR_ALLOC,
6566
STATE_NOT_FOUND,
67+
NOT_IN_USE,
6668
ATTRS_COUNT
6769
#define ATTRS_MAX (ATTRS_COUNT - 1)
6870
};

0 commit comments

Comments
 (0)