/* ******************************************************************************* * Copyright (c) 1996 Martin Poole * ******************************************************************************* ** ** WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! ** ** Any changes to be made to this file should first be checked with ** mplib1 source control for library integrity. ** ** mplib1 source control can be reached at mplib1@quatermass.co.uk ** * * $Source: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/bpo_proc_me.c,v $ * $Author: mpoole $ * $Date: 2002/10/07 09:37:37 $ * $Revision: 1.2 $ * ******************************************************************************* * * Change History * * $Log: bpo_proc_me.c,v $ * Revision 1.2 2002/10/07 09:37:37 mpoole * Initial checkin of mplib1-3.1.0 * * Revision 1.1 2002/10/07 09:36:54 mpoole * Initial checkin of mplib1-3.1.0 * * ******************************************************************************* */ #ident "$Header: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/bpo_proc_me.c,v 1.2 2002/10/07 09:37:37 mpoole Exp $" /* ------------------------------------------------------------------ Include files ------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "include/bpo_proc_internal.h" #include /* ------------------------------------------------------------------ defines ------------------------------------------------------------------ */ static pid_t my_pid = (pid_t)0; static char my_pidstr[14]; static char *fifo_prefix=NULL; static struct shm_process_private *mdp=NULL; static char proc_debug_str[]="BPO_PROC_DEBUG"; static int bpo_proc_debug=0; /* ------------------------------------------------------------------ Code starts here ------------------------------------------------------------------ */ struct shm_process_private * alloc_private_process( pid_t the_pid, const char *pid_grp, const char *pid_nm, const char *pid_fifo ) { struct shm_process_private *sppp; char *g,*n,*f; if (pid_grp && *pid_grp) g = strdup(pid_grp); else g=NULL; if (pid_nm && *pid_nm) n = strdup(pid_nm); else n=NULL; if (pid_fifo && *pid_fifo) f = strdup(pid_fifo); else f=NULL; sppp = malloc( sizeof(struct shm_process_private) ); if (sppp) { /* initialise the structure */ if (the_pid == (pid_t)0) the_pid=getpid(); gen_pid_str( sppp->pid_str, the_pid ); sppp->pid = the_pid; sppp->pid_grp = g; sppp->pid_nm = n; sppp->pid_fifo = f; sppp->fifo_fd = -1; sppp->fifo_created = 0; sppp->fifo_mode = 0600; dl_Init_Node( &sppp->spp_Node, sppp->pid_str, sppp ); } else { if (g) free(g); if (n) free(n); if (f) free(f); } if (bpo_proc_debug) fprintfile(stderr,"alloc_private_process: returns: %p\n", sppp ); return(sppp); } void free_private_process( struct shm_process_private *sppp ) { if (bpo_proc_debug) fprintfile(stderr,"free_private_process: %p\n", sppp ); dl_Remove_Node( &sppp->spp_Node ); if (sppp->pid_grp) { free( sppp->pid_grp ); sppp->pid_grp=NULL; } if (sppp->pid_nm) { free( sppp->pid_nm ); sppp->pid_nm=NULL; } if (sppp->pid_fifo) { free( sppp->pid_fifo ); sppp->pid_fifo=NULL; } free(sppp); return; } static int check_self( void ) { int new_here=0; pid_t this_pid; if (bpo_proc_debug) fprintfile(stderr,"check_self: \n" ); this_pid = getpid(); if (my_pid==(pid_t)0) { /* Not been registered before. Set a few things up */ new_here = 1; }else if (my_pid!=this_pid) { new_here = 1; my_pid = this_pid; } if(new_here) { my_pid = this_pid; gen_pid_str( my_pidstr, this_pid ); } return(new_here); } static void close_old_details( void ) { if (bpo_proc_debug) fprintfile(stderr,"close_old_details: \n" ); if (mdp) { check_self(); if (mdp->fifo_fd > -1) { if (mdp->fifo_created) unlink(mdp->pid_fifo); free(mdp->pid_fifo); mdp->pid_fifo=NULL; mdp->fifo_created = 0; close( mdp->fifo_fd ); mdp->fifo_fd = -1; } free_private_process( mdp ); mdp=NULL; } return; } static int open_read_fifo( char *fifo_name, mode_t create_mask, int *created ) { int rv,t; mode_t old_mask; struct stat tstat; create_mask &= 0666; create_mask |= 0600; if (bpo_proc_debug) fprintfile(stderr,"open_read_fifo: <%s> mask %o\n", fifo_name, create_mask ); old_mask=umask((mode_t)0); if (created) *created = 0; if ( stat( fifo_name, &tstat ) != 0 ) { rv = mkfifo( fifo_name, create_mask ); if (rv==-1 && bpo_proc_debug) fprintfile(stderr,"open_read_fifo: mkfifo failed: %d\n", errno ); if (created) *created = 1; } if (bpo_proc_debug) fprintfile(stderr, "open_read_fifo: About to open\n" ); rv = open( fifo_name, O_RDWR | O_NONBLOCK ); if (rv>=0) { if (bpo_proc_debug) fprintfile(stderr, "open_read_fifo: About to set blocking mode\n" ); t = fcntl( rv, F_GETFL, 0 ); fcntl( rv, F_SETFL, t & ~O_NONBLOCK ); } else if (bpo_proc_debug) fprintfile(stderr,"open_read_fifo: open failed: %d\n", errno ); (void)umask(old_mask); return(rv); } int Set_My_Proc_Details( const char *group_name, const char *prog_name, const char *p_f_prefix, const char *fifo_name, mode_t fifo_mode, int flags ) { char t_buf[255]; const char *grp_nm,*prg_nm; int rv=0; bpo_proc_debug = get_config_flag(proc_debug_str); if (bpo_proc_debug) fprintfile(stderr,"Set_My_Proc_Details: \n" ); if (check_self()) close_old_details(); if (mdp==NULL) { /* Time to start a new one */ mdp = alloc_private_process( my_pid, NULL, NULL, NULL ); } if (bpo_proc_debug) fprintfile(stderr,"Set_My_Proc_Details: mdp = %p\n", mdp ); if (mdp==NULL) return(-1); time( &mdp->pid_start_tm ); /* generate numerous strings and fiddle with them */ if ( (flags & SHM_INHERIT_GROUP) || (flags & SHM_AUTOMAGIC_PIPE) ) grp_nm = getenv("PROGRAM_GROUP"); else grp_nm = group_name; if ( (flags & SHM_INHERIT_PROGRAM) || (flags & SHM_AUTOMAGIC_PIPE) ) prg_nm = getenv("PROGRAM_NAME"); else prg_nm = prog_name; if (prg_nm==NULL || *prg_nm=='\0') prg_nm = "unknown"; if (fifo_prefix) free(fifo_prefix); if (p_f_prefix && *p_f_prefix) { fifo_prefix = strdup( p_f_prefix ); }else fifo_prefix = strdup( eval_config_default( "PIPE_DIR", "$HOME/pipe" )); if (bpo_proc_debug) fprintfile(stderr,"Set_My_Proc_Details: grp: %s prg: %s prefix: %s\n", (grp_nm)?grp_nm:"NULL", prg_nm, fifo_prefix ); if (mdp->pid_fifo) free (mdp->pid_fifo); if ( (flags & SHM_AUTOMAGIC_PIPE) || fifo_name==NULL || *fifo_name=='\0' ) { /* Now what name shall we create it under */ if (grp_nm && *grp_nm) sprintf( t_buf, "%s/%s.%s.%s", fifo_prefix, grp_nm, prg_nm, my_pidstr ); else sprintf( t_buf, "%s/%s.%s", fifo_prefix, prg_nm, my_pidstr ); fifo_name = t_buf; } mdp->pid_fifo = strdup( fifo_name ); if (mdp->pid_grp) free(mdp->pid_grp); if (grp_nm && *grp_nm) mdp->pid_grp = strdup(grp_nm); else mdp->pid_grp = NULL; if (mdp->pid_nm) free(mdp->pid_nm); mdp->pid_nm = strdup(prg_nm); fifo_mode &= 0666; fifo_mode |= 0600; mdp->fifo_mode = fifo_mode; mdp->fifo_fd = open_read_fifo( mdp->pid_fifo, mdp->fifo_mode, &mdp->fifo_created ); if (bpo_proc_debug) fprintfile(stderr,"Set_My_Proc_Details: exit: rv=%d\n", rv ); return(rv); } struct shm_process_private * Get_My_Proc_Details( mode_t privs ) { bpo_proc_debug = get_config_flag(proc_debug_str); if (bpo_proc_debug) fprintfile(stderr,"Get_My_Proc_Details: \n" ); if (mdp==NULL || check_self()) { close_old_details(); /* Never been registered, no it now */ Set_My_Proc_Details( NULL, NULL, NULL, NULL, privs, SHM_AUTOMAGIC_PIPE ); /* Now open the pipe (in case it isn't) */ if (mdp && mdp->fifo_fd==-1) { mdp->fifo_fd = open_read_fifo( mdp->pid_fifo, mdp->fifo_mode, &mdp->fifo_created ); } } /* Now open the pipe (in case it isn't) */ if (mdp && mdp->fifo_fd==-1) { mdp->fifo_fd = open_read_fifo( mdp->pid_fifo, mdp->fifo_mode, &mdp->fifo_created ); } return(mdp); } struct shm_process_private * Check_My_Proc_Details( void ) { return(mdp); } int Get_Process_Pipe_fd( void ) { int rv= -1; bpo_proc_debug = get_config_flag(proc_debug_str); if (bpo_proc_debug) fprintfile(stderr,"Get_Process_Pipe_fd: \n" ); Get_My_Proc_Details( 0 ); if (mdp) rv = mdp->fifo_fd; return(rv); } int Wait_Process_Pipe( void ) { int rv; char tc='1'; bpo_proc_debug = get_config_flag(proc_debug_str); if (bpo_proc_debug) fprintfile(stderr,"Wait_Process_Pipe: \n" ); Get_My_Proc_Details( 0 ); if (mdp && mdp->fifo_fd!=-1) rv = read( mdp->fifo_fd, &tc, 1 ); else rv = -1; return(rv); } /* -- End of File -- */