00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <sys/stat.h>
00038 #include <sys/types.h>
00039 #include <unistd.h>
00040 #include <errno.h>
00041 #include <signal.h>
00042
00043 #include "../../sr_module.h"
00044 #include "../../dprint.h"
00045 #include "../../ut.h"
00046 #include "../../mem/mem.h"
00047 #include "../../mem/shm_mem.h"
00048 #include "../../mi/mi.h"
00049 #include "mi_fifo.h"
00050 #include "mi_parser.h"
00051 #include "mi_writer.h"
00052 #include "fifo_fnc.h"
00053
00054 static int mi_mod_init(void);
00055 static int mi_child_init(int rank);
00056 static void fifo_process(int rank);
00057 static int mi_destroy(void);
00058
00059
00060 static char *mi_fifo = 0;
00061 static char *mi_fifo_reply_dir = DEFAULT_MI_REPLY_DIR;
00062 static char *mi_reply_indent = DEFAULT_MI_REPLY_IDENT;
00063 static int mi_fifo_uid = -1;
00064 static char *mi_fifo_uid_s = 0;
00065 static int mi_fifo_gid = -1;
00066 static char *mi_fifo_gid_s = 0;
00067 static int mi_fifo_mode = S_IRUSR| S_IWUSR| S_IRGRP| S_IWGRP;
00068 static int read_buf_size = MAX_MI_FIFO_READ;
00069
00070 MODULE_VERSION
00071
00072
00073 static param_export_t mi_params[] = {
00074 {"fifo_name", STR_PARAM, &mi_fifo},
00075 {"fifo_mode", INT_PARAM, &mi_fifo_mode},
00076 {"fifo_group", STR_PARAM, &mi_fifo_gid_s},
00077 {"fifo_group", INT_PARAM, &mi_fifo_gid},
00078 {"fifo_user", STR_PARAM, &mi_fifo_uid_s},
00079 {"fifo_user", INT_PARAM, &mi_fifo_uid},
00080 {"reply_dir", STR_PARAM, &mi_fifo_reply_dir},
00081 {"reply_indent", STR_PARAM, &mi_reply_indent},
00082 {0,0,0}
00083 };
00084
00085
00086 static proc_export_t mi_procs[] = {
00087 {"MI FIFO", 0, 0, fifo_process, 1 },
00088 {0,0,0,0,0}
00089 };
00090
00091
00092 struct module_exports exports = {
00093 "mi_fifo",
00094 DEFAULT_DLFLAGS,
00095 0,
00096 mi_params,
00097 0,
00098 0,
00099 0,
00100 mi_procs,
00101 mi_mod_init,
00102 0,
00103 (destroy_function) mi_destroy,
00104 mi_child_init
00105 };
00106
00107
00108
00109
00110 static int mi_mod_init(void)
00111 {
00112 int n;
00113 struct stat filestat;
00114
00115
00116 if (mi_fifo==NULL || *mi_fifo == 0) {
00117 LM_ERR("No MI fifo configured\n");
00118 return -1;
00119 }
00120
00121 LM_DBG("testing mi_fifo existance ...\n");
00122 n=stat(mi_fifo, &filestat);
00123 if (n==0) {
00124
00125 if (unlink(mi_fifo)<0){
00126 LM_ERR("Cannot delete old MI fifo (%s): %s\n",
00127 mi_fifo, strerror(errno));
00128 return -1;
00129 }
00130 } else if (n<0 && errno!=ENOENT){
00131 LM_ERR("MI FIFO stat failed: %s\n", strerror(errno));
00132 return -1;
00133 }
00134
00135
00136 if(!mi_fifo_reply_dir || *mi_fifo_reply_dir == 0) {
00137 LM_ERR("mi_fifo_reply_dir parameter is empty\n");
00138 return -1;
00139 }
00140
00141
00142 n = stat(mi_fifo_reply_dir, &filestat);
00143 if(n < 0){
00144 LM_ERR("Directory stat for MI Fifo reply failed: %s\n", strerror(errno));
00145 return -1;
00146 }
00147
00148 if(S_ISDIR(filestat.st_mode) == 0){
00149 LM_ERR("mi_fifo_reply_dir parameter is not a directory\n");
00150 return -1;
00151 }
00152
00153
00154 if(!mi_fifo_mode){
00155 LM_WARN("cannot specify mi_fifo_mode = 0, forcing it to rw-------\n");
00156 mi_fifo_mode = S_IRUSR| S_IWUSR;
00157 }
00158
00159 if (mi_fifo_uid_s){
00160 if (user2uid(&mi_fifo_uid, &mi_fifo_gid, mi_fifo_uid_s)<0){
00161 LM_ERR("Bad user name %s\n", mi_fifo_uid_s);
00162 return -1;
00163 }
00164 }
00165
00166 if (mi_fifo_gid_s){
00167 if (group2gid(&mi_fifo_gid, mi_fifo_gid_s)<0){
00168 LM_ERR("Bad group name %s\n", mi_fifo_gid_s);
00169 return -1;
00170 }
00171 }
00172
00173 return 0;
00174 }
00175
00176
00177
00178 static int mi_child_init(int rank)
00179 {
00180 if (rank==PROC_TIMER || rank>0 ) {
00181 if ( mi_writer_init(read_buf_size, mi_reply_indent)!=0 ) {
00182 LM_CRIT("failed to init the reply writer\n");
00183 return -1;
00184 }
00185 }
00186
00187 return 0;
00188 }
00189
00190
00191 static void fifo_process(int rank)
00192 {
00193 FILE *fifo_stream;
00194
00195 LM_DBG("new process with pid = %d created\n",getpid());
00196
00197 fifo_stream = mi_init_fifo_server( mi_fifo, mi_fifo_mode,
00198 mi_fifo_uid, mi_fifo_gid, mi_fifo_reply_dir);
00199 if ( fifo_stream==NULL ) {
00200 LM_CRIT("The function mi_init_fifo_server returned with error!!!\n");
00201 exit(-1);
00202 }
00203
00204 if( init_mi_child()!=0) {
00205 LM_CRIT("Failed to init the mi process\n");
00206 exit(-1);
00207 }
00208
00209 if ( mi_parser_init(read_buf_size)!=0 ) {
00210 LM_CRIT("Failed to init the command parser\n");
00211 exit(-1);
00212 }
00213
00214 if ( mi_writer_init(read_buf_size, mi_reply_indent)!=0 ) {
00215 LM_CRIT("Failed to init the reply writer\n");
00216 exit(-1);
00217 }
00218
00219 mi_fifo_server( fifo_stream );
00220
00221 LM_CRIT("the function mi_fifo_server returned with error!!!\n");
00222 exit(-1);
00223 }
00224
00225
00226 static int mi_destroy(void)
00227 {
00228 int n;
00229 struct stat filestat;
00230
00231
00232 n=stat(mi_fifo, &filestat);
00233 if (n==0){
00234
00235 if (unlink(mi_fifo)<0){
00236 LM_ERR("cannot delete the fifo (%s): %s\n",
00237 mi_fifo, strerror(errno));
00238 goto error;
00239 }
00240 } else if (n<0 && errno!=ENOENT) {
00241 LM_ERR("FIFO stat failed: %s\n", strerror(errno));
00242 goto error;
00243 }
00244
00245 return 0;
00246 error:
00247 return -1;
00248 }
00249