summaryrefslogtreecommitdiff
path: root/l_cmd.c
blob: 86bc87290ebdd164cf9bf573dc9e561ca19742d4 (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
/*
 *  rhctl
 *
 *  Copyright (C) 2009-2014 Christian Pointner <equinox@helsinki.at>
 *
 *  This file is part of rhctl.
 *
 *  rhctl is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  rhctl 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with rhctl. If not, see <http://www.gnu.org/licenses/>.
 */

#include <lua.h>
#include <lauxlib.h>

#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>

#include "datatypes.h"

#include "l_cmd.h"

#include "options.h"
#include "command_queue.h"
#include "client_list.h"
#include "log.h"
#include "utils.h"

int cmd_fd;
read_buffer_t cmd_buffer;

static int l_cmd_getfd(lua_State *L)
{
  if(!lua_istable(L, -1))
    luaL_error(L, "can't retreive signal fd");

  lua_pushliteral(L, "fd");
  lua_gettable(L, -2);
  return 1;
}

static int l_cmd_dirty(lua_State *L)
{
  lua_pushboolean(L, 0);
  return 1;
}

static int l_cmd_init(lua_State *L)
{
  lua_newtable(L);
  lua_pushliteral(L, "fd");
  lua_pushinteger(L, cmd_fd);
  lua_settable(L, -3);
  lua_pushliteral(L, "getfd");
  lua_pushcfunction(L, l_cmd_getfd);
  lua_settable(L, -3);
  lua_pushliteral(L, "dirty");
  lua_pushcfunction(L, l_cmd_dirty);
  lua_settable(L, -3);
  return 1;
}

// gets called by nonblock_recvline for every complete message
static int cmd_recv_data(lua_State* L)
{
  int ret = 0;
  for(;;) {
    ret = recv(cmd_fd, &cmd_buffer.buf[cmd_buffer.offset], 1, 0);
    if(!ret)
      return 2;
    if(ret == -1 && errno == EAGAIN)
      return 0;
    else if(ret < 0)
      break;

    if(cmd_buffer.buf[cmd_buffer.offset] == '\n') {
      cmd_buffer.buf[cmd_buffer.offset] = 0;

      lua_pushstring(L, (char *)(cmd_buffer.buf));

      ret = lua_pcall(L, 1, 1, 0);
      if(ret) {
        const char* err_str = luaL_checkstring(L, -1);
        switch(ret) {
        case LUA_ERRRUN: log_printf(ERROR, "lua_pcall(cmd_callback) runtime error: %s", err_str); break;
        case LUA_ERRMEM: log_printf(ERROR, "lua_pcall(cmd_callback) malloc error: %s", err_str); break;
        case LUA_ERRERR: log_printf(ERROR, "lua_pcall(cmd_callback) error at error handler function: %s", err_str); break;
        }
        return -1;
      }

      ret = lua_tointeger(L, 1);
      cmd_buffer.offset = 0;
      break;
    }

    cmd_buffer.offset++;
    if(cmd_buffer.offset >= sizeof(cmd_buffer.buf)) {
      log_printf(DEBUG, "string too long cmd_fd");
      cmd_buffer.offset = 0;
      return 0;
    }
  }

  return ret;
}

static int l_cmd_recv_data(lua_State* L)
{
  int ret = cmd_recv_data(L);
  lua_pushinteger(L, ret);
  return 1;
}

static int l_cmd_send_string(lua_State* L)
{
  int ret = send_string(cmd_fd, luaL_checkstring(L,1));
  if (ret > 0)
    do {
      ret = write(cmd_fd, "\n", 1);
    } while(!ret || (ret == -1 && errno == EINTR));

  if(ret > 0)
    ret = 0;

  lua_pushinteger(L, ret);
  return 1;
}

static const struct luaL_reg cmd_funcs [] = {
  { "init", l_cmd_init },
  { "recv_data", l_cmd_recv_data },
  { "send_string", l_cmd_send_string },
  { NULL, NULL }
};


LUALIB_API int luaopen_cmd(lua_State *L)
{
  luaL_register(L, LUA_CMDLIBNAME, cmd_funcs);
  return 1;
}