Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

exec.c

Go to the documentation of this file.
00001 /*
00002  *
00003  * $Id: exec.c,v 1.19 2007/06/14 23:14:27 andrei Exp $
00004  *
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  *
00029  * History
00030  * --------
00031  * 2003-02-28 scratchpad compatibility abandoned (jiri)
00032  * 2003-01-28 scratchpad removed
00033  * 2004-07-21 rewrite uri done via action() (bogdan)
00034  */
00035 
00036 
00037 #include "../../comp_defs.h"
00038 
00039 #include <stdio.h>
00040 #include <strings.h>
00041 #include <errno.h>
00042 #include <stdlib.h>
00043 #include <sys/types.h>
00044 /*
00045 #include <sys/resource.h>
00046 */
00047 #include <sys/wait.h>
00048 #include "../../mem/mem.h"
00049 #include "../../error.h"
00050 #include "../../config.h"
00051 #include "../../parser/msg_parser.h"
00052 #include "../../dprint.h"
00053 #include "../../dset.h"
00054 #include "../../action.h"
00055 #include "../../ut.h"
00056 #include "config.h"
00057 
00058 int exec_msg(struct sip_msg *msg, str* cmd )
00059 {
00060         FILE *pipe;
00061         int exit_status;
00062         int ret;
00063         char* c;
00064 
00065         ret=-1; /* pessimist: assume error */
00066 
00067         c = as_asciiz(cmd);
00068         if (!c) {
00069             ERR("No memory left\n");
00070             return -1;
00071         }
00072 
00073         pipe=popen( c, "w" );
00074         pkg_free(c);
00075         if (pipe==NULL) {
00076             LOG(L_ERR, "ERROR: exec_msg: cannot open pipe: %.*s\n",
00077                         cmd->len, ZSW(cmd->s));
00078                 ser_error=E_EXEC;
00079                 return -1;
00080         }
00081 
00082         if (fwrite(msg->buf, 1, msg->len, pipe)!=msg->len) {
00083                 LOG(L_ERR, "ERROR: exec_msg: error writing to pipe\n");
00084                 ser_error=E_EXEC;
00085                 goto error01;
00086         }
00087         /* success */
00088         ret=1;
00089 
00090 error01:
00091         if (ferror(pipe)) {
00092                 LOG(L_ERR, "ERROR: exec_str: error in pipe: %s\n",
00093                         strerror(errno));
00094                 ser_error=E_EXEC;
00095                 ret=-1;
00096         }
00097         exit_status=pclose(pipe);
00098         if (WIFEXITED(exit_status)) { /* exited properly .... */
00099                 /* return false if script exited with non-zero status */
00100                 if (WEXITSTATUS(exit_status)!=0) ret=-1;
00101         } else { /* exited erroneously */
00102                 LOG(L_ERR, "ERROR: exec_msg: cmd %.*s failed. "
00103                         "exit_status=%d, errno=%d: %s\n",
00104                         cmd->len, ZSW(cmd->s), exit_status, errno, strerror(errno) );
00105                 ret=-1;
00106         }
00107         return ret;
00108 }
00109 
00110 int exec_str(struct sip_msg *msg, str* cmd, char *param, int param_len) {
00111 
00112         struct action act;
00113         int cmd_len;
00114         FILE *pipe;
00115         char *cmd_line;
00116         int ret;
00117         char uri_line[MAX_URI_SIZE+1];
00118         int uri_cnt;
00119         int uri_len;
00120         int exit_status;
00121         struct run_act_ctx ra_ctx;
00122 
00123         /* pessimist: assume error by default */
00124         ret=-1;
00125 
00126         cmd_len=cmd->len+param_len+2;
00127         cmd_line=pkg_malloc(cmd_len);
00128         if (cmd_line==0) {
00129                 ret=ser_error=E_OUT_OF_MEM;
00130                 LOG(L_ERR, "ERROR: exec_str: no mem for command\n");
00131                 goto error00;
00132         }
00133 
00134         /* 'command parameter \0' */
00135         memcpy(cmd_line, cmd->s, cmd->len); cmd_line[cmd->len]=' ';
00136         memcpy(cmd_line+cmd->len+1, param, param_len);cmd_line[cmd->len+param_len+1]=0;
00137 
00138         pipe=popen( cmd_line, "r" );
00139         if (pipe==NULL) {
00140                 LOG(L_ERR, "ERROR: exec_str: cannot open pipe: %s\n",
00141                         cmd_line);
00142                 ser_error=E_EXEC;
00143                 goto error01;
00144         }
00145 
00146         /* read now line by line */
00147         uri_cnt=0;
00148         while( fgets(uri_line, MAX_URI_SIZE, pipe)!=NULL){
00149                 uri_len=strlen(uri_line);
00150                 /* trim from right */
00151                 while(uri_len && (uri_line[uri_len-1]=='\r'
00152                                 || uri_line[uri_len-1]=='\n'
00153                                 || uri_line[uri_len-1]=='\t'
00154                                 || uri_line[uri_len-1]==' ' )) {
00155                         DBG("exec_str: rtrim\n");
00156                         uri_len--;
00157                 }
00158                 /* skip empty line */
00159                 if (uri_len==0) continue;
00160                 /* ZT */
00161                 uri_line[uri_len]=0;
00162                 if (uri_cnt==0) {
00163                         memset(&act, 0, sizeof(act));
00164                         act.type = SET_URI_T;
00165                         act.val[0].type = STRING_ST;
00166                         act.val[0].u.string = uri_line;
00167                         init_run_actions_ctx(&ra_ctx);
00168                         if (do_action(&ra_ctx, &act, msg)<0) {
00169                                 LOG(L_ERR,"ERROR:exec_str : SET_URI_T action failed\n");
00170                                 ser_error=E_OUT_OF_MEM;
00171                                 goto error02;
00172                         }
00173                 } else {
00174                         if (append_branch(msg, uri_line, uri_len, 0, 0, Q_UNSPECIFIED, 0)==-1) {
00175                                 LOG(L_ERR, "ERROR: exec_str: append_branch failed;"
00176                                         " too many or too long URIs?\n");
00177                                 goto error02;
00178                         }
00179                 }
00180                 uri_cnt++;
00181         }
00182         if (uri_cnt==0) {
00183                 LOG(L_ERR, "ERROR:exec_str: no uri from %s\n", cmd_line );
00184                 goto error02;
00185         }
00186         /* success */
00187         ret=1;
00188 
00189 error02:
00190         if (ferror(pipe)) {
00191                 LOG(L_ERR, "ERROR: exec_str: error in pipe: %s\n",
00192                         strerror(errno));
00193                 ser_error=E_EXEC;
00194                 ret=-1;
00195         }
00196         exit_status=pclose(pipe);
00197         if (WIFEXITED(exit_status)) { /* exited properly .... */
00198                 /* return false if script exited with non-zero status */
00199                 if (WEXITSTATUS(exit_status)!=0) ret=-1;
00200         } else { /* exited erroneously */
00201                 LOG(L_ERR, "ERROR: exec_str: cmd %.*s failed. "
00202                         "exit_status=%d, errno=%d: %s\n",
00203                         cmd->len, ZSW(cmd->s), exit_status, errno, strerror(errno) );
00204                 ret=-1;
00205         }
00206 error01:
00207         pkg_free(cmd_line);
00208 error00:
00209         return ret;
00210 }

Generated on Tue Sep 7 04:16:00 2010 for SIPExpressRouter by  doxygen 1.3.9.1