| 1 |
/*------------------------------------------------------------------------- |
| 2 |
| RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. |
| 3 |
| RXTX is a native interface to serial ports in java. |
| 4 |
| Copyright 2002-2007 by Trent Jarvi [email protected] and others who |
| 5 |
| actually wrote it. See individual source files for more information. |
| 6 |
| |
| 7 |
| A copy of the LGPL v 2.1 may be found at |
| 8 |
| http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is |
| 9 |
| here for your convenience. |
| 10 |
| |
| 11 |
| This library is free software; you can redistribute it and/or |
| 12 |
| modify it under the terms of the GNU Lesser General Public |
| 13 |
| License as published by the Free Software Foundation; either |
| 14 |
| version 2.1 of the License, or (at your option) any later version. |
| 15 |
| |
| 16 |
| This library is distributed in the hope that it will be useful, |
| 17 |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 |
| Lesser General Public License for more details. |
| 20 |
| |
| 21 |
| An executable that contains no derivative of any portion of RXTX, but |
| 22 |
| is designed to work with RXTX by being dynamically linked with it, |
| 23 |
| is considered a "work that uses the Library" subject to the terms and |
| 24 |
| conditions of the GNU Lesser General Public License. |
| 25 |
| |
| 26 |
| The following has been added to the RXTX License to remove |
| 27 |
| any confusion about linking to RXTX. We want to allow in part what |
| 28 |
| section 5, paragraph 2 of the LGPL does not permit in the special |
| 29 |
| case of linking over a controlled interface. The intent is to add a |
| 30 |
| Java Specification Request or standards body defined interface in the |
| 31 |
| future as another exception but one is not currently available. |
| 32 |
| |
| 33 |
| http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface |
| 34 |
| |
| 35 |
| As a special exception, the copyright holders of RXTX give you |
| 36 |
| permission to link RXTX with independent modules that communicate with |
| 37 |
| RXTX solely through the Sun Microsytems CommAPI interface version 2, |
| 38 |
| regardless of the license terms of these independent modules, and to copy |
| 39 |
| and distribute the resulting combined work under terms of your choice, |
| 40 |
| provided that every copy of the combined work is accompanied by a complete |
| 41 |
| copy of the source code of RXTX (the version of RXTX used to produce the |
| 42 |
| combined work), being distributed under the terms of the GNU Lesser General |
| 43 |
| Public License plus this exception. An independent module is a |
| 44 |
| module which is not derived from or based on RXTX. |
| 45 |
| |
| 46 |
| Note that people who make modified versions of RXTX are not obligated |
| 47 |
| to grant this special exception for their modified versions; it is |
| 48 |
| their choice whether to do so. The GNU Lesser General Public License |
| 49 |
| gives permission to release a modified version without this exception; this |
| 50 |
| exception also makes it possible to release a modified version which |
| 51 |
| carries forward this exception. |
| 52 |
| |
| 53 |
| You should have received a copy of the GNU Lesser General Public |
| 54 |
| License along with this library; if not, write to the Free |
| 55 |
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 56 |
| All trademarks belong to their respective owners. |
| 57 |
--------------------------------------------------------------------------*/ |
| 58 |
#include <sys/types.h> |
| 59 |
#include <unistd.h> |
| 60 |
#include <sys/stat.h> |
| 61 |
#include <fcntl.h> |
| 62 |
#include <syslog.h> |
| 63 |
#include <sys/socket.h> |
| 64 |
#include <netdb.h> |
| 65 |
#include <stdio.h> |
| 66 |
#include <stdlib.h> |
| 67 |
#include <string.h> |
| 68 |
#include <arpa/inet.h> |
| 69 |
#include <errno.h> |
| 70 |
#include <signal.h> |
| 71 |
#include <pwd.h> |
| 72 |
|
| 73 |
#define FHS |
| 74 |
#define LOCKFILEPREFIX "LCK.." |
| 75 |
#define LOCK fhs_lock |
| 76 |
#define UNLOCK fhs_unlock |
| 77 |
#define LOCKDIR "/var/lock" |
| 78 |
|
| 79 |
char hostname[256]; |
| 80 |
#define M200 "200 Command okay.\n" |
| 81 |
#define M202 "202 Command not implemented\n" |
| 82 |
#define M220 "220 %s Lock File Server (Version rxtx-1.5-9) ready\n", hostname |
| 83 |
#define M221 "221 Thank you for using the Lock File service on %s\n", hostname |
| 84 |
#define M450 "450 : File busy.\n" |
| 85 |
#define M500 "500 '%s': command not understood\n", str |
| 86 |
#define M550 "550 : Permission denied.\n" |
| 87 |
|
| 88 |
int is_device_locked( const char * ); |
| 89 |
int check_group_uucp(); |
| 90 |
int check_lock_status( const char * ); |
| 91 |
int check_lock_pid( const char *, int ); |
| 92 |
|
| 93 |
#define UNEXPECTED_LOCK_FILE "RXTX Error: Unexpected lock file: %s\n Please report to the RXTX developers\n" |
| 94 |
#define UUCP_ERROR "\n\n\nRXTX WARNING: This library requires the user running applications to be in\ngroup uucp. Please consult the INSTALL documentation. More information is\navaiable under the topic 'How can I use Lock Files with rxtx?'\n" |
| 95 |
|
| 96 |
extern int errno; |
| 97 |
|
| 98 |
/*---------------------------------------------------------- |
| 99 |
fhs_lock |
| 100 |
|
| 101 |
accept: The name of the device to try to lock |
| 102 |
termios struct |
| 103 |
perform: Create a lock file if there is not one already. |
| 104 |
return: 1 on failure 0 on success |
| 105 |
exceptions: none |
| 106 |
comments: This is for linux and freebsd only currently. I see SVR4 does |
| 107 |
this differently and there are other proposed changes to the |
| 108 |
Filesystem Hierachy Standard |
| 109 |
|
| 110 |
more reading: |
| 111 |
|
| 112 |
----------------------------------------------------------*/ |
| 113 |
int fhs_lock( const char *filename, int pid ) |
| 114 |
{ |
| 115 |
/* |
| 116 |
* There is a zoo of lockdir possibilities |
| 117 |
* Its possible to check for stale processes with most of them. |
| 118 |
* for now we will just check for the lockfile on most |
| 119 |
* Problem lockfiles will be dealt with. Some may not even be in use. |
| 120 |
* |
| 121 |
*/ |
| 122 |
int fd,j; |
| 123 |
char lockinfo[12], message[80]; |
| 124 |
char file[80], *p; |
| 125 |
|
| 126 |
j = strlen( filename ); |
| 127 |
p = ( char * ) filename + j; |
| 128 |
/* FIXME need to handle subdirectories /dev/cua/... |
| 129 |
SCO Unix use lowercase all the time |
| 130 |
taj |
| 131 |
*/ |
| 132 |
while( *( p - 1 ) != '/' && j-- != 1 ) |
| 133 |
{ |
| 134 |
#if defined ( __unixware__ ) |
| 135 |
*p = tolower( *p ); |
| 136 |
#endif /* __unixware__ */ |
| 137 |
p--; |
| 138 |
} |
| 139 |
sprintf( file, "%s/LCK..%s", LOCKDIR, p ); |
| 140 |
if ( check_lock_status( filename ) ) |
| 141 |
{ |
| 142 |
/* syslog( LOG_INFO, "fhs_lock() lockstatus fail\n" ); */ |
| 143 |
return 1; |
| 144 |
} |
| 145 |
fd = open( file, O_CREAT | O_WRONLY | O_EXCL, 0444 ); |
| 146 |
if( fd < 0 ) |
| 147 |
{ |
| 148 |
sprintf( message, |
| 149 |
"RXTX fhs_lock() Error: creating lock file: %s: %s\n", |
| 150 |
file, strerror(errno) ); |
| 151 |
syslog( LOG_INFO, message ); |
| 152 |
return 1; |
| 153 |
} |
| 154 |
sprintf( lockinfo, "%10d\n", pid ); |
| 155 |
sprintf( message, "fhs_lock: creating lockfile: %s\n", lockinfo ); |
| 156 |
//syslog( LOG_INFO, message ); |
| 157 |
write( fd, lockinfo, 11 ); |
| 158 |
close( fd ); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
/*---------------------------------------------------------- |
| 163 |
uucp_lock |
| 164 |
|
| 165 |
accept: char * filename. Device to be locked |
| 166 |
perform: Try to get a uucp_lock |
| 167 |
return: int 0 on success |
| 168 |
exceptions: none |
| 169 |
comments: |
| 170 |
The File System Hierarchy Standard |
| 171 |
http://www.pathname.com/fhs/ |
| 172 |
|
| 173 |
UUCP Lock Files |
| 174 |
http://docs.freebsd.org/info/uucp/uucp.info.UUCP_Lock_Files.html |
| 175 |
|
| 176 |
FSSTND |
| 177 |
ftp://tsx-11.mit.edu/pub/linux/docs/linux-standards/fsstnd/ |
| 178 |
|
| 179 |
Proposed Changes to the File System Hierarchy Standard |
| 180 |
ftp://scicom.alphacdc.com/pub/linux/devlock-0.X.tgz |
| 181 |
|
| 182 |
"UNIX Network Programming", W. Richard Stevens, |
| 183 |
Prentice-Hall, 1990, pages 96-101. |
| 184 |
|
| 185 |
There is much to do here. |
| 186 |
|
| 187 |
1) UUCP style locks (done) |
| 188 |
/var/spool/uucp |
| 189 |
2) SVR4 locks |
| 190 |
/var/spool/locks |
| 191 |
3) FSSTND locks (done) |
| 192 |
/var/lock |
| 193 |
4) handle stale locks (done except kermit locks) |
| 194 |
5) handle minicom lockfile contents (FSSTND?) |
| 195 |
" 16929 minicom root\n" (done) |
| 196 |
6) there are other Lock conventions that use Major and Minor |
| 197 |
numbers... |
| 198 |
7) Stevens recommends LCK..<pid> |
| 199 |
|
| 200 |
most are caught above. If they turn out to be problematic |
| 201 |
rather than an exercise, we will handle them. |
| 202 |
|
| 203 |
----------------------------------------------------------*/ |
| 204 |
int uucp_lock( const char *filename, int pid ) |
| 205 |
{ |
| 206 |
char lockfilename[80], lockinfo[12], message[80]; |
| 207 |
char name[80]; |
| 208 |
int fd; |
| 209 |
struct stat buf; |
| 210 |
|
| 211 |
sprintf( message, "uucp_lock( %s );\n", filename ); |
| 212 |
syslog( LOG_INFO, message ); |
| 213 |
|
| 214 |
if ( check_lock_status( filename ) ) |
| 215 |
{ |
| 216 |
syslog( LOG_INFO, "RXTX uucp check_lock_status true\n" ); |
| 217 |
return 1; |
| 218 |
} |
| 219 |
if ( stat( LOCKDIR, &buf ) != 0 ) |
| 220 |
{ |
| 221 |
syslog( LOG_INFO, "RXTX uucp_lock() could not find lock directory.\n" ); |
| 222 |
return 1; |
| 223 |
} |
| 224 |
if ( stat( filename, &buf ) != 0 ) |
| 225 |
{ |
| 226 |
syslog( LOG_INFO, "RXTX uucp_lock() could not find device.\n" ); |
| 227 |
sprintf( message, "uucp_lock: device was %s\n", name ); |
| 228 |
syslog( LOG_INFO, message ); |
| 229 |
sprintf( message, "Filename is : %s \n", filename ); |
| 230 |
syslog( LOG_INFO, message ); |
| 231 |
return 1; |
| 232 |
} |
| 233 |
sprintf( lockfilename, "%s/LK.%03d.%03d.%03d", |
| 234 |
LOCKDIR, |
| 235 |
(int) major( buf.st_dev ), |
| 236 |
(int) major( buf.st_rdev ), |
| 237 |
(int) minor( buf.st_rdev ) |
| 238 |
); |
| 239 |
sprintf( lockinfo, "%10d\n", pid ); |
| 240 |
if ( stat( lockfilename, &buf ) == 0 ) |
| 241 |
{ |
| 242 |
sprintf( message, "RXTX uucp_lock() %s is there\n", |
| 243 |
lockfilename ); |
| 244 |
syslog( LOG_INFO, message ); |
| 245 |
syslog( LOG_INFO, message ); |
| 246 |
return 1; |
| 247 |
} |
| 248 |
fd = open( lockfilename, O_CREAT | O_WRONLY | O_EXCL, 0444 ); |
| 249 |
if( fd < 0 ) |
| 250 |
{ |
| 251 |
sprintf( message, |
| 252 |
"RXTX uucp_lock() Error: creating lock file: %s\n", |
| 253 |
lockfilename ); |
| 254 |
syslog( LOG_INFO, message ); |
| 255 |
return 1; |
| 256 |
} |
| 257 |
write( fd, lockinfo,11 ); |
| 258 |
close( fd ); |
| 259 |
/* |
| 260 |
setgid( nobody ); |
| 261 |
setuid( nobody ); |
| 262 |
*/ |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
/*---------------------------------------------------------- |
| 267 |
check_lock_status |
| 268 |
|
| 269 |
accept: the lock name in question |
| 270 |
perform: Make sure everything is sane |
| 271 |
return: 0 on success |
| 272 |
exceptions: none |
| 273 |
comments: |
| 274 |
----------------------------------------------------------*/ |
| 275 |
int check_lock_status( const char *filename ) |
| 276 |
{ |
| 277 |
struct stat buf; |
| 278 |
/* First, can we find the directory? */ |
| 279 |
|
| 280 |
if ( stat( LOCKDIR, &buf ) != 0 ) |
| 281 |
{ |
| 282 |
syslog( LOG_INFO, "check_lock_status: could not find lock directory.\n" ); |
| 283 |
return 1; |
| 284 |
} |
| 285 |
|
| 286 |
/* OK. Are we able to write to it? If not lets bail */ |
| 287 |
|
| 288 |
if ( check_group_uucp() ) |
| 289 |
{ |
| 290 |
syslog( LOG_INFO, "check_lock_status: No permission to create lock file. |
| 291 |
|
| 292 |
please see: How can I use Lock Files with rxtx? in INSTALL\n" ); |
| 293 |
return 1; |
| 294 |
} |
| 295 |
|
| 296 |
/* is the device alread locked */ |
| 297 |
|
| 298 |
if ( is_device_locked( filename ) ) |
| 299 |
{ |
| 300 |
/* syslog( LOG_INFO, "check_lock_status: device is locked by another application\n" ); */ |
| 301 |
return 1; |
| 302 |
} |
| 303 |
return 0; |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
/*---------------------------------------------------------- |
| 308 |
fhs_unlock |
| 309 |
|
| 310 |
accept: The name of the device to unlock |
| 311 |
perform: delete the lock file |
| 312 |
return: none |
| 313 |
exceptions: none |
| 314 |
comments: This is for linux only currently. I see SVR4 does this |
| 315 |
differently and there are other proposed changes to the |
| 316 |
Filesystem Hierachy Standard |
| 317 |
----------------------------------------------------------*/ |
| 318 |
int fhs_unlock( const char *filename, int openpid ) |
| 319 |
{ |
| 320 |
char file[80],*p, msg[80]; |
| 321 |
struct stat buf; |
| 322 |
int i; |
| 323 |
|
| 324 |
i = strlen( filename ); |
| 325 |
p = ( char * ) filename + i; |
| 326 |
sprintf( msg, "fhs_unlock %s\n", filename ); |
| 327 |
/* syslog( LOG_INFO, msg ); */ |
| 328 |
/* FIXME need to handle subdirectories /dev/cua/... */ |
| 329 |
while( *( p - 1 ) != '/' && i-- != 1 ) p--; |
| 330 |
sprintf( file, "%s/LCK..%s", LOCKDIR, p ); |
| 331 |
#ifdef asdf |
| 332 |
if ( ! check_lock_status( p ) ) |
| 333 |
{ |
| 334 |
sprintf( msg, "fhs_unlock %s check_lock_status\n", filename ); |
| 335 |
syslog( LOG_INFO, msg ); |
| 336 |
return 0; |
| 337 |
} |
| 338 |
#endif |
| 339 |
if ( stat( filename, &buf ) != 0 ) |
| 340 |
{ |
| 341 |
/* hmm the file is not there? */ |
| 342 |
syslog( LOG_INFO, "uucp_unlock() no such device\n" ); |
| 343 |
return(0); |
| 344 |
} |
| 345 |
|
| 346 |
if( !check_lock_pid( file, openpid ) ) |
| 347 |
{ |
| 348 |
unlink(file); |
| 349 |
//syslog( LOG_INFO,"fhs_unlock: Removing LockFile\n"); |
| 350 |
return( 0 ); |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
syslog( LOG_INFO,"fhs_unlock: Unable to remove LockFile\n"); |
| 355 |
return( 1 ); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
/*---------------------------------------------------------- |
| 360 |
uucp_unlock |
| 361 |
|
| 362 |
accept: char *filename the device that is locked |
| 363 |
perform: remove the uucp lockfile if it exists |
| 364 |
return: none |
| 365 |
exceptions: none |
| 366 |
comments: http://docs.freebsd.org/info/uucp/uucp.info.UUCP_Lock_Files.html |
| 367 |
----------------------------------------------------------*/ |
| 368 |
void uucp_unlock( const char *filename, int openpid ) |
| 369 |
{ |
| 370 |
struct stat buf; |
| 371 |
char file[80], message[80]; |
| 372 |
/* FIXME */ |
| 373 |
|
| 374 |
sprintf( message, "uucp_unlock( %s );\n", filename ); |
| 375 |
syslog( LOG_INFO, message ); |
| 376 |
|
| 377 |
if ( stat( filename, &buf ) != 0 ) |
| 378 |
{ |
| 379 |
/* hmm the file is not there? */ |
| 380 |
syslog( LOG_INFO, "uucp_unlock() no such device\n" ); |
| 381 |
return; |
| 382 |
} |
| 383 |
sprintf( file, LOCKDIR"/LK.%03d.%03d.%03d", |
| 384 |
(int) major( buf.st_dev ), |
| 385 |
(int) major( buf.st_rdev ), |
| 386 |
(int) minor( buf.st_rdev ) |
| 387 |
); |
| 388 |
if ( stat( file, &buf ) != 0 ) |
| 389 |
{ |
| 390 |
/* hmm the file is not there? */ |
| 391 |
syslog( LOG_INFO, "uucp_unlock no such lockfile\n" ); |
| 392 |
return; |
| 393 |
} |
| 394 |
if( !check_lock_pid( file, openpid ) ) |
| 395 |
{ |
| 396 |
sprintf( message, "uucp_unlock: unlinking %s\n", file ); |
| 397 |
syslog( LOG_INFO, message ); |
| 398 |
unlink(file); |
| 399 |
} |
| 400 |
else |
| 401 |
{ |
| 402 |
sprintf( message, "uucp_unlock: unlinking failed %s\n", file ); |
| 403 |
syslog( LOG_INFO, message ); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
/*---------------------------------------------------------- |
| 408 |
check_lock_pid |
| 409 |
|
| 410 |
accept: the name of the lockfile |
| 411 |
perform: make sure the lock file is ours. |
| 412 |
return: 0 on success |
| 413 |
exceptions: none |
| 414 |
comments: |
| 415 |
----------------------------------------------------------*/ |
| 416 |
int check_lock_pid( const char *file, int openpid ) |
| 417 |
{ |
| 418 |
int fd, lockpid; |
| 419 |
char pid_buffer[12]; |
| 420 |
char message[80]; |
| 421 |
|
| 422 |
fd=open( file, O_RDONLY ); |
| 423 |
if ( fd < 0 ) |
| 424 |
{ |
| 425 |
return( 1 ); |
| 426 |
} |
| 427 |
if ( read( fd, pid_buffer, 11 ) < 0 ) |
| 428 |
{ |
| 429 |
close( fd ); |
| 430 |
return( 1 ); |
| 431 |
} |
| 432 |
close( fd ); |
| 433 |
pid_buffer[11] = '\0'; |
| 434 |
lockpid = atol( pid_buffer ); |
| 435 |
if ( lockpid != openpid ) |
| 436 |
{ |
| 437 |
if( kill( (pid_t) lockpid, 0 ) && errno==ESRCH ) |
| 438 |
{ |
| 439 |
return( 0 ); |
| 440 |
} |
| 441 |
sprintf(message, "check_lock_pid: lock = %s pid = %i gpid=%i openpid=%i\n", |
| 442 |
pid_buffer, (int) getpid(), (int) getppid(), openpid ); |
| 443 |
syslog( LOG_INFO, message ); |
| 444 |
return( 1 ); |
| 445 |
} |
| 446 |
return( 0 ); |
| 447 |
} |
| 448 |
|
| 449 |
/*---------------------------------------------------------- |
| 450 |
check_group_uucp |
| 451 |
|
| 452 |
accept: none |
| 453 |
perform: check if the user is root or in group uucp |
| 454 |
return: 0 on success |
| 455 |
exceptions: none |
| 456 |
comments: |
| 457 |
This checks if the effective user is in group uucp so we can |
| 458 |
create lock files. If not we give them a warning and bail. |
| 459 |
If its root we just skip the test. |
| 460 |
|
| 461 |
if someone really wants to override this they can use the USER_LOCK_DIRECTORY --not recommended. |
| 462 |
|
| 463 |
In a recent change RedHat 7.2 decided to use group lock. |
| 464 |
In order to get around this we just check the group id |
| 465 |
of the lock directory. |
| 466 |
----------------------------------------------------------*/ |
| 467 |
int check_group_uucp() |
| 468 |
{ |
| 469 |
#ifndef USER_LOCK_DIRECTORY |
| 470 |
int group_count; |
| 471 |
struct passwd *user = getpwuid( geteuid() ); |
| 472 |
struct stat buf; |
| 473 |
char msg[80]; |
| 474 |
gid_t list[ NGROUPS_MAX ]; |
| 475 |
|
| 476 |
if( stat( LOCKDIR, &buf) ) |
| 477 |
{ |
| 478 |
sprintf( msg, "check_group_uucp: Can not find Lock Directory: %s\n", LOCKDIR ); |
| 479 |
syslog( LOG_INFO, msg ); |
| 480 |
return( 1 ); |
| 481 |
} |
| 482 |
group_count = getgroups( NGROUPS_MAX, list ); |
| 483 |
list[ group_count ] = geteuid(); |
| 484 |
|
| 485 |
if( user->pw_gid ) |
| 486 |
{ |
| 487 |
while( group_count >= 0 && buf.st_gid != list[ group_count ] ) |
| 488 |
{ |
| 489 |
group_count--; |
| 490 |
} |
| 491 |
if( buf.st_gid == list[ group_count ] ) |
| 492 |
return 0; |
| 493 |
sprintf( msg, "%i %i\n", buf.st_gid, list[ group_count ] ); |
| 494 |
syslog( LOG_INFO, msg ); |
| 495 |
syslog( LOG_INFO, UUCP_ERROR ); |
| 496 |
return 1; |
| 497 |
} |
| 498 |
return 0; |
| 499 |
/* |
| 500 |
if( strcmp( user->pw_name, "root" ) ) |
| 501 |
{ |
| 502 |
while( *g->gr_mem ) |
| 503 |
{ |
| 504 |
if( !strcmp( *g->gr_mem, user->pw_name ) ) |
| 505 |
{ |
| 506 |
break; |
| 507 |
} |
| 508 |
(void) *g->gr_mem++; |
| 509 |
} |
| 510 |
if( !*g->gr_mem ) |
| 511 |
{ |
| 512 |
syslog( LOG_INFO, UUCP_ERROR ); |
| 513 |
return 1; |
| 514 |
} |
| 515 |
} |
| 516 |
*/ |
| 517 |
#endif /* USER_LOCK_DIRECTORY */ |
| 518 |
return 0; |
| 519 |
} |
| 520 |
|
| 521 |
/*---------------------------------------------------------- |
| 522 |
The following should be able to follow symbolic links. I think the stat |
| 523 |
method used below will work on more systems. This was found while looking |
| 524 |
for information. |
| 525 |
|
| 526 |
* realpath() doesn't exist on all of the systems my code has to run |
| 527 |
on (HP-UX 9.x, specifically) |
| 528 |
---------------------------------------------------------- |
| 529 |
int different_from_LOCKDIR(const char* ld) |
| 530 |
{ |
| 531 |
char real_ld[MAXPATHLEN]; |
| 532 |
char real_LOCKDIR[MAXPATHLEN]; |
| 533 |
if (strncmp(ld, LOCKDIR, strlen(ld)) == 0) |
| 534 |
return 0; |
| 535 |
if (realpath(ld, real_ld) == NULL) |
| 536 |
return 1; |
| 537 |
if (realpath(LOCKDIR, real_LOCKDIR) == NULL) |
| 538 |
return 1; |
| 539 |
if (strncmp(real_ld, real_LOCKDIR, strlen(real_ld)) == 0) |
| 540 |
return 0; |
| 541 |
else |
| 542 |
return 1; |
| 543 |
} |
| 544 |
*/ |
| 545 |
|
| 546 |
/*---------------------------------------------------------- |
| 547 |
is_device_locked |
| 548 |
|
| 549 |
accept: char * filename. The device in question including the path. |
| 550 |
perform: see if one of the many possible lock files is aready there |
| 551 |
if there is a stale lock, remove it. |
| 552 |
return: 1 if the device is locked or somethings wrong. |
| 553 |
0 if its possible to create our own lock file. |
| 554 |
exceptions: none |
| 555 |
comments: check if the device is already locked |
| 556 |
----------------------------------------------------------*/ |
| 557 |
int is_device_locked( const char *port_filename ) |
| 558 |
{ |
| 559 |
const char *lockdirs[] = { "/etc/locks", "/usr/spool/kermit", |
| 560 |
"/usr/spool/locks", "/usr/spool/uucp", "/usr/spool/uucp/", |
| 561 |
"/usr/spool/uucp/LCK", "/var/lock", "/var/lock/modem", |
| 562 |
"/var/spool/lock", "/var/spool/locks", "/var/spool/uucp", |
| 563 |
LOCKDIR, NULL |
| 564 |
}; |
| 565 |
const char *lockprefixes[] = { "LCK..", "lk..", "LK.", NULL }; |
| 566 |
char *p, file[80], pid_buffer[20], message[80]; |
| 567 |
int i = 0, j, k, fd , pid; |
| 568 |
struct stat buf; |
| 569 |
struct stat buf2; |
| 570 |
|
| 571 |
j = strlen( port_filename ); |
| 572 |
p = ( char * ) port_filename+j; |
| 573 |
while( *( p-1 ) != '/' && j-- !=1 ) p--; |
| 574 |
|
| 575 |
while( lockdirs[i] ) |
| 576 |
{ |
| 577 |
/* |
| 578 |
Look for lockfiles in all known places other than the |
| 579 |
defined lock directory for this system |
| 580 |
report any unexpected lockfiles. |
| 581 |
|
| 582 |
Is the suspect lockdir there? |
| 583 |
if it is there is it not the expected lock dir? |
| 584 |
*/ |
| 585 |
if( !stat( lockdirs[i], &buf2 ) && |
| 586 |
strncmp( lockdirs[i], LOCKDIR, strlen( lockdirs[i] ) ) ) |
| 587 |
{ |
| 588 |
j = strlen( port_filename ); |
| 589 |
p = ( char * ) port_filename + j; |
| 590 |
/* |
| 591 |
SCO Unix use lowercase all the time |
| 592 |
taj |
| 593 |
*/ |
| 594 |
while( *( p - 1 ) != '/' && j-- != 1 ) |
| 595 |
{ |
| 596 |
#if defined ( __unixware__ ) |
| 597 |
*p = tolower( *p ); |
| 598 |
#endif /* __unixware__ */ |
| 599 |
p--; |
| 600 |
} |
| 601 |
k=0; |
| 602 |
while ( lockprefixes[k] ) |
| 603 |
{ |
| 604 |
/* FHS style */ |
| 605 |
sprintf( file, "%s/%s%s", lockdirs[i], |
| 606 |
lockprefixes[k], p ); |
| 607 |
if( stat( file, &buf ) == 0 ) |
| 608 |
{ |
| 609 |
sprintf( message, UNEXPECTED_LOCK_FILE, |
| 610 |
file ); |
| 611 |
syslog( LOG_INFO, message ); |
| 612 |
return 1; |
| 613 |
} |
| 614 |
|
| 615 |
/* UUCP style */ |
| 616 |
stat(port_filename , &buf ); |
| 617 |
sprintf( file, "%s/%s%03d.%03d.%03d", |
| 618 |
lockdirs[i], |
| 619 |
lockprefixes[k], |
| 620 |
(int) major( buf.st_dev ), |
| 621 |
(int) major( buf.st_rdev ), |
| 622 |
(int) minor( buf.st_rdev ) |
| 623 |
); |
| 624 |
if( stat( file, &buf ) == 0 ) |
| 625 |
{ |
| 626 |
sprintf( message, UNEXPECTED_LOCK_FILE, |
| 627 |
file ); |
| 628 |
syslog( LOG_INFO, message ); |
| 629 |
return 1; |
| 630 |
} |
| 631 |
k++; |
| 632 |
} |
| 633 |
} |
| 634 |
i++; |
| 635 |
} |
| 636 |
|
| 637 |
/* |
| 638 |
OK. We think there are no unexpect lock files for this device |
| 639 |
Lets see if there any stale lock files that need to be |
| 640 |
removed. |
| 641 |
*/ |
| 642 |
|
| 643 |
#ifdef FHS |
| 644 |
/* FHS standard locks */ |
| 645 |
i = strlen( port_filename ); |
| 646 |
p = ( char * ) port_filename + i; |
| 647 |
while( *(p-1) != '/' && i-- != 1) |
| 648 |
{ |
| 649 |
#if defined ( __unixware__ ) |
| 650 |
*p = tolower( *p ); |
| 651 |
#endif /* __unixware__ */ |
| 652 |
p--; |
| 653 |
} |
| 654 |
sprintf( file, "%s/%s%s", LOCKDIR, LOCKFILEPREFIX, p ); |
| 655 |
#else |
| 656 |
/* UUCP standard locks */ |
| 657 |
sprintf( file, "%s/LK.%03d.%03d.%03d", |
| 658 |
LOCKDIR, |
| 659 |
(int) major( buf.st_dev ), |
| 660 |
(int) major( buf.st_rdev ), |
| 661 |
(int) minor( buf.st_rdev ) |
| 662 |
); |
| 663 |
|
| 664 |
#endif /* FHS */ |
| 665 |
|
| 666 |
if( stat( file, &buf ) == 0 ) |
| 667 |
{ |
| 668 |
|
| 669 |
/* check if its a stale lock */ |
| 670 |
fd=open( file, O_RDONLY ); |
| 671 |
read( fd, pid_buffer, 11 ); |
| 672 |
/* FIXME null terminiate pid_buffer? need to check in Solaris */ |
| 673 |
close( fd ); |
| 674 |
sscanf( pid_buffer, "%d", &pid ); |
| 675 |
sprintf( message, "found lock for %s with pid %i\n", file, pid ); |
| 676 |
/* syslog( LOG_INFO, message ); */ |
| 677 |
|
| 678 |
if( kill( (pid_t) pid, 0 ) && errno==ESRCH ) |
| 679 |
{ |
| 680 |
sprintf( message, |
| 681 |
"RXTX Warning: Removing stale lock file. %s\n", |
| 682 |
file ); |
| 683 |
syslog( LOG_INFO, message ); |
| 684 |
if( unlink( file ) != 0 ) |
| 685 |
{ |
| 686 |
snprintf( message, 80, "RXTX Error: Unable to \ |
| 687 |
remove stale lock file: %s\n", |
| 688 |
file |
| 689 |
); |
| 690 |
syslog( LOG_INFO, message ); |
| 691 |
return 0; |
| 692 |
} |
| 693 |
} |
| 694 |
else |
| 695 |
{ |
| 696 |
sprintf( message, "could not kill %i\n", pid ); |
| 697 |
/* syslog( LOG_INFO, message ); */ |
| 698 |
return 1; |
| 699 |
} |
| 700 |
} |
| 701 |
return 0; |
| 702 |
} |
| 703 |
int init( void ) |
| 704 |
{ |
| 705 |
pid_t pid; |
| 706 |
|
| 707 |
if( ( pid = fork() ) < 0 ) |
| 708 |
{ |
| 709 |
return(-1); |
| 710 |
} |
| 711 |
else if ( pid != 0 ) |
| 712 |
{ |
| 713 |
exit( 0 ); |
| 714 |
} |
| 715 |
setsid(); |
| 716 |
chdir("/"); |
| 717 |
umask( 0 ); |
| 718 |
return( 0 ); |
| 719 |
} |
| 720 |
int process_requests( ) |
| 721 |
{ |
| 722 |
for(;;) |
| 723 |
{ |
| 724 |
char str[80]; |
| 725 |
char str2[80]; |
| 726 |
char *p; |
| 727 |
int ret; |
| 728 |
|
| 729 |
ret = read( 1, str, 80 ); |
| 730 |
|
| 731 |
if( ret < 80 && ret > 1 ) |
| 732 |
str[ret] = '\0'; |
| 733 |
else |
| 734 |
str[79] = '\0'; |
| 735 |
if ( !strncasecmp( str, "quit", 4 ) ) |
| 736 |
{ |
| 737 |
sprintf( str, M221 ); |
| 738 |
write( 0, str, strlen( str ) ); |
| 739 |
return( 1 ); |
| 740 |
} |
| 741 |
else if( !strncasecmp( str, "lock ", 4 ) ) |
| 742 |
{ |
| 743 |
char *q,*r; |
| 744 |
p = str + 5; |
| 745 |
q=p; |
| 746 |
while( *q != ' ' && *q != '\0' ) |
| 747 |
q++; |
| 748 |
if ( *q == '\0' ) |
| 749 |
{ |
| 750 |
write( 0, M450, strlen( M450 ) ); |
| 751 |
return(0); |
| 752 |
} |
| 753 |
*q = '\0'; |
| 754 |
q++; |
| 755 |
r=q; |
| 756 |
while( *r != '\n' && *r != '\0' ) |
| 757 |
r++; |
| 758 |
if( *r == '\n' ) |
| 759 |
*r = '\0'; |
| 760 |
if ( LOCK( p, atoi( q ) ) ) |
| 761 |
{ |
| 762 |
write( 0, M450, strlen( M450 ) ); |
| 763 |
} |
| 764 |
else |
| 765 |
{ |
| 766 |
write( 0, M200, strlen( M200 ) ); |
| 767 |
} |
| 768 |
} |
| 769 |
else if( !strncasecmp( str, "unlock ", 6 ) ) |
| 770 |
{ |
| 771 |
char *q,*r; |
| 772 |
p = str + 7; |
| 773 |
q=p; |
| 774 |
while( *q != ' ' && *q != '\0' ) |
| 775 |
q++; |
| 776 |
if ( *q == '\0' ) |
| 777 |
{ |
| 778 |
write( 0, "q=0\n", strlen( "q=0\n" ) ); |
| 779 |
write( 0, M450, strlen( M450 ) ); |
| 780 |
return(0); |
| 781 |
} |
| 782 |
*q = '\0'; |
| 783 |
q++; |
| 784 |
r=q; |
| 785 |
while( *r != '\n' && *r != '\0' ) |
| 786 |
r++; |
| 787 |
if( *r == '\n' ) |
| 788 |
*r = '\0'; |
| 789 |
if ( UNLOCK( (const char *) p, atoi( q ) ) ) |
| 790 |
{ |
| 791 |
write( 0, M450, strlen( M450 ) ); |
| 792 |
} |
| 793 |
else |
| 794 |
{ |
| 795 |
write( 0, M200, strlen( M200 ) ); |
| 796 |
} |
| 797 |
} |
| 798 |
else |
| 799 |
{ |
| 800 |
str[ret-2]='\0'; |
| 801 |
sprintf( str2, M500 ); |
| 802 |
write( 0, str2, strlen(str2)); |
| 803 |
} |
| 804 |
return( 0 ); |
| 805 |
} |
| 806 |
} |
| 807 |
int main( int argc, char **argv ) |
| 808 |
{ |
| 809 |
char str[128]; |
| 810 |
char portstr[7]; |
| 811 |
struct sockaddr *cliaddr; |
| 812 |
struct sockaddr_in *sin; |
| 813 |
socklen_t len; |
| 814 |
|
| 815 |
openlog( argv[0], LOG_PID, 0 ); |
| 816 |
cliaddr= malloc( 128 ); |
| 817 |
len = 128; |
| 818 |
sin = ( struct sockaddr_in * ) cliaddr; |
| 819 |
cliaddr= malloc( 128 ); |
| 820 |
gethostname( hostname, 255 ); |
| 821 |
if ( !inet_ntop( AF_INET, &(sin->sin_addr), str, sizeof(str) ) ) |
| 822 |
str[0] = '\0'; |
| 823 |
if ( !ntohs( sin->sin_port ) ) |
| 824 |
{ |
| 825 |
snprintf( portstr, sizeof( portstr ), ".%d", ntohs( sin->sin_port) ); |
| 826 |
strcat( str, portstr); |
| 827 |
} |
| 828 |
sprintf( str, M220 ); |
| 829 |
//syslog( LOG_INFO, str ); |
| 830 |
write( 0, str, strlen(str) ); |
| 831 |
for(;;) |
| 832 |
{ |
| 833 |
if (process_requests( )) |
| 834 |
{ |
| 835 |
goto exit; |
| 836 |
} |
| 837 |
} |
| 838 |
syslog( LOG_INFO, "Lock Daemon Shutting down" ); |
| 839 |
exit: |
| 840 |
closelog(); |
| 841 |
exit(0); |
| 842 |
} |