server.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Milis Linux Ayguci Server
  3. * Milisarge - 2020
  4. * References:
  5. * http://beej.us/guide/bgipc/output/html/multipage/unixsock.html
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <sys/stat.h>
  14. #include <sys/un.h>
  15. #include <unistd.h>
  16. #include <poll.h>
  17. #include "lua.h"
  18. #include "lualib.h"
  19. #include "lauxlib.h"
  20. #include "lfs.h"
  21. lua_State* L;
  22. #define SOCK_PATH "/tmp/ayguci.sock"
  23. #define BUFFSIZE 4096
  24. #define AYGUCI_PATH "/usr/milis/ayguci/"
  25. #define SERVER_PATH "/usr/milis/ayguci/server/"
  26. // lua related
  27. void bail(lua_State *L, char *msg){
  28. fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
  29. msg, lua_tostring(L, -1));
  30. exit(1);
  31. }
  32. char * split(char str[], char delim[], int order) {
  33. char *rest=strdup(str), *ret = NULL, *state, *token;
  34. int i = 0;
  35. for(token = strtok_r(rest, delim, &state); token != NULL; token = strtok_r(NULL, delim, &state)) {
  36. i++;
  37. if(order == i) ret = strdup(token);
  38. //printf("%s\n", token);
  39. }
  40. free(rest);
  41. return ret;
  42. }
  43. const char* lua_stringexpr( lua_State* L, const char* expr,
  44. const char* def ) {
  45. const char* r = def ;
  46. char buf[1024] = "" ;
  47. /* Assign the Lua expression to a Lua global variable. */
  48. sprintf( buf, "evalExpr=%s", expr );
  49. if ( !luaL_dostring( L, buf ) ) {
  50. /* Get the value of the global varibable */
  51. lua_getglobal( L, "evalExpr" );
  52. if ( lua_isstring( L, -1 ) ) {
  53. r = lua_tostring( L, -1 );
  54. } else r=0;
  55. /* remove lua_getglobal value */
  56. lua_pop( L, 1 );
  57. }
  58. return r ;
  59. }
  60. int is_regular_file(const char *path)
  61. {
  62. struct stat path_stat;
  63. stat(path, &path_stat);
  64. return S_ISREG(path_stat.st_mode);
  65. }
  66. int main(void)
  67. {
  68. int s, s2, t, len;
  69. struct sockaddr_un local, remote;
  70. char reqstr[512] = {0};
  71. struct pollfd pfd;
  72. if ((s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) {
  73. perror("socket");
  74. exit(1);
  75. }
  76. local.sun_family = AF_UNIX;
  77. strcpy(local.sun_path, SOCK_PATH);
  78. unlink(local.sun_path);
  79. len = strlen(local.sun_path) + sizeof(local.sun_family);
  80. if (bind(s, (struct sockaddr *)&local, len) == -1) {
  81. perror("bind");
  82. exit(1);
  83. }
  84. if (listen(s, 5) == -1) {
  85. perror("listen");
  86. exit(1);
  87. }
  88. for(;;) {
  89. int done, n;
  90. printf("Ayguci Daemon Listening...\n");
  91. fflush(stdout);
  92. t = sizeof(remote);
  93. if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) {
  94. perror("accept");
  95. exit(1);
  96. }
  97. printf("Connected.\n");
  98. done = 0;
  99. const char *resp;
  100. do {
  101. int rc;
  102. //printf("Sleeping for 5 seconds, connect the client and exit from client\n");
  103. //sleep(5);
  104. pfd.fd = s2;
  105. pfd.events = POLLIN;
  106. rc = poll(&pfd, 1, 15000);
  107. if (!rc) printf("poll timed out\n");
  108. if (rc < 0) printf("poll failed\n");
  109. //printf("events: 0x%x\n", pfd.revents);
  110. // get request from ayguci client
  111. n = recv(s2, reqstr, 512, 0);
  112. reqstr[n] = '\0';
  113. if (n <= 0) {
  114. if (n < 0) perror("recv");
  115. done = 1;
  116. }
  117. printf("data recv:%s:\n", reqstr);
  118. printf("modul:%s:\n",split(reqstr," ",1));
  119. printf("param:%s:\n",split(reqstr," ",2));
  120. printf("--------------\n");
  121. lua_State *L = luaL_newstate();
  122. luaL_openlibs(L);
  123. // load useful libraries for plugins
  124. luaL_requiref(L, "lfs", luaopen_lfs, 0);
  125. if ( luaL_dofile( L, SERVER_PATH "json.lua" ) == 1 ) {
  126. printf( "Error loading %s\n", "json.lua" );
  127. //return 1 ;
  128. }
  129. if ( luaL_dofile( L, SERVER_PATH "util.lua" ) == 1 ) {
  130. printf( "Error loading %s\n", "util.lua" );
  131. //return 1 ;
  132. }
  133. if ( luaL_dofile( L, SERVER_PATH "serpent.lua" ) == 1 ) {
  134. printf( "Error loading %s\n", "serpent.lua" );
  135. //return 1 ;
  136. }
  137. char *filename=malloc(512);
  138. snprintf(filename, 512, AYGUCI_PATH "modules/%s.lua", split(reqstr," ",1));
  139. // moduller için ayguci server kütüphane yolları tanımlanır.
  140. // modül yüklenmeden önce
  141. luaL_dostring(L, "package.path = '" SERVER_PATH "'..'/?.lua' .. ';'.. package.path");
  142. if ( luaL_dofile( L, filename ) == 1 ) {
  143. printf( "Error loading %s\n", filename );
  144. resp="{'error':'loading lua file failed!'}";
  145. }else {
  146. // give input to lua set function
  147. // if there is an input
  148. if (split(reqstr," ",2)){
  149. lua_getglobal( L, "input" );
  150. lua_pushstring(L,split(reqstr," ",2));
  151. lua_setfield(L, -2, "data");
  152. }
  153. // appyly set data
  154. lua_getglobal(L, "set");
  155. if (lua_pcall(L, 0, 1, 0)){
  156. printf("{'warn':'lua code has not set method or failed'}\n");
  157. }else{
  158. printf("set function is run\n");
  159. }
  160. // apply get data
  161. lua_getglobal(L, "get");
  162. if (lua_pcall(L, 0, 1, 0)){
  163. printf("{'warn':'lua code has not get method or failed'}\n");
  164. }else{
  165. printf("get function is run\n");
  166. }
  167. if (lua_stringexpr( L, "result", 0 )){
  168. resp=lua_stringexpr( L, "result", 0 );
  169. } else{
  170. resp="{'error':'no result return'}";
  171. }
  172. }
  173. free(filename);
  174. printf("rsp:%s:\n",resp);
  175. if (send(s2, resp , strlen(resp), 0) < 0) {
  176. perror("send");
  177. done = 1;
  178. }
  179. lua_close(L);
  180. break;
  181. } while (!done);
  182. close(s2);
  183. }
  184. return 0;
  185. }