Skip to content

Commit

Permalink
Implementation of put state function
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusNtg committed Oct 17, 2020
1 parent 9277e14 commit 70e4651
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ccflags-y += -D_LUNATIK -D_KERNEL -I$(src) -D_CONFIG_FULL_PANIC -DLUNATIK_UNUSED \
ccflags-y += -D_LUNATIK -D_KERNEL -I$(src) -D_CONFIG_FULL_PANIC -DLUNATIK_UNUSED -DDEBUG\
-I$(src)/lua -I$(src)/deps/lua-memory/src
asflags-y += -D_LUNATIK -D_KERNEL

Expand Down
24 changes: 24 additions & 0 deletions lib/lunatik.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ static int response_state_handler(struct nl_msg *msg, void *arg)
} else if (attrs_tb[OP_ERROR] && nla_get_u8(attrs_tb[OP_ERROR])) {
state->cb_result = CB_ERROR;
return NL_OK;
} else if (attrs_tb[NOT_IN_USE] && nla_get_u8(attrs_tb[NOT_IN_USE])) {
state->cb_result = CB_ERROR;
return NL_OK;
}

if (attrs_tb[CURR_ALLOC]) {
Expand Down Expand Up @@ -810,3 +813,24 @@ int lunatik_getcurralloc(struct lunatik_nl_state *state)
printf("Failed to put attribute to get current alloc of state %s\n", state->name);
return -1;
}

int lunatik_putstate(struct lunatik_nl_state *state)
{
struct nl_msg *msg;

int err = -1;

if ((msg = prepare_message(PUT_STATE, 0)) == NULL)
return err;

NLA_PUT_STRING(msg, STATE_NAME, state->name);

if ((err = nl_send_auto(state->control_sock, msg)) < 0)
return err;

return receive_state_op_result(state);

nla_put_failure:
printf("Failed to put attribute to put state\n");
return -1;
}
2 changes: 2 additions & 0 deletions lib/lunatik.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ int lunatik_datasend(struct lunatik_nl_state *state,

int lunatik_getcurralloc(struct lunatik_nl_state *state);

int lunatik_putstate(struct lunatik_nl_state *state);

#endif /* LUNATIK_H */
18 changes: 17 additions & 1 deletion lib/lunatik_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static int lstate_close(lua_State *L)
lua_pushnil(L);
return 1;
}

free(state);
lua_pushboolean(L, true);
return 1;
}
Expand Down Expand Up @@ -319,6 +319,21 @@ static int lstate_getcurralloc(lua_State *L)
return 1;
}

static int lstate_put(lua_State *L)
{
struct lunatik_nl_state *state = getnlstate(L);

if (lunatik_putstate(state)) {
lua_pushnil(L);
return 1;
}

free(state);
lua_pushboolean(L, true);

return 1;
}

static const luaL_Reg session_mt[] = {
{"close", lsession_close},
{"getfd", lsession_getfd},
Expand All @@ -337,6 +352,7 @@ static const luaL_Reg state_mt[] = {
{"close", lstate_close},
{"send", lstate_datasend},
{"receive", lstate_datareceive},
{"put", lstate_put},
{NULL, NULL}
};

Expand Down
37 changes: 37 additions & 0 deletions netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static int lunatikN_data(struct sk_buff *buff, struct genl_info *info);
static int lunatikN_datainit(struct sk_buff *buff, struct genl_info *info);
static int lunatikN_sendstate(struct sk_buff *buff, struct genl_info *info);
static int lunatikN_getcurralloc(struct sk_buff *buff, struct genl_info *info);
static int lunatikN_putstate(struct sk_buff *buff, struct genl_info *info);

struct nla_policy lunatik_policy[ATTRS_COUNT] = {
[STATE_NAME] = { .type = NLA_STRING },
Expand Down Expand Up @@ -123,8 +124,16 @@ static const struct genl_ops l_ops[] = {
.doit = lunatikN_getcurralloc,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
.policy = lunatik_policy
#endif
},
{
.cmd = PUT_STATE,
.doit = lunatikN_putstate,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
.policy = lunatik_policy
#endif
}

};

struct genl_family lunatik_family = {
Expand Down Expand Up @@ -714,6 +723,34 @@ static int lunatikN_getcurralloc(struct sk_buff *buff, struct genl_info *info)
return 0;
}

static int lunatikN_putstate(struct sk_buff *buff, struct genl_info *info)
{
struct lunatik_state *s;
char *state_name;

pr_debug("Received a PUT_STATE command\n");

state_name = (char*)nla_data(info->attrs[STATE_NAME]);
s = lunatik_netstatelookup(state_name, genl_info_net(info));

if (s == NULL)
goto error;

if (!s->inuse) {
reply_with(NOT_IN_USE, PUT_STATE, info);
return 0;
}

s->inuse = false;
reply_with(OP_SUCESS, PUT_STATE, info);

return 0;

error:
reply_with(OP_ERROR, PUT_STATE, info);
return 0;
}

/* Note: Most of the function below is copied from NFLua: https://github.com/cujoai/nflua
* Copyright (C) 2017-2019 CUJO LLC
*
Expand Down
2 changes: 2 additions & 0 deletions netlink_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum lunatik_operations {
DATA_INIT,
GET_STATE,
GET_CURRALLOC,
PUT_STATE,
};

enum lunatik_attrs {
Expand All @@ -63,6 +64,7 @@ enum lunatik_attrs {
LUNATIK_DATA_LEN,
CURR_ALLOC,
STATE_NOT_FOUND,
NOT_IN_USE,
ATTRS_COUNT
#define ATTRS_MAX (ATTRS_COUNT - 1)
};
Expand Down

0 comments on commit 70e4651

Please sign in to comment.