/* ** openMosix lock/unlock test ** ** Copyright (C) 2004 Moreno 'baro' Baricevic ** ** This file may be used subject to the terms and conditions of the ** GNU Library General Public License as published by the Free Soft- ** ware Foundation; either version 2 of the License, or (at your op- ** tion) any later version. ** ** This file is distributed in the hope that it will be useful, but ** WITHOUT ANY WARRANTY; without even the implied warranty of MER- ** CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Li- ** brary General Public License for more details. */ /************************************************** * Author: Moreno "Baro" Baricevic * * Contact: baro@democritos.it * * Date: 21-02-2004 * * Title: sguscio.c * *================================================* * Prev Modified: 21-02-2004 * * Last Modified: 06-06-2004 * **************************************************/ #include #include #include #include #include #include #include char p_lock[32]; #define LOCKED '1' #define UNLOCKED '0' /************************************************************************* *_____________________________ OM_SETLOCK _____________________________* *************************************************************************/ // adapted from omprocps-X.Y.Z proc/ominfo.c and lock/* /* ** om_unlock(): always success ** om_lock(): - always success if already locked (...); ** - always works on caller self process pid; ** - can't lock any unlocked process, write() returns -1 ** and sets errno to EINVAL. */ int __om_setlock( const char * lockfile , char lk ) { int w = -1 , r = -1; int fd = -1; char c = 0x00; fd = open( lockfile , O_RDWR | O_TRUNC ); if ( fd < 0 ) { fprintf( stderr , "*** cannot open lock file '%s': [%d] %m\n" , p_lock , errno ); return -1; } w = write( fd , &lk , 1 ); if ( w != 1 ) { fprintf( stderr , "*** cannot %slock myself: [%d] %m\n" , lk == UNLOCKED ? "un" : "" , errno ); close( fd ); return -1; } (void)lseek( fd , 0L , SEEK_SET ); r = read( fd , &c , 1 ); if ( r != w ) { fprintf( stderr , "*** cannot read lock status: [%d] %m\n" , errno ); close( fd ); return -1; } else if ( c != lk ) { fprintf( stderr , "*** %slock failed: wrote 0x%02x - read 0x%02x\n" , lk == UNLOCKED ? "un" : "" , lk , c ); close( fd ); return -1; } close( fd ); return 0; } /* __om_setlock */ void lock( int signo ) { __om_setlock( p_lock , LOCKED ); } void unlock( int signo ) { __om_setlock( p_lock , UNLOCKED ); } int main ( int argc , char * argv[] ) { long long int lli; pid_t pid = getpid(); const char * basename = strrchr( argv[0] , '/' ); if ( basename && *basename && *(basename+1) ) ++basename; else basename = argv[0]; snprintf( p_lock , sizeof( p_lock ) , "/proc/%u/lock" , pid ); printf( "Some tips:\n" ); printf( "name %s\n" , basename ); printf( "pid %u\n" , pid ); printf( "lock %s\n" , p_lock ); #define CMD( SIGNAL ) \ "ompsinfo -C %s ; " \ "kill -" SIGNAL " %u && " \ "usleep 0 ; " \ "ompsinfo -h -C %s" \ printf( CMD( "SIGUSR1" ) "\t# lock\n" , basename , pid , basename ); printf( CMD( "SIGUSR2" ) "\t# unlock\n" , basename , pid , basename ); // printf( "migrate %u SOMENODE && ompsinfo -C %s\n" , pid , basename ); signal( SIGUSR1 , lock ); signal( SIGUSR2 , unlock ); for(lli=0;;lli++); exit( 0 ); } //EOF