Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- / * vi: establecer sw = 4 ts = 4: * /
- / *
- * libxmldbc.c
- * *
- * biblioteca común para el cliente xmldb.
- * Creado por David Hsieh <david_hsieh@alphanetworks.com>
- * Copyright (C) 2004-2009 por Alpha Networks, Inc.
- * *
- * Este archivo es software libre; puedes redistribuirlo y / o
- * Modifíquelo bajo los términos del Público General Menor de GNU
- * Licencia publicada por la Free Software Foundation; ya sea'
- * versión 2.1 de la Licencia, o (a su elección) cualquier versión posterior.
- * *
- * La Biblioteca GNU C se distribuye con la esperanza de que sea útil '
- * pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de
- * COMERCIABILIDAD o APTITUD PARA UN PROPÓSITO EN PARTICULAR. Ver la GNU
- * Licencia pública general menor para más detalles.
- * *
- * Debería haber recibido una copia del Público general menor de GNU
- * Licencia junto con la Biblioteca GNU C; si no, escriba a la libre
- * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- * 02111-1307 EE. UU.
- * /
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdarg.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys / types.h>
- #include <sys / socket.h>
- #include <sys / un.h>
- #include <errno.h>
- #include <dtrace.h>
- #include <xmldb.h>
- #include <libxmldbc.h>
- #ifdef DEBUG_LIBXMLDBC
- #define XMLDBCDBG (x) x
- #más
- #define XMLDBCDBG (x)
- #terminara si
- int lxmldbc_run_shell (char * buf, int size, const char * format, ...)
- {
- ARCHIVO * fp;
- int i, c;
- cmd de caracteres [MAX_CMD_LEN];
- marcador va_list;
- va_start (marcador, formato);
- vsnprintf (cmd, sizeof (cmd), formato, marcador);
- va_end (marcador);
- fp = popen (cmd, "r");
- si (fp)
- {
- para (i = 0; i <tamaño-1; i ++)
- {
- c = fgetc (fp);
- if (c == EOF) descanso;
- buf [i] = (char) c;
- }
- buf [i] = '\ 0';
- pclose (fp);
- / * eliminar el último '\ n' * /
- i = strlen (buf);
- if (buf [i-1] == '\ n') buf [i-1] = 0;
- devuelve 0;
- }
- buf [0] = 0;
- volver -1;
- }
- / * sistema de llamada () en formato printf (). * /
- int lxmldbc_system (formato const char *, ...)
- {
- cmd de caracteres [MAX_CMD_LEN];
- marcador va_list;
- va_start (marcador, formato);
- vsnprintf (cmd, sizeof (cmd), formato, marcador);
- va_end (marcador);
- sistema de retorno (cmd);
- }
- #define IS_WHITE (x) ((x) == '' || (x) == '\ t' || (x) == '\ n' || (x) == '\ r')
- char * lxmldbc_eatwhite (char * cadena)
- {
- if (cadena == NULL) devuelve NULL;
- while (* cadena)
- {
- if (! IS_WHITE (* string)) se rompe;
- string ++;
- }
- cadena de retorno;
- }
- char * lxmldbc_reatwhite (char * ptr)
- {
- int i;
- if (ptr == NULL) devuelve NULL;
- i = strlen (ptr) -1;
- while (i> = 0 && IS_WHITE (ptr [i])) ptr [i--] = '\ 0';
- volver ptr;
- }
- / ************************************************* ************************* /
- static int __open_socket (const char * sockname)
- {
- struct sockaddr_un donde;
- int fd;
- if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) <0)
- {
- d_error ("% s: no se crea un socket de dominio Unix:% s. \ n", __ FUNCTION__, strerror (errno));
- volver -1;
- }
- fcntl (fd, F_SETFD, FD_CLOEXEC);
- where.sun_family = AF_UNIX;
- if (nombre de calcetín == NULL) nombre de calcetín = XMLDB_DEFAULT_UNIXSOCK;
- snprintf (where.sun_path, sizeof (where.sun_path), "% s", nombre del calcetín);
- if (connect (fd, (struct sockaddr *) & where, sizeof (where)) <0)
- {
- d_error ("% s: el código no se conecta al socket de Unix:% s. \ n", __ FUNCTION__, sockname);
- cerca (fd);
- volver -1;
- }
- volver fd;
- }
- static int send_xmldb_cmd (int fd, action_t action, flags largos sin signo, const char * data, longitud corta sin signo)
- {
- rgdb_ipc_t ipc;
- tamaño ssize_t;
- ipc.action = acción;
- ipc.flags = flags;
- ipc.length = longitud;
- size = send (fd, & ipc, sizeof (ipc), MSG_NOSIGNAL);
- if (tamaño <= 0) devuelve -1;
- tamaño = enviar (fd, datos, longitud, MSG_NOSIGNAL);
- if (tamaño <= 0) devuelve -1;
- devuelve 0;
- }
- static void redirect_output (int fd, FILE * out)
- {
- fd_set read_set;
- tamaño ssize_t;
- char buff [1024];
- para (;;)
- {
- FD_ZERO (& read_set);
- FD_SET (fd, & read_set);
- if (seleccione (fd + 1, & read_set, NULL, NULL, NULL) <0) continuar;
- if (FD_ISSET (fd, y read_set))
- {
- tamaño = leer (fd, buff, sizeof (buff));
- if (tamaño <= 0) descanso;
- if (buff [tamaño-1] == '\ 0')
- {
- fwrite (buff, 1, strlen (buff), fuera);
- rotura;
- }
- más
- {
- fwrite (buff, 1, tamaño, fuera);
- }
- }
- }
- // joel add para la plataforma bcm. el servidor web perderá algunos datos sin vaciar.
- fflush (fuera);
- }
- static_ size_t redirect_to_buffer (int fd, char * buff, size_t buff_size)
- {
- fd_set read_set;
- tamaño ssize_t;
- tamaño_t escrito = 0;
- dassert (buff && buff_size);
- para (;;)
- {
- FD_ZERO (& read_set);
- FD_SET (fd, & read_set);
- if (seleccione (fd + 1, & read_set, NULL, NULL, NULL) <0) continuar;
- if (FD_ISSET (fd, y read_set))
- {
- tamaño = leer (fd, buff + escrito, buff_size - escrito);
- if (tamaño <= 0) descanso;
- escrito + = tamaño;
- if (buff [escrito - 1] == '\ 0') descanso;
- if (buff_size> = escrito)
- {
- d_error ("% s: ¡no más espacio en el búfer para leer! \ n", __FUNCTION__);
- rotura;
- }
- }
- }
- devolución por escrito;
- }
- / * comando con salida * /
- static int _cmd_w_out (sock_t sn, action_t a, flag_t f, const void * param, size_t size, FILE * out)
- {
- int calcetín, ret = -1;
- if ((calcetín = __open_socket (sn))> = 0)
- {
- if (send_xmldb_cmd (calcetín, a, f, parámetro, tamaño)> = 0)
- {
- redirect_output (calcetín, out? out: stdout);
- ret = 0;
- }
- cerca (calcetín);
- }
- volver ret;
- }
- / * comando sin salida * /
- static int _cmd_wo_out (sock_t sn, action_t a, flag_t f, const void * param, size_t size)
- {
- rgdb_ipc_t ipc;
- ssize_t rsize;
- calcetín int;
- int ret = -1;
- if ((calcetín = __open_socket (sn))> = 0)
- {
- if (send_xmldb_cmd (calcetín, a, f, parámetro, tamaño)> = 0)
- {
- rsize = read (calcetín, & ipc, sizeof (ipc));
- ret = ipc.retcode;
- }
- cerca (calcetín);
- }
- volver ret;
- }
- / ************************************************* ************************** /
- / * funciones de exportación * /
- ssize_t xmldbc_get_wb (sock_t sn, flag_t f, const char * node, char * buff, size_t size)
- {
- calcetín int;
- ssize_t ret = -1;
- if ((calcetín = __open_socket (sn))> = 0)
- {
- if (send_xmldb_cmd (sock, XMLDB_GET, f, nodo, strlen (nodo) +1)> = 0)
- {
- redirect_to_buffer (calcetín, beneficio, tamaño);
- ret = 0;
- }
- cerca (calcetín);
- }
- volver ret;
- }
- int xmldbc_get (sock_t sn, flag_t f, const char * node, FILE * out)
- {
- return _cmd_w_out (sn, XMLDB_GET, f, nodo, strlen (nodo) +1, fuera);
- }
- ssize_t xmldbc_ephp_wb (sock_t sn, flag_t f, const char * file, char * buff, size_t size)
- {
- calcetín int;
- ssize_t ret = -1;
- if ((calcetín = __open_socket (sn))> = 0)
- {
- if (send_xmldb_cmd (sock, XMLDB_EPHP, f, archivo, strlen (archivo) +1)> = 0)
- {
- redirect_to_buffer (calcetín, beneficio, tamaño);
- ret = 0;
- }
- cerca (calcetín);
- }
- volver ret;
- }
- int xmldbc_ephp (sock_t sn, flag_t f, const char * file, FILE * out)
- {
- return _cmd_w_out (sn, XMLDB_EPHP, f, archivo, strlen (archivo) +1, fuera);
- }
- int xmldbc_set (sock_t sn, flag_t f, const char * node, const char * value)
- {
- char buff [512];
- snprintf (buff, sizeof (buff) -1, "% s% s", nodo, valor);
- buff [511] = '\ 0';
- return _cmd_wo_out (sn, XMLDB_SET, f, buff, strlen (buff) +1);
- }
- int xmldbc_setext (sock_t sn, flag_t f, const char * node, const char * cmd)
- {
- char buff [512];
- snprintf (buff, sizeof (buff) -1, "% s% s", nodo, cmd);
- buff [511] = '\ 0';
- return _cmd_wo_out (sn, XMLDB_SETEXT, f, buff, strlen (buff) +1);
- }
- int xmldbc_timer (sock_t sn, flag_t f, const char * cmd)
- {
- return _cmd_wo_out (sn, XMLDB_TIMER, f, cmd, strlen (cmd) +1);
- }
- int xmldbc_killtimer (sock_t sn, flag_t f, const char * tag)
- {
- return _cmd_wo_out (sn, XMLDB_KILLTIMER, f, etiqueta, strlen (etiqueta) +1);
- }
- int xmldbc_del (sock_t sn, flag_t f, const char * node)
- {
- return _cmd_wo_out (sn, XMLDB_DEL, f, nodo, strlen (nodo) +1);
- }
- int xmldbc_reload (sock_t sn, flag_t f, archivo const char *)
- {
- return _cmd_wo_out (sn, XMLDB_RELOAD, f, archivo, strlen (archivo) +1);
- }
- int xmldbc_patch (sock_t sn, flag_t f, const char * file)
- {
- return _cmd_wo_out (sn, XMLDB_PATCH, f, archivo, strlen (archivo) +1);
- }
- int xmldbc_read (sock_t sn, flag_t f, const char * file)
- {
- return _cmd_wo_out (sn, XMLDB_READ, f, archivo, strlen (archivo) +1);
- }
- int xmldbc_write (sock_t sn, flag_t f, const char * node, FILE * out)
- {
- return _cmd_w_out (sn, XMLDB_WRITE, f, nodo, strlen (nodo) +1, fuera);
- }
- int xmldbc_dump (sock_t sn, flag_t f, const char * file)
- {
- return _cmd_wo_out (sn, XMLDB_DUMP, f, archivo, strlen (archivo) +1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement