| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
int fhs_lock( const char *filename, int pid ) |
| 114 |
{ |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
int fd,j; |
| •123 |
char lockinfo[12]; |
| 124 |
char *file, *p, *message; |
| 125 |
|
| 126 |
j = strlen( filename ); |
| 127 |
p = ( char * ) filename + j; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
while( *( p - 1 ) != '/' && j-- != 1 ) |
| 133 |
{ |
| 134 |
#if defined ( __unixware__ ) |
| 135 |
*p = tolower( *p ); |
| 136 |
#endif |
| 137 |
p--; |
| 138 |
} |
| 139 |
if ( check_lock_status( filename ) ) |
| 140 |
{ |
| 141 |
|
| 142 |
return 1; |
| 143 |
} |
| •144 |
asprintf( &file, "%s/LCK..%s", LOCKDIR, p ); |
| 145 |
fd = open( file, O_CREAT | O_WRONLY | O_EXCL, 0444 ); |
| 146 |
if( fd < 0 ) |
| 147 |
{ |
| •148 |
asprintf( &message, |
| 149 |
"RXTX fhs_lock() Error: creating lock file: %s: %s\n", |
| 150 |
file, strerror(errno) ); |
| 151 |
syslog( LOG_INFO, message ); |
| •152 |
free(message); |
| 153 |
free(file); |
| 154 |
return 1; |
| 155 |
} |
| 156 |
sprintf( lockinfo, "%10d\n", pid ); |
| •157 |
|
| 158 |
|
| •159 |
|
| 160 |
free(file); |
| 161 |
write( fd, lockinfo, 11 ); |
| 162 |
close( fd ); |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
int uucp_lock( const char *filename, int pid ) |
| 209 |
{ |
| 210 |
char lockfilename[80], lockinfo[12], message[80]; |
| 211 |
char name[80]; |
| 212 |
int fd; |
| 213 |
struct stat buf; |
| 214 |
|
| 215 |
sprintf( message, "uucp_lock( %s );\n", filename ); |
| 216 |
syslog( LOG_INFO, message ); |
| 217 |
|
| 218 |
if ( check_lock_status( filename ) ) |
| 219 |
{ |
| 220 |
syslog( LOG_INFO, "RXTX uucp check_lock_status true\n" ); |
| 221 |
return 1; |
| 222 |
} |
| 223 |
if ( stat( LOCKDIR, &buf ) != 0 ) |
| 224 |
{ |
| 225 |
syslog( LOG_INFO, "RXTX uucp_lock() could not find lock directory.\n" ); |
| 226 |
return 1; |
| 227 |
} |
| 228 |
if ( stat( filename, &buf ) != 0 ) |
| 229 |
{ |
| 230 |
syslog( LOG_INFO, "RXTX uucp_lock() could not find device.\n" ); |
| 231 |
sprintf( message, "uucp_lock: device was %s\n", name ); |
| 232 |
syslog( LOG_INFO, message ); |
| 233 |
sprintf( message, "Filename is : %s \n", filename ); |
| 234 |
syslog( LOG_INFO, message ); |
| 235 |
return 1; |
| 236 |
} |
| 237 |
sprintf( lockfilename, "%s/LK.%03d.%03d.%03d", |
| 238 |
LOCKDIR, |
| 239 |
(int) major( buf.st_dev ), |
| 240 |
(int) major( buf.st_rdev ), |
| 241 |
(int) minor( buf.st_rdev ) |
| 242 |
); |
| 243 |
sprintf( lockinfo, "%10d\n", pid ); |
| 244 |
if ( stat( lockfilename, &buf ) == 0 ) |
| 245 |
{ |
| 246 |
sprintf( message, "RXTX uucp_lock() %s is there\n", |
| 247 |
lockfilename ); |
| 248 |
syslog( LOG_INFO, message ); |
| 249 |
syslog( LOG_INFO, message ); |
| 250 |
return 1; |
| 251 |
} |
| 252 |
fd = open( lockfilename, O_CREAT | O_WRONLY | O_EXCL, 0444 ); |
| 253 |
if( fd < 0 ) |
| 254 |
{ |
| 255 |
sprintf( message, |
| 256 |
"RXTX uucp_lock() Error: creating lock file: %s\n", |
| 257 |
lockfilename ); |
| 258 |
syslog( LOG_INFO, message ); |
| 259 |
return 1; |
| 260 |
} |
| 261 |
write( fd, lockinfo,11 ); |
| 262 |
close( fd ); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
int check_lock_status( const char *filename ) |
| 280 |
{ |
| 281 |
struct stat buf; |
| 282 |
|
| 283 |
|
| 284 |
if ( stat( LOCKDIR, &buf ) != 0 ) |
| 285 |
{ |
| 286 |
syslog( LOG_INFO, "check_lock_status: could not find lock directory.\n" ); |
| 287 |
return 1; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
if ( check_group_uucp() ) |
| 293 |
{ |
| 294 |
syslog( LOG_INFO, "check_lock_status: No permission to create lock file. |
| 295 |
|
| 296 |
please see: How can I use Lock Files with rxtx? in INSTALL\n" ); |
| 297 |
return 1; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
if ( is_device_locked( filename ) ) |
| 303 |
{ |
| 304 |
|
| 305 |
return 1; |
| 306 |
} |
| 307 |
return 0; |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
int fhs_unlock( const char *filename, int openpid ) |
| 323 |
{ |
| 324 |
char file[80],*p, msg[80]; |
| 325 |
struct stat buf; |
| 326 |
int i; |
| 327 |
|
| 328 |
i = strlen( filename ); |
| 329 |
p = ( char * ) filename + i; |
| 330 |
sprintf( msg, "fhs_unlock %s\n", filename ); |
| 331 |
|
| 332 |
|
| 333 |
while( *( p - 1 ) != '/' && i-- != 1 ) p--; |
| 334 |
sprintf( file, "%s/LCK..%s", LOCKDIR, p ); |
| 335 |
#ifdef asdf |
| 336 |
if ( ! check_lock_status( p ) ) |
| 337 |
{ |
| 338 |
sprintf( msg, "fhs_unlock %s check_lock_status\n", filename ); |
| 339 |
syslog( LOG_INFO, msg ); |
| 340 |
return 0; |
| 341 |
} |
| 342 |
#endif |
| 343 |
if ( stat( filename, &buf ) != 0 ) |
| 344 |
{ |
| 345 |
|
| 346 |
syslog( LOG_INFO, "uucp_unlock() no such device\n" ); |
| 347 |
return(0); |
| 348 |
} |
| 349 |
|
| 350 |
if( !check_lock_pid( file, openpid ) ) |
| 351 |
{ |
| 352 |
unlink(file); |
| 353 |
|
| 354 |
return( 0 ); |
| 355 |
} |
| 356 |
else |
| 357 |
{ |
| 358 |
syslog( LOG_INFO,"fhs_unlock: Unable to remove LockFile\n"); |
| 359 |
return( 1 ); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
void uucp_unlock( const char *filename, int openpid ) |
| 373 |
{ |
| 374 |
struct stat buf; |
| 375 |
char file[80], message[80]; |
| 376 |
|
| 377 |
|
| 378 |
sprintf( message, "uucp_unlock( %s );\n", filename ); |
| 379 |
syslog( LOG_INFO, message ); |
| 380 |
|
| 381 |
if ( stat( filename, &buf ) != 0 ) |
| 382 |
{ |
| 383 |
|
| 384 |
syslog( LOG_INFO, "uucp_unlock() no such device\n" ); |
| 385 |
return; |
| 386 |
} |
| 387 |
sprintf( file, LOCKDIR"/LK.%03d.%03d.%03d", |
| 388 |
(int) major( buf.st_dev ), |
| 389 |
(int) major( buf.st_rdev ), |
| 390 |
(int) minor( buf.st_rdev ) |
| 391 |
); |
| 392 |
if ( stat( file, &buf ) != 0 ) |
| 393 |
{ |
| 394 |
|
| 395 |
syslog( LOG_INFO, "uucp_unlock no such lockfile\n" ); |
| 396 |
return; |
| 397 |
} |
| 398 |
if( !check_lock_pid( file, openpid ) ) |
| 399 |
{ |
| 400 |
sprintf( message, "uucp_unlock: unlinking %s\n", file ); |
| 401 |
syslog( LOG_INFO, message ); |
| 402 |
unlink(file); |
| 403 |
} |
| 404 |
else |
| 405 |
{ |
| 406 |
sprintf( message, "uucp_unlock: unlinking failed %s\n", file ); |
| 407 |
syslog( LOG_INFO, message ); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
int check_lock_pid( const char *file, int openpid ) |
| 421 |
{ |
| 422 |
int fd, lockpid; |
| 423 |
char pid_buffer[12]; |
| 424 |
char message[80]; |
| 425 |
|
| 426 |
fd=open( file, O_RDONLY ); |
| 427 |
if ( fd < 0 ) |
| 428 |
{ |
| 429 |
return( 1 ); |
| 430 |
} |
| 431 |
if ( read( fd, pid_buffer, 11 ) < 0 ) |
| 432 |
{ |
| 433 |
close( fd ); |
| 434 |
return( 1 ); |
| 435 |
} |
| 436 |
close( fd ); |
| 437 |
pid_buffer[11] = '\0'; |
| 438 |
lockpid = atol( pid_buffer ); |
| 439 |
if ( lockpid != openpid ) |
| 440 |
{ |
| 441 |
if( kill( (pid_t) lockpid, 0 ) && errno==ESRCH ) |
| 442 |
{ |
| 443 |
return( 0 ); |
| 444 |
} |
| 445 |
sprintf(message, "check_lock_pid: lock = %s pid = %i gpid=%i openpid=%i\n", |
| 446 |
pid_buffer, (int) getpid(), (int) getppid(), openpid ); |
| 447 |
syslog( LOG_INFO, message ); |
| 448 |
return( 1 ); |
| 449 |
} |
| 450 |
return( 0 ); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
int check_group_uucp() |
| 472 |
{ |
| 473 |
#ifndef USER_LOCK_DIRECTORY |
| 474 |
int group_count; |
| 475 |
struct passwd *user = getpwuid( geteuid() ); |
| 476 |
struct stat buf; |
| 477 |
char msg[80]; |
| 478 |
gid_t list[ NGROUPS_MAX ]; |
| 479 |
|
| 480 |
if( stat( LOCKDIR, &buf) ) |
| 481 |
{ |
| 482 |
sprintf( msg, "check_group_uucp: Can not find Lock Directory: %s\n", LOCKDIR ); |
| 483 |
syslog( LOG_INFO, msg ); |
| 484 |
return( 1 ); |
| 485 |
} |
| 486 |
group_count = getgroups( NGROUPS_MAX, list ); |
| 487 |
list[ group_count ] = geteuid(); |
| 488 |
|
| 489 |
if( user->pw_gid ) |
| 490 |
{ |
| 491 |
while( group_count >= 0 && buf.st_gid != list[ group_count ] ) |
| 492 |
{ |
| 493 |
group_count--; |
| 494 |
} |
| 495 |
if( buf.st_gid == list[ group_count ] ) |
| 496 |
return 0; |
| 497 |
sprintf( msg, "%i %i\n", buf.st_gid, list[ group_count ] ); |
| 498 |
syslog( LOG_INFO, msg ); |
| 499 |
syslog( LOG_INFO, UUCP_ERROR ); |
| 500 |
return 1; |
| 501 |
} |
| 502 |
return 0; |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
#endif |
| 522 |
return 0; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
int is_device_locked( const char *port_filename ) |
| 562 |
{ |
| 563 |
const char *lockdirs[] = { "/etc/locks", "/usr/spool/kermit", |
| 564 |
"/usr/spool/locks", "/usr/spool/uucp", "/usr/spool/uucp/", |
| 565 |
"/usr/spool/uucp/LCK", "/var/lock", "/var/lock/modem", |
| 566 |
"/var/spool/lock", "/var/spool/locks", "/var/spool/uucp", |
| 567 |
LOCKDIR, NULL |
| 568 |
}; |
| 569 |
const char *lockprefixes[] = { "LCK..", "lk..", "LK.", NULL }; |
| •570 |
char *p, *file, pid_buffer[20], *message; |
| 571 |
int i = 0, j, k, fd , pid; |
| 572 |
struct stat buf; |
| 573 |
struct stat buf2; |
| 574 |
|
| 575 |
j = strlen( port_filename ); |
| 576 |
p = ( char * ) port_filename+j; |
| 577 |
while( *( p-1 ) != '/' && j-- !=1 ) p--; |
| 578 |
|
| 579 |
while( lockdirs[i] ) |
| 580 |
{ |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
if( !stat( lockdirs[i], &buf2 ) && |
| 590 |
strncmp( lockdirs[i], LOCKDIR, strlen( lockdirs[i] ) ) ) |
| 591 |
{ |
| 592 |
j = strlen( port_filename ); |
| 593 |
p = ( char * ) port_filename + j; |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
while( *( p - 1 ) != '/' && j-- != 1 ) |
| 599 |
{ |
| 600 |
#if defined ( __unixware__ ) |
| 601 |
*p = tolower( *p ); |
| 602 |
#endif |
| 603 |
p--; |
| 604 |
} |
| 605 |
k=0; |
| 606 |
while ( lockprefixes[k] ) |
| 607 |
{ |
| 608 |
|
| •609 |
asprintf( &file, "%s/%s%s", lockdirs[i], |
| 610 |
lockprefixes[k], p ); |
| 611 |
if( stat( file, &buf ) == 0 ) |
| 612 |
{ |
| •613 |
asprintf( &message, UNEXPECTED_LOCK_FILE, |
| 614 |
file ); |
| 615 |
syslog( LOG_INFO, message ); |
| •616 |
free( message ); |
| 617 |
free( file ); |
| 618 |
return 1; |
| 619 |
} |
| •620 |
free( file ); |
| 621 |
|
| 622 |
|
| 623 |
stat(port_filename , &buf ); |
| •624 |
asprintf( &file, "%s/%s%03d.%03d.%03d", |
| 625 |
lockdirs[i], |
| 626 |
lockprefixes[k], |
| 627 |
(int) major( buf.st_dev ), |
| 628 |
(int) major( buf.st_rdev ), |
| 629 |
(int) minor( buf.st_rdev ) |
| 630 |
); |
| 631 |
if( stat( file, &buf ) == 0 ) |
| 632 |
{ |
| •633 |
asprintf( &message, UNEXPECTED_LOCK_FILE, |
| 634 |
file ); |
| 635 |
syslog( LOG_INFO, message ); |
| •636 |
free( message ); |
| 637 |
free( file ); |
| 638 |
return 1; |
| 639 |
} |
| •640 |
free( file ); |
| 641 |
k++; |
| 642 |
} |
| 643 |
} |
| 644 |
i++; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
#ifdef FHS |
| 654 |
|
| 655 |
i = strlen( port_filename ); |
| 656 |
p = ( char * ) port_filename + i; |
| 657 |
while( *(p-1) != '/' && i-- != 1) |
| 658 |
{ |
| 659 |
#if defined ( __unixware__ ) |
| 660 |
*p = tolower( *p ); |
| 661 |
#endif |
| 662 |
p--; |
| 663 |
} |
| •664 |
asprintf( &file, "%s/%s%s", LOCKDIR, LOCKFILEPREFIX, p ); |
| 665 |
#else |
| 666 |
|
| •667 |
asprintf( &file, "%s/LK.%03d.%03d.%03d", |
| 668 |
LOCKDIR, |
| 669 |
(int) major( buf.st_dev ), |
| 670 |
(int) major( buf.st_rdev ), |
| 671 |
(int) minor( buf.st_rdev ) |
| 672 |
); |
| 673 |
|
| 674 |
#endif |
| 675 |
|
| 676 |
if( stat( file, &buf ) == 0 ) |
| 677 |
{ |
| 678 |
|
| 679 |
|
| 680 |
fd=open( file, O_RDONLY ); |
| 681 |
read( fd, pid_buffer, 11 ); |
| 682 |
|
| 683 |
close( fd ); |
| 684 |
sscanf( pid_buffer, "%d", &pid ); |
| •685 |
|
| 686 |
|
| •687 |
|
| 688 |
|
| 689 |
if( kill( (pid_t) pid, 0 ) && errno==ESRCH ) |
| 690 |
{ |
| •691 |
asprintf( &message, |
| 692 |
"RXTX Warning: Removing stale lock file. %s\n", |
| 693 |
file ); |
| 694 |
syslog( LOG_INFO, message ); |
| •695 |
free( message ); |
| 696 |
if( unlink( file ) != 0 ) |
| 697 |
{ |
| •698 |
asprintf( &message, "RXTX Error: Unable to \ |
| 699 |
remove stale lock file: %s\n", |
| 700 |
file |
| 701 |
); |
| 702 |
syslog( LOG_INFO, message ); |
| •703 |
free( message ); |
| 704 |
free( file ); |
| 705 |
return 0; |
| 706 |
} |
| 707 |
} |
| 708 |
else |
| 709 |
{ |
| •710 |
|
| 711 |
|
| •712 |
|
| 713 |
free( file ); |
| 714 |
return 1; |
| 715 |
} |
| 716 |
} |
| •717 |
free( file ); |
| 718 |
return 0; |
| 719 |
} |
| 720 |
int init( void ) |
| 721 |
{ |
| 722 |
pid_t pid; |
| 723 |
|
| 724 |
if( ( pid = fork() ) < 0 ) |
| 725 |
{ |
| 726 |
return(-1); |
| 727 |
} |
| 728 |
else if ( pid != 0 ) |
| 729 |
{ |
| 730 |
exit( 0 ); |
| 731 |
} |
| 732 |
setsid(); |
| 733 |
chdir("/"); |
| 734 |
umask( 0 ); |
| 735 |
return( 0 ); |
| 736 |
} |
| 737 |
int process_requests( ) |
| 738 |
{ |
| 739 |
for(;;) |
| 740 |
{ |
| 741 |
char str[80]; |
| 742 |
char str2[80]; |
| 743 |
char *p; |
| 744 |
int ret; |
| 745 |
|
| 746 |
ret = read( 1, str, 80 ); |
| 747 |
|
| 748 |
if( ret < 80 && ret > 1 ) |
| 749 |
str[ret] = '\0'; |
| 750 |
else |
| 751 |
str[79] = '\0'; |
| 752 |
if ( !strncasecmp( str, "quit", 4 ) ) |
| 753 |
{ |
| 754 |
sprintf( str, M221 ); |
| 755 |
write( 0, str, strlen( str ) ); |
| 756 |
return( 1 ); |
| 757 |
} |
| 758 |
else if( !strncasecmp( str, "lock ", 4 ) ) |
| 759 |
{ |
| 760 |
char *q,*r; |
| 761 |
p = str + 5; |
| 762 |
q=p; |
| 763 |
while( *q != ' ' && *q != '\0' ) |
| 764 |
q++; |
| 765 |
if ( *q == '\0' ) |
| 766 |
{ |
| 767 |
write( 0, M450, strlen( M450 ) ); |
| 768 |
return(0); |
| 769 |
} |
| 770 |
*q = '\0'; |
| 771 |
q++; |
| 772 |
r=q; |
| 773 |
while( *r != '\n' && *r != '\0' ) |
| 774 |
r++; |
| 775 |
if( *r == '\n' ) |
| 776 |
*r = '\0'; |
| 777 |
if ( LOCK( p, atoi( q ) ) ) |
| 778 |
{ |
| 779 |
write( 0, M450, strlen( M450 ) ); |
| 780 |
} |
| 781 |
else |
| 782 |
{ |
| 783 |
write( 0, M200, strlen( M200 ) ); |
| 784 |
} |
| 785 |
} |
| 786 |
else if( !strncasecmp( str, "unlock ", 6 ) ) |
| 787 |
{ |
| 788 |
char *q,*r; |
| 789 |
p = str + 7; |
| 790 |
q=p; |
| 791 |
while( *q != ' ' && *q != '\0' ) |
| 792 |
q++; |
| 793 |
if ( *q == '\0' ) |
| 794 |
{ |
| 795 |
write( 0, "q=0\n", strlen( "q=0\n" ) ); |
| 796 |
write( 0, M450, strlen( M450 ) ); |
| 797 |
return(0); |
| 798 |
} |
| 799 |
*q = '\0'; |
| 800 |
q++; |
| 801 |
r=q; |
| 802 |
while( *r != '\n' && *r != '\0' ) |
| 803 |
r++; |
| 804 |
if( *r == '\n' ) |
| 805 |
*r = '\0'; |
| 806 |
if ( UNLOCK( (const char *) p, atoi( q ) ) ) |
| 807 |
{ |
| 808 |
write( 0, M450, strlen( M450 ) ); |
| 809 |
} |
| 810 |
else |
| 811 |
{ |
| 812 |
write( 0, M200, strlen( M200 ) ); |
| 813 |
} |
| 814 |
} |
| 815 |
else |
| 816 |
{ |
| 817 |
str[ret-2]='\0'; |
| 818 |
sprintf( str2, M500 ); |
| 819 |
write( 0, str2, strlen(str2)); |
| 820 |
} |
| 821 |
return( 0 ); |
| 822 |
} |
| 823 |
} |
| 824 |
int main( int argc, char **argv ) |
| 825 |
{ |
| 826 |
char str[128]; |
| 827 |
char portstr[7]; |
| 828 |
struct sockaddr *cliaddr; |
| 829 |
struct sockaddr_in *sin; |
| 830 |
socklen_t len; |
| 831 |
|
| 832 |
openlog( argv[0], LOG_PID, 0 ); |
| 833 |
cliaddr= malloc( 128 ); |
| 834 |
len = 128; |
| 835 |
sin = ( struct sockaddr_in * ) cliaddr; |
| 836 |
cliaddr= malloc( 128 ); |
| 837 |
gethostname( hostname, 255 ); |
| 838 |
if ( !inet_ntop( AF_INET, &(sin->sin_addr), str, sizeof(str) ) ) |
| 839 |
str[0] = '\0'; |
| 840 |
if ( !ntohs( sin->sin_port ) ) |
| 841 |
{ |
| 842 |
snprintf( portstr, sizeof( portstr ), ".%d", ntohs( sin->sin_port) ); |
| 843 |
strcat( str, portstr); |
| 844 |
} |
| 845 |
sprintf( str, M220 ); |
| 846 |
|
| 847 |
write( 0, str, strlen(str) ); |
| 848 |
for(;;) |
| 849 |
{ |
| 850 |
if (process_requests( )) |
| 851 |
{ |
| 852 |
goto exit; |
| 853 |
} |
| 854 |
} |
| 855 |
syslog( LOG_INFO, "Lock Daemon Shutting down" ); |
| 856 |
exit: |
| 857 |
closelog(); |
| 858 |
exit(0); |
| 859 |
} |