📄 lockdaemon.c
字号:
pid_buffer, (int) getpid(), (int) getppid(), openpid ); syslog( LOG_INFO, message ); return( 1 ); } return( 0 );}/*---------------------------------------------------------- check_group_uucp accept: none perform: check if the user is root or in group uucp return: 0 on success exceptions: none comments: This checks if the effective user is in group uucp so we can create lock files. If not we give them a warning and bail. If its root we just skip the test. if someone really wants to override this they can use the USER_LOCK_DIRECTORY --not recommended. In a recent change RedHat 7.2 decided to use group lock. In order to get around this we just check the group id of the lock directory.----------------------------------------------------------*/int check_group_uucp(){#ifndef USER_LOCK_DIRECTORY int group_count; struct passwd *user = getpwuid( geteuid() ); struct stat buf; char msg[80]; gid_t list[ NGROUPS_MAX ]; if( stat( LOCKDIR, &buf) ) { sprintf( msg, "check_group_uucp: Can not find Lock Directory: %s\n", LOCKDIR ); syslog( LOG_INFO, msg ); return( 1 ); } group_count = getgroups( NGROUPS_MAX, list ); list[ group_count ] = geteuid(); if( user->pw_gid ) { while( group_count >= 0 && buf.st_gid != list[ group_count ] ) { group_count--; } if( buf.st_gid == list[ group_count ] ) return 0; sprintf( msg, "%i %i\n", buf.st_gid, list[ group_count ] ); syslog( LOG_INFO, msg ); syslog( LOG_INFO, UUCP_ERROR ); return 1; } return 0;/* if( strcmp( user->pw_name, "root" ) ) { while( *g->gr_mem ) { if( !strcmp( *g->gr_mem, user->pw_name ) ) { break; } (void) *g->gr_mem++; } if( !*g->gr_mem ) { syslog( LOG_INFO, UUCP_ERROR ); return 1; } }*/#endif /* USER_LOCK_DIRECTORY */ return 0;}/*---------------------------------------------------------- The following should be able to follow symbolic links. I think the stat method used below will work on more systems. This was found while looking for information. * realpath() doesn't exist on all of the systems my code has to run on (HP-UX 9.x, specifically)----------------------------------------------------------int different_from_LOCKDIR(const char* ld){ char real_ld[MAXPATHLEN]; char real_LOCKDIR[MAXPATHLEN]; if (strncmp(ld, LOCKDIR, strlen(ld)) == 0) return 0; if (realpath(ld, real_ld) == NULL) return 1; if (realpath(LOCKDIR, real_LOCKDIR) == NULL) return 1; if (strncmp(real_ld, real_LOCKDIR, strlen(real_ld)) == 0) return 0; else return 1;}*//*---------------------------------------------------------- is_device_locked accept: char * filename. The device in question including the path. perform: see if one of the many possible lock files is aready there if there is a stale lock, remove it. return: 1 if the device is locked or somethings wrong. 0 if its possible to create our own lock file. exceptions: none comments: check if the device is already locked----------------------------------------------------------*/int is_device_locked( const char *port_filename ){ const char *lockdirs[] = { "/etc/locks", "/usr/spool/kermit", "/usr/spool/locks", "/usr/spool/uucp", "/usr/spool/uucp/", "/usr/spool/uucp/LCK", "/var/lock", "/var/lock/modem", "/var/spool/lock", "/var/spool/locks", "/var/spool/uucp", LOCKDIR, NULL }; const char *lockprefixes[] = { "LCK..", "lk..", "LK.", NULL }; char *p, file[80], pid_buffer[20], message[80]; int i = 0, j, k, fd , pid; struct stat buf; struct stat buf2; j = strlen( port_filename ); p = ( char * ) port_filename+j; while( *( p-1 ) != '/' && j-- !=1 ) p--; while( lockdirs[i] ) { /* Look for lockfiles in all known places other than the defined lock directory for this system report any unexpected lockfiles. Is the suspect lockdir there? if it is there is it not the expected lock dir? */ if( !stat( lockdirs[i], &buf2 ) && strncmp( lockdirs[i], LOCKDIR, strlen( lockdirs[i] ) ) ) { j = strlen( port_filename ); p = ( char * ) port_filename + j; /* SCO Unix use lowercase all the time taj */ while( *( p - 1 ) != '/' && j-- != 1 ) {#if defined ( __unixware__ ) *p = tolower( *p );#endif /* __unixware__ */ p--; } k=0; while ( lockprefixes[k] ) { /* FHS style */ sprintf( file, "%s/%s%s", lockdirs[i], lockprefixes[k], p ); if( stat( file, &buf ) == 0 ) { sprintf( message, UNEXPECTED_LOCK_FILE, file ); syslog( LOG_INFO, message ); return 1; } /* UUCP style */ stat(port_filename , &buf ); sprintf( file, "%s/%s%03d.%03d.%03d", lockdirs[i], lockprefixes[k], (int) major( buf.st_dev ), (int) major( buf.st_rdev ), (int) minor( buf.st_rdev ) ); if( stat( file, &buf ) == 0 ) { sprintf( message, UNEXPECTED_LOCK_FILE, file ); syslog( LOG_INFO, message ); return 1; } k++; } } i++; } /* OK. We think there are no unexpect lock files for this device Lets see if there any stale lock files that need to be removed. */ #ifdef FHS /* FHS standard locks */ i = strlen( port_filename ); p = ( char * ) port_filename + i; while( *(p-1) != '/' && i-- != 1) {#if defined ( __unixware__ ) *p = tolower( *p );#endif /* __unixware__ */ p--; } sprintf( file, "%s/%s%s", LOCKDIR, LOCKFILEPREFIX, p );#else /* UUCP standard locks */ sprintf( file, "%s/LK.%03d.%03d.%03d", LOCKDIR, (int) major( buf.st_dev ), (int) major( buf.st_rdev ), (int) minor( buf.st_rdev ) );#endif /* FHS */ if( stat( file, &buf ) == 0 ) { /* check if its a stale lock */ fd=open( file, O_RDONLY ); read( fd, pid_buffer, 11 ); /* FIXME null terminiate pid_buffer? need to check in Solaris */ close( fd ); sscanf( pid_buffer, "%d", &pid ); sprintf( message, "found lock for %s with pid %i\n", file, pid ); /* syslog( LOG_INFO, message ); */ if( kill( (pid_t) pid, 0 ) && errno==ESRCH ) { sprintf( message, "RXTX Warning: Removing stale lock file. %s\n", file ); syslog( LOG_INFO, message ); if( unlink( file ) != 0 ) { snprintf( message, 80, "RXTX Error: Unable to \ remove stale lock file: %s\n", file ); syslog( LOG_INFO, message ); return 0; } } else { sprintf( message, "could not kill %i\n", pid ); /* syslog( LOG_INFO, message ); */ return 1; } } return 0;}int init( void ){ pid_t pid; if( ( pid = fork() ) < 0 ) { return(-1); } else if ( pid != 0 ) { exit( 0 ); } setsid(); chdir("/"); umask( 0 ); return( 0 );}int process_requests( ){ for(;;) { char str[80]; char str2[80]; char *p; int ret; ret = read( 1, str, 80 ); if( ret < 80 && ret > 1 ) str[ret] = '\0'; else str[79] = '\0'; if ( !strncasecmp( str, "quit", 4 ) ) { sprintf( str, M221 ); write( 0, str, strlen( str ) ); return( 1 ); } else if( !strncasecmp( str, "lock ", 4 ) ) { char *q,*r; p = str + 5; q=p; while( *q != ' ' && *q != '\0' ) q++; if ( *q == '\0' ) { write( 0, M450, strlen( M450 ) ); return(0); } *q = '\0'; q++; r=q; while( *r != '\n' && *r != '\0' ) r++; if( *r == '\n' ) *r = '\0'; if ( LOCK( p, atoi( q ) ) ) { write( 0, M450, strlen( M450 ) ); } else { write( 0, M200, strlen( M200 ) ); } } else if( !strncasecmp( str, "unlock ", 6 ) ) { char *q,*r; p = str + 7; q=p; while( *q != ' ' && *q != '\0' ) q++; if ( *q == '\0' ) { write( 0, "q=0\n", strlen( "q=0\n" ) ); write( 0, M450, strlen( M450 ) ); return(0); } *q = '\0'; q++; r=q; while( *r != '\n' && *r != '\0' ) r++; if( *r == '\n' ) *r = '\0'; if ( UNLOCK( (const char *) p, atoi( q ) ) ) { write( 0, M450, strlen( M450 ) ); } else { write( 0, M200, strlen( M200 ) ); } } else { str[ret-2]='\0'; sprintf( str2, M500 ); write( 0, str2, strlen(str2)); } return( 0 ); }}int main( int argc, char **argv ){ char str[128]; char portstr[7]; struct sockaddr *cliaddr; struct sockaddr_in *sin; socklen_t len; openlog( argv[0], LOG_PID, 0 ); cliaddr= malloc( 128 ); len = 128; sin = ( struct sockaddr_in * ) cliaddr; cliaddr= malloc( 128 ); gethostname( hostname, 255 ); if ( !inet_ntop( AF_INET, &(sin->sin_addr), str, sizeof(str) ) ) str[0] = '\0'; if ( !ntohs( sin->sin_port ) ) { snprintf( portstr, sizeof( portstr ), ".%d", ntohs( sin->sin_port) ); strcat( str, portstr); } sprintf( str, M220 ); //syslog( LOG_INFO, str ); write( 0, str, strlen(str) ); for(;;) { if (process_requests( )) { goto exit; } } syslog( LOG_INFO, "Lock Daemon Shutting down" );exit: closelog(); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -