home *** CD-ROM | disk | FTP | other *** search
- #include <tk.h>
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <signal.h>
- #include <sys/wait.h>
-
- #include "hotsync.h"
- #include "synchronize.h"
- #include "Random.h"
- #include "User.h"
- #include "defs.h"
- #include "assert.h"
- #include "proc.h"
-
- #include "Comm.h"
-
- /* Tcl/Tk reference variables*/
- Tk_Window mainw;
- Tcl_Interp *g_interp;
-
- char home_dir[256]=""; /* inited at startup */
- char username[256]=""; /* inited at startup */
-
-
- /* daemon */
- int child_pid=-1; /* daemon usage */
- int daemon_flag = 0; /* 0 - always, 1 - only when jetsync, 2 - never */
- int write_daemon_line();
- int check_daemon();
-
-
- /* tcl/tk <-> C bindings */
- void create_commands(Tcl_Interp *interp);
-
-
-
- /* quit and signal binds - also checks if a daemon is running and if it should
- be killed or not */
- void __quit()
- {
- #ifdef __DEBUG
- printf("Bailing out!\n");
- #endif
-
- if (!child_pid) { /* child exits */
- child_pid = -1;
- write_daemon_line();
- }
-
- check_daemon();
- if (daemon_flag != 0 && child_pid != -1) {
- kill(child_pid, SIGINT);
- child_pid = -1;
- write_daemon_line();
- }
-
- exit(0);
- }
-
-
- /* get current user - used in Tcl/Tk */
- /* Arguments: JETHOME directory as string */
- /* Returns: last user that used JetSync */
- __TCL__ int whoami(ClientData clientData, Tcl_Interp *interp,
- int argc, char **argv)
- {
- FILE *fp;
- char line[80], path[256];
-
- /* whoami receives one param stating JETHOME directory */
- memset(home_dir, 0, 256);
- strcpy(home_dir, argv[1]);
-
- sprintf(path,"%s/%s",home_dir,JS_PREFS);
- fp = fopen(path,"r");
- WARN(fp)
-
- while (!feof(fp)) {
- fgets(line, 80, fp);
-
- if (feof(fp)) break;
-
- strtok(line,":");
- if (!line) {
- printf("File \"conf/prefs\" in your installation directory is corrupt.\n");
- return -1;
- }
-
- if (!strcmp(line,"USER")) {
- strcpy(interp->result, strtok(NULL,":"));
- interp->result[strlen(interp->result)-1] = 0; /* remove '\n' */
- break;
- }
- }
- fclose(fp);
-
- return TCL_OK;
- }
-
-
- /* changes the current user */
- /* Arguments: argv[1] contains the new username */
- /* Returns: nothing */
- __TCL__ int tcl_change_user(ClientData clientData, Tcl_Interp *interp,
- int argc, char **argv)
- {
- change_user(argv[1]);
- return TCL_OK;
- }
-
-
- /* Tcl/Tk call to quit program */
- __TCL__ int quit(ClientData clientData, Tcl_Interp *interp,
- int argc, char **argv)
- {
- __quit();
- return TCL_OK;
- }
-
-
- /* trapping of the SIGINT signal */
- void *ctrlc(int i)
- {
- __quit();
- return 0;
- }
-
-
- /* auxiliary function to DoTrace */
- char *__xdtrace(ClientData clientData, Tcl_Interp *interp,
- char *name1, char *name2, int flags)
- {
- #ifdef __DEBUG
- printf("TRACE::%s ACCESS TO %s=%s\n",
- flags & TCL_TRACE_READS ? "READ" : "WRITE",
- name1,Tcl_GetVar(interp,name1,flags));
- #endif
-
- return NULL;
- }
-
- /* handle tracing of a variable */
- __TCL__ int DoTrace(ClientData clientData, Tcl_Interp *interp,
- int argc, char **argv)
- {
- #ifdef __DEBUG
- printf("Tracing variable \"%s\"...\n", argv[1]);
- #endif
- Tcl_TraceVar(interp, argv[1],
- TCL_TRACE_WRITES | TCL_GLOBAL_ONLY | TCL_TRACE_READS,
- __xdtrace, clientData);
- return 0;
- }
-
-
- /* get daemon preferences from global prefs file */
- /* Arguments: none */
- /* Returns: ignored */
- int check_daemon()
- {
- FILE *fp;
- char line[80], path[256];
-
- sprintf(path,"%s/%s",home_dir,JS_PREFS);
- fp = fopen(path,"r");
-
- if (!fp) {
- daemon_flag = 2; /* don't launch daemon */
- return -1;
- }
-
- while (!feof(fp)) {
- fgets(line, 80, fp);
-
- if (feof(fp)) break;
-
- strtok(line,":");
-
- if (!strcmp(line,"DAEMON")) {
- daemon_flag = atoi(strtok(NULL,":"));
- child_pid = atoi(strtok(NULL,":"));
- break;
- }
- }
-
- fclose(fp);
- return 0;
- }
-
-
- /* writes the appropriate values of the daemon interface to disk */
- /* Arguments: none */
- /* Returns: always 0 */
- int write_daemon_line()
- {
- FILE *fp, *fpw;
- char line[80], path[256], path2[256];
-
- sprintf(path,"%s/%s",home_dir,JS_PREFS);
- fp = fopen(path,"r");
- WARN(fp)
-
- sprintf(path2,"%s/%s.tmp",home_dir,JS_PREFS);
- fpw = fopen(path2,"w");
- WARN(fpw)
-
- memset((char *)line, 0, 80);
- fgets(line, 80, fp); /* preserve 1st line (PCID) */
- fputs(line, fpw);
- memset((char *)line, 0, 80);
- fgets(line, 80, fp); /* preserve 2nd line (USER) */
- fputs(line, fpw);
- memset((char *)line, 0, 80);
- fgets(line, 80, fp); /* preserve 3rd line (MODEM) */
- fputs(line, fpw);
- memset((char *)line, 0, 80);
- fgets(line, 80, fp); /* preserve 4th line (MODEMSPEED) */
- fputs(line, fpw);
- memset((char *)line, 0, 80);
- fgets(line, 80, fp); /* preserve 5th line (MODEMTYPE) */
- fputs(line, fpw);
- memset((char *)line, 0, 80);
- fgets(line, 80, fp); /* preserve 6th line (MODEMSTR) */
- fputs(line, fpw);
- memset((char *)line, 0, 80);
- fgets(line, 80, fp); /* preserve 7th line (LOCAL) */
- fputs(line, fpw);
- memset((char *)line, 0, 80);
- fgets(line, 80, fp); /* preserve 8th line (LOCALSPEED) */
- fputs(line, fpw);
- memset((char *)line, 0, 80);
- fgets(line, 80, fp);
- /* write new 9th line (DAEMON) */
- fprintf(fpw,"DAEMON:%d:%d\n", daemon_flag, child_pid);
-
- fclose(fp);
- fclose(fpw);
- rename(path2, path);
-
- return 0;
- }
-
-
-
- /* background daemon that listens to synch requests sent via pilot */
- /* Arguments: none */
- /* Returns: always 0 unless an error occurs */
- int run_daemon()
- {
- /* check if daemon is to be present */
- if (daemon_flag == 2) return 0;
- if (child_pid != -1) {
- #ifdef __DEBUG
- printf("Daemon is already running with PID %d\n", child_pid);
- #endif
- return 0;
- }
-
- if (!(child_pid=fork())) {
- g_interp = Tcl_CreateInterp();
- if (Tk_Init(g_interp) == TCL_ERROR) return TCL_ERROR;
-
- whoami(NULL, g_interp, 0, 0);
- strcpy(username, g_interp->result);
- printf("Daemon running in background...\n");
- while (1) {
- FILE *fp = fopen(getenv("PILOTPORT"),"r+b");
- unsigned char c = fgetc(fp);
- ungetc(c, fp);
- fclose(fp);
- printf("Synch signal received!\n");
-
- if (check_processes() < 0) {
- int sd;
- char path[256], logbuffer[1024]="";
- struct PilotUser U;
-
- printf("Cannot start syncronization since there are JetSync "
- "applications still running!\nPlease close all applications "
- "first.\n");
- js_Start(&sd, &U, username, path);
- strcpy(logbuffer, "Cannot start syncronization since there are "
- "JetSync applications still running!\n");
- js_CloseComm(sd, &U, logbuffer);
-
- continue;
- }
-
- HOTSYNCMANAGER();
- }
- }
- else
- write_daemon_line();
- return 0;
- }
-
-
-
- /* program entry point */
- /* Arguments: ignored */
- /* Returns: always 0 unless an error occurs */
- int main(int argc, char **argv)
- {
- signal(SIGINT,(void *)ctrlc);
- signal(SIGTERM,(void *)ctrlc);
-
- /* check if daemon should be launched and launch it */
- check_daemon();
- run_daemon();
-
- /* enter Tcl/Tk main loop */
- Tk_Main(argc, argv, Tcl_AppInit);
-
- return 0;
- }
-
- /*
- * Tcl_AppInit
- * This procedure performs application-specific initialization.
- * Most applications, especially those that incorporate additional
- * packages, will have their own version of this procedure.
- *
- * Results:
- * Returns a standard Tcl completion code, and leaves an error
- * message in interp->result if an error occurs.
- *
- */
-
- int Tcl_AppInit(Tcl_Interp *interp)
- {
- if (Tcl_Init(interp) == TCL_ERROR) return TCL_ERROR;
- if (Tk_Init(interp) == TCL_ERROR) return TCL_ERROR;
-
- mainw = Tk_MainWindow(interp);
- if (mainw == NULL) {
- return TCL_ERROR;
- }
-
- /*
- * Call Tcl_CreateCommand for application-specific commands, if
- * they weren't already created by the init procedures called above.
- */
-
-
- /* hotsync.c */
- Tcl_CreateCommand(interp,"initHotSyncPrefs",initHotSyncPrefs,0,0);
- Tcl_CreateCommand(interp,"getHotSyncPrefs",getHotSyncPrefs,0,0);
- Tcl_CreateCommand(interp,"setHotSyncPrefs",setHotSyncPrefs,0,0);
-
- /* synchronize.c */
- Tcl_CreateCommand(interp,"HOTSYNCMANAGER",HOTSYNCMANAGER,(ClientData *)NULL,
- (Tcl_CmdDeleteProc *) NULL);
- Tcl_CreateCommand(interp,"DoTrace",DoTrace,(ClientData *)NULL,
- (Tcl_CmdDeleteProc *) NULL);
-
- /* main.c */
- Tcl_CreateCommand(interp,"whoami",whoami,(ClientData *)NULL,
- (Tcl_CmdDeleteProc *) NULL);
- Tcl_CreateCommand(interp,"tcl_change_user",tcl_change_user,(ClientData *)NULL,
- (Tcl_CmdDeleteProc *) NULL);
- Tcl_CreateCommand(interp,"quit",quit,(ClientData *)NULL,
- (Tcl_CmdDeleteProc *) NULL);
-
-
- Tcl_SetVar(interp,"tcl_RcFileName","~/.wishrc",TCL_GLOBAL_ONLY);
-
- /*
- g_interp goal: keep track of the interp on a global pointer
- usage: in libs and conduit code
- */
- g_interp = interp;
-
- return TCL_OK;
- }
-