Advertisement
Guest User

libxmldbc.c

a guest
Oct 23rd, 2019
1,939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.82 KB | None | 0 0
  1. / * vi: establecer sw = 4 ts = 4: * /
  2. / *
  3.  * libxmldbc.c
  4.  * *
  5.  * biblioteca común para el cliente xmldb.
  6.  * Creado por David Hsieh <david_hsieh@alphanetworks.com>
  7.  * Copyright (C) 2004-2009 por Alpha Networks, Inc.
  8.  * *
  9.  * Este archivo es software libre; puedes redistribuirlo y / o
  10.  * Modifíquelo bajo los términos del Público General Menor de GNU
  11.  * Licencia publicada por la Free Software Foundation; ya sea'
  12. * versión 2.1 de la Licencia, o (a su elección) cualquier versión posterior.
  13. * *
  14. * La Biblioteca GNU C se distribuye con la esperanza de que sea útil '
  15.  * pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de
  16.  * COMERCIABILIDAD o APTITUD PARA UN PROPÓSITO EN PARTICULAR. Ver la GNU
  17.  * Licencia pública general menor para más detalles.
  18.  * *
  19.  * Debería haber recibido una copia del Público general menor de GNU
  20.  * Licencia junto con la Biblioteca GNU C; si no, escriba a la libre
  21.  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22.  * 02111-1307 EE. UU.
  23.  * /
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <sys / types.h>
  32. #include <sys / socket.h>
  33. #include <sys / un.h>
  34. #include <errno.h>
  35.  
  36. #include <dtrace.h>
  37. #include <xmldb.h>
  38. #include <libxmldbc.h>
  39.  
  40. #ifdef DEBUG_LIBXMLDBC
  41. #define XMLDBCDBG (x) x
  42. #más
  43. #define XMLDBCDBG (x)
  44. #terminara si
  45.  
  46. int lxmldbc_run_shell (char * buf, int size, const char * format, ...)
  47. {
  48.     ARCHIVO * fp;
  49.     int i, c;
  50.     cmd de caracteres [MAX_CMD_LEN];
  51.     marcador va_list;
  52.  
  53.     va_start (marcador, formato);
  54.     vsnprintf (cmd, sizeof (cmd), formato, marcador);
  55.     va_end (marcador);
  56.  
  57.     fp = popen (cmd, "r");
  58.     si (fp)
  59.     {
  60.         para (i = 0; i <tamaño-1; i ++)
  61.         {
  62.             c = fgetc (fp);
  63.             if (c == EOF) descanso;
  64.             buf [i] = (char) c;
  65.         }
  66.         buf [i] = '\ 0';
  67.         pclose (fp);
  68.  
  69.         / * eliminar el último '\ n' * /
  70.         i = strlen (buf);
  71.         if (buf [i-1] == '\ n') buf [i-1] = 0;
  72.         devuelve 0;
  73.     }
  74.     buf [0] = 0;
  75.     volver -1;
  76. }
  77.  
  78. / * sistema de llamada () en formato printf (). * /
  79. int lxmldbc_system (formato const char *, ...)
  80. {
  81.     cmd de caracteres [MAX_CMD_LEN];
  82.     marcador va_list;
  83.  
  84.     va_start (marcador, formato);
  85.     vsnprintf (cmd, sizeof (cmd), formato, marcador);
  86.     va_end (marcador);
  87.     sistema de retorno (cmd);
  88. }
  89.  
  90. #define IS_WHITE (x) ((x) == '' || (x) == '\ t' || (x) == '\ n' || (x) == '\ r')
  91.  
  92. char * lxmldbc_eatwhite (char * cadena)
  93. {
  94.     if (cadena == NULL) devuelve NULL;
  95.     while (* cadena)
  96.     {
  97.         if (! IS_WHITE (* string)) se rompe;
  98.         string ++;
  99.     }
  100.     cadena de retorno;
  101. }
  102.  
  103. char * lxmldbc_reatwhite (char * ptr)
  104. {
  105.     int i;
  106.  
  107.     if (ptr == NULL) devuelve NULL;
  108.     i = strlen (ptr) -1;
  109.     while (i> = 0 && IS_WHITE (ptr [i])) ptr [i--] = '\ 0';
  110.     volver ptr;
  111. }
  112.  
  113. / ************************************************* ************************* /
  114.  
  115. static int __open_socket (const char * sockname)
  116. {
  117.     struct sockaddr_un donde;
  118.     int fd;
  119.  
  120.     if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) <0)
  121.     {
  122.         d_error ("% s: no se crea un socket de dominio Unix:% s. \ n", __ FUNCTION__, strerror (errno));
  123.         volver -1;
  124.     }
  125.  
  126.     fcntl (fd, F_SETFD, FD_CLOEXEC);
  127.  
  128.     where.sun_family = AF_UNIX;
  129.     if (nombre de calcetín == NULL) nombre de calcetín = XMLDB_DEFAULT_UNIXSOCK;
  130.     snprintf (where.sun_path, sizeof (where.sun_path), "% s", nombre del calcetín);
  131.  
  132.     if (connect (fd, (struct sockaddr *) & where, sizeof (where)) <0)
  133.     {
  134.         d_error ("% s: el código no se conecta al socket de Unix:% s. \ n", __ FUNCTION__, sockname);
  135.         cerca (fd);
  136.         volver -1;
  137.     }
  138.     volver fd;
  139. }
  140.  
  141. static int send_xmldb_cmd (int fd, action_t action, flags largos sin signo, const char * data, longitud corta sin signo)
  142. {
  143.     rgdb_ipc_t ipc;
  144.     tamaño ssize_t;
  145.  
  146.     ipc.action = acción;
  147.     ipc.flags = flags;
  148.     ipc.length = longitud;
  149.     size = send (fd, & ipc, sizeof (ipc), MSG_NOSIGNAL);
  150.     if (tamaño <= 0) devuelve -1;
  151.     tamaño = enviar (fd, datos, longitud, MSG_NOSIGNAL);
  152.     if (tamaño <= 0) devuelve -1;
  153.     devuelve 0;
  154. }
  155.  
  156. static void redirect_output (int fd, FILE * out)
  157. {
  158.     fd_set read_set;
  159.     tamaño ssize_t;
  160.     char buff [1024];
  161.  
  162.     para (;;)
  163.     {
  164.         FD_ZERO (& read_set);
  165.         FD_SET (fd, & read_set);
  166.         if (seleccione (fd + 1, & read_set, NULL, NULL, NULL) <0) continuar;
  167.         if (FD_ISSET (fd, y read_set))
  168.         {
  169.             tamaño = leer (fd, buff, sizeof (buff));
  170.             if (tamaño <= 0) descanso;
  171.             if (buff [tamaño-1] == '\ 0')
  172.             {
  173.                 fwrite (buff, 1, strlen (buff), fuera);
  174.                 rotura;
  175.             }
  176.             más
  177.             {
  178.                 fwrite (buff, 1, tamaño, fuera);
  179.             }
  180.         }
  181.     }
  182.     // joel add para la plataforma bcm. el servidor web perderá algunos datos sin vaciar.
  183.     fflush (fuera);
  184. }
  185.  
  186. static_ size_t redirect_to_buffer (int fd, char * buff, size_t buff_size)
  187. {
  188.     fd_set read_set;
  189.     tamaño ssize_t;
  190.     tamaño_t escrito = 0;
  191.  
  192.     dassert (buff && buff_size);
  193.  
  194.     para (;;)
  195.     {
  196.         FD_ZERO (& read_set);
  197.         FD_SET (fd, & read_set);
  198.         if (seleccione (fd + 1, & read_set, NULL, NULL, NULL) <0) continuar;
  199.         if (FD_ISSET (fd, y read_set))
  200.         {
  201.             tamaño = leer (fd, buff + escrito, buff_size - escrito);
  202.             if (tamaño <= 0) descanso;
  203.             escrito + = tamaño;
  204.             if (buff [escrito - 1] == '\ 0') descanso;
  205.             if (buff_size> = escrito)
  206.             {
  207.                 d_error ("% s: ¡no más espacio en el búfer para leer! \ n", __FUNCTION__);
  208.                 rotura;
  209.             }
  210.         }
  211.     }
  212.     devolución por escrito;
  213. }
  214.  
  215. / * comando con salida * /
  216. static int _cmd_w_out (sock_t sn, action_t a, flag_t f, const void * param, size_t size, FILE * out)
  217. {
  218.     int calcetín, ret = -1;
  219.     if ((calcetín = __open_socket (sn))> = 0)
  220.     {
  221.         if (send_xmldb_cmd (calcetín, a, f, parámetro, tamaño)> = 0)
  222.         {
  223.             redirect_output (calcetín, out? out: stdout);
  224.             ret = 0;
  225.         }
  226.         cerca (calcetín);
  227.     }
  228.     volver ret;
  229. }
  230.  
  231. / * comando sin salida * /
  232. static int _cmd_wo_out (sock_t sn, action_t a, flag_t f, const void * param, size_t size)
  233. {
  234.     rgdb_ipc_t ipc;
  235.     ssize_t rsize;
  236.     calcetín int;
  237.     int ret = -1;
  238.  
  239.     if ((calcetín = __open_socket (sn))> = 0)
  240.     {
  241.         if (send_xmldb_cmd (calcetín, a, f, parámetro, tamaño)> = 0)
  242.         {
  243.             rsize = read (calcetín, & ipc, sizeof (ipc));
  244.             ret = ipc.retcode;
  245.         }
  246.         cerca (calcetín);
  247.     }
  248.     volver ret;
  249. }
  250.  
  251. / ************************************************* ************************** /
  252. / * funciones de exportación * /
  253.  
  254. ssize_t xmldbc_get_wb (sock_t sn, flag_t f, const char * node, char * buff, size_t size)
  255. {
  256.     calcetín int;
  257.     ssize_t ret = -1;
  258.  
  259.     if ((calcetín = __open_socket (sn))> = 0)
  260.     {
  261.         if (send_xmldb_cmd (sock, XMLDB_GET, f, nodo, strlen (nodo) +1)> = 0)
  262.         {
  263.             redirect_to_buffer (calcetín, beneficio, tamaño);
  264.             ret = 0;
  265.         }
  266.         cerca (calcetín);
  267.     }
  268.     volver ret;
  269. }
  270.  
  271. int xmldbc_get (sock_t sn, flag_t f, const char * node, FILE * out)
  272. {
  273.     return _cmd_w_out (sn, XMLDB_GET, f, nodo, strlen (nodo) +1, fuera);
  274. }
  275.  
  276. ssize_t xmldbc_ephp_wb (sock_t sn, flag_t f, const char * file, char * buff, size_t size)
  277. {
  278.     calcetín int;
  279.     ssize_t ret = -1;
  280.  
  281.     if ((calcetín = __open_socket (sn))> = 0)
  282.     {
  283.         if (send_xmldb_cmd (sock, XMLDB_EPHP, f, archivo, strlen (archivo) +1)> = 0)
  284.         {
  285.             redirect_to_buffer (calcetín, beneficio, tamaño);
  286.             ret = 0;
  287.         }
  288.         cerca (calcetín);
  289.     }
  290.     volver ret;
  291. }
  292.  
  293. int xmldbc_ephp (sock_t sn, flag_t f, const char * file, FILE * out)
  294. {
  295.     return _cmd_w_out (sn, XMLDB_EPHP, f, archivo, strlen (archivo) +1, fuera);
  296. }
  297.  
  298. int xmldbc_set (sock_t sn, flag_t f, const char * node, const char * value)
  299. {
  300.     char buff [512];
  301.  
  302.     snprintf (buff, sizeof (buff) -1, "% s% s", nodo, valor);
  303.     buff [511] = '\ 0';
  304.     return _cmd_wo_out (sn, XMLDB_SET, f, buff, strlen (buff) +1);
  305. }
  306.  
  307. int xmldbc_setext (sock_t sn, flag_t f, const char * node, const char * cmd)
  308. {
  309.     char buff [512];
  310.  
  311.     snprintf (buff, sizeof (buff) -1, "% s% s", nodo, cmd);
  312.     buff [511] = '\ 0';
  313.     return _cmd_wo_out (sn, XMLDB_SETEXT, f, buff, strlen (buff) +1);
  314. }
  315.  
  316. int xmldbc_timer (sock_t sn, flag_t f, const char * cmd)
  317. {
  318.     return _cmd_wo_out (sn, XMLDB_TIMER, f, cmd, strlen (cmd) +1);
  319. }
  320.  
  321. int xmldbc_killtimer (sock_t sn, flag_t f, const char * tag)
  322. {
  323.     return _cmd_wo_out (sn, XMLDB_KILLTIMER, f, etiqueta, strlen (etiqueta) +1);
  324. }
  325.  
  326. int xmldbc_del (sock_t sn, flag_t f, const char * node)
  327. {
  328.     return _cmd_wo_out (sn, XMLDB_DEL, f, nodo, strlen (nodo) +1);
  329. }
  330.  
  331. int xmldbc_reload (sock_t sn, flag_t f, archivo const char *)
  332. {
  333.     return _cmd_wo_out (sn, XMLDB_RELOAD, f, archivo, strlen (archivo) +1);
  334. }
  335.  
  336. int xmldbc_patch (sock_t sn, flag_t f, const char * file)
  337. {
  338.     return _cmd_wo_out (sn, XMLDB_PATCH, f, archivo, strlen (archivo) +1);
  339. }
  340.  
  341. int xmldbc_read (sock_t sn, flag_t f, const char * file)
  342. {
  343.     return _cmd_wo_out (sn, XMLDB_READ, f, archivo, strlen (archivo) +1);
  344. }
  345.  
  346. int xmldbc_write (sock_t sn, flag_t f, const char * node, FILE * out)
  347. {
  348.     return _cmd_w_out (sn, XMLDB_WRITE, f, nodo, strlen (nodo) +1, fuera);
  349. }
  350.  
  351. int xmldbc_dump (sock_t sn, flag_t f, const char * file)
  352. {
  353.     return _cmd_wo_out (sn, XMLDB_DUMP, f, archivo, strlen (archivo) +1);
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement