00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include "cmdparams.h"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #define HANDLE_FILE "/home/jsoc/exports/Web_request_handles"
00025 #define OP_ERR (0)
00026 #define OP_ADD (1)
00027 #define OP_LOOKUP (2)
00028 #define OP_DELETE (3)
00029 #define NOT_SPECIFIED "NOT Specified"
00030
00031
00032
00033
00034 #include <unistd.h>
00035
00036 static FILE *lock_open(const char *filename)
00037 {
00038 int sleeps;
00039 char lockfile[1024];
00040 strcpy(lockfile, filename);
00041 strcat(lockfile, ".lck");
00042 FILE *fp = fopen(lockfile, "w");
00043 if (!fp)
00044 {
00045 fprintf(stderr, "Failed to open file for locking %s.\n", lockfile);
00046 return(NULL);
00047 }
00048 for(sleeps=0; lockf(fileno(fp),F_TLOCK,0); sleeps++)
00049 {
00050 if (sleeps >= 300)
00051 {
00052 fprintf(stderr,"Lock on %s failed.\n", lockfile);
00053 return(NULL);
00054 }
00055 sleep(1);
00056 }
00057 return(fp);
00058 }
00059
00060 static lock_close(FILE *fp)
00061 {
00062 lockf(fileno(fp),F_ULOCK,0);
00063 fclose(fp);
00064 }
00065
00066 int jsoc_add_proc_handle(const char *file, const char *handle, const char *pid)
00067 {
00068 FILE *fp, *fp_lock;
00069 if (strcmp(pid, NOT_SPECIFIED) == 0)
00070 {
00071 fprintf(stderr, "For adding entry, the pid must be provided.\n");
00072 return(1);
00073 }
00074 fp_lock = lock_open(file);
00075 if (!fp_lock)
00076 return(1);
00077 fp = fopen(file, "a");
00078 if (!fp)
00079 {
00080 fprintf(stderr, "Failed to open %s file.\n", file);
00081 return(1);
00082 }
00083 fprintf(fp, "%s\t%s\n", handle, pid);
00084 fclose(fp);
00085 lock_close(fp_lock);
00086 return(0);
00087 }
00088
00089 char *jsoc_lookup_proc_handle(const char *file, const char *handle)
00090 {
00091 FILE *fp, *fp_lock;
00092 char PID[100];
00093 char HANDLE[100];
00094 char *pid = NULL;
00095 fp_lock = lock_open(file);
00096 if (!fp_lock)
00097 return(NULL);
00098 fp = fopen(file, "r");
00099 if (!fp)
00100 {
00101 fprintf(stderr, "Failed to open %s file.\n", file);
00102 return(NULL);
00103 }
00104 while (fscanf(fp, "%s\t%s\n", HANDLE, PID) == 2)
00105 if (strcmp(handle, HANDLE) == 0)
00106 {
00107 pid = strdup(PID);
00108 break;
00109 }
00110 fclose(fp);
00111 lock_close(fp_lock);
00112 return(pid);
00113 }
00114
00115 int jsoc_delete_proc_handle(const char *file, const char *handle)
00116 {
00117 FILE *fp, *fp_lock;
00118 char cmd[1024];
00119 fp_lock = lock_open(file);
00120 if (!fp_lock)
00121 return(1);
00122 sprintf(cmd, "ed -s %s <<END\ng/^%s\t/d\nwq\nEND\n", file, handle);
00123 system(cmd);
00124 lock_close(fp_lock);
00125 return(0);
00126 }
00127
00128 int jsoc_edit_proc_handles(const char *file )
00129 {
00130 FILE *fp, *fp_lock;
00131 char cmd[1024];
00132 fp_lock = lock_open(file);
00133 if (!fp_lock)
00134 return(1);
00135 sprintf(cmd, "vi %s\n", file);
00136 system(cmd);
00137 lock_close(fp_lock);
00138 return(0);
00139 }
00140
00141
00142
00143 #include "cmdparams.h"
00144
00145 ModuleArgs_t module_args[] =
00146 {
00147 {ARG_STRING, "handle", NOT_SPECIFIED, "Unique request handle"},
00148 {ARG_STRING, "pid", NOT_SPECIFIED, "Running processes ID"},
00149 {ARG_STRING, "file", HANDLE_FILE, "Active handle log file"},
00150 {ARG_FLAG, "a", "0", "op = add entry"},
00151 {ARG_FLAG, "l", "0", "op = lookup entry"},
00152 {ARG_FLAG, "d", "0", "op = delete entry"},
00153 {ARG_FLAG, "e", "0", "op = edit file"},
00154 {ARG_END}
00155 };
00156 ModuleArgs_t *gModArgs = module_args;
00157
00158
00159 CmdParams_t cmdparams;
00160 char *module_name = "jsoc_manage_cgibin_handles";
00161
00162 int main(int argc, char **argv)
00163 {
00164 int status;
00165 const char *handle;
00166 const char *filename;
00167 const char *pid;
00168 int op_add, op_lookup, op_delete, op_edit;
00169
00170
00171 status = cmdparams_parse (&cmdparams, argc, argv);
00172 if (status == CMDPARAMS_QUERYMODE)
00173 {
00174 cmdparams_usage (argv[0]);
00175 return 0;
00176 }
00177
00178 filename = cmdparams_get_str(&cmdparams, "file", NULL);
00179 pid = cmdparams_get_str(&cmdparams, "pid", NULL);
00180 handle = cmdparams_get_str(&cmdparams, "handle", NULL);
00181 op_add = cmdparams_isflagset(&cmdparams, "a");
00182 op_lookup = cmdparams_isflagset(&cmdparams, "l");
00183 op_delete = cmdparams_isflagset(&cmdparams, "d");
00184 op_edit = cmdparams_isflagset(&cmdparams, "e");
00185
00186 if (!op_edit && (!handle || strcmp(handle, NOT_SPECIFIED) == 0))
00187 {
00188 fprintf(stderr, "handle must be provided.\n");
00189 return(1);
00190 }
00191
00192 if (op_add)
00193 return(jsoc_add_proc_handle(filename, handle, pid));
00194
00195 if (op_lookup)
00196 {
00197 char *pid = jsoc_lookup_proc_handle(filename, handle);
00198 if (pid)
00199 printf("%s\n", pid);
00200 fflush(stdout);
00201 return(0);
00202 }
00203
00204 if (op_delete)
00205 return(jsoc_delete_proc_handle(filename, handle));
00206
00207 if (op_edit)
00208 return(jsoc_edit_proc_handles(filename));
00209
00210 }