Debian Codegraph @

src/lfd/lockdaemon.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
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];
124 char *file, *p, *message;
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 if ( check_lock_status( filename ) )
140 {
141 /* syslog( LOG_INFO, "fhs_lock() lockstatus fail\n" ); */
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 //asprintf( &message, "fhs_lock: creating lockfile: %s\n", lockinfo );
158 //syslog( LOG_INFO, message );
•159 //free(message);
160 free(file);
161 write( fd, lockinfo, 11 );
162 close( fd );
163 return 0;
164 }
165
166 /*----------------------------------------------------------
167 uucp_lock
168
169 accept: char * filename. Device to be locked
170 perform: Try to get a uucp_lock
171 return: int 0 on success
172 exceptions: none
173 comments:
174 The File System Hierarchy Standard
175 http://www.pathname.com/fhs/
176
177 UUCP Lock Files
178 http://docs.freebsd.org/info/uucp/uucp.info.UUCP_Lock_Files.html
179
180 FSSTND
181 ftp://tsx-11.mit.edu/pub/linux/docs/linux-standards/fsstnd/
182
183 Proposed Changes to the File System Hierarchy Standard
184 ftp://scicom.alphacdc.com/pub/linux/devlock-0.X.tgz
185
186 "UNIX Network Programming", W. Richard Stevens,
187 Prentice-Hall, 1990, pages 96-101.
188
189 There is much to do here.
190
191 1) UUCP style locks (done)
192 /var/spool/uucp
193 2) SVR4 locks
194 /var/spool/locks
195 3) FSSTND locks (done)
196 /var/lock
197 4) handle stale locks (done except kermit locks)
198 5) handle minicom lockfile contents (FSSTND?)
199 " 16929 minicom root\n" (done)
200 6) there are other Lock conventions that use Major and Minor
201 numbers...
202 7) Stevens recommends LCK..<pid>
203
204 most are caught above. If they turn out to be problematic
205 rather than an exercise, we will handle them.
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 setgid( nobody );
265 setuid( nobody );
266 */
267 return 0;
268 }
269
270 /*----------------------------------------------------------
271 check_lock_status
272
273 accept: the lock name in question
274 perform: Make sure everything is sane
275 return: 0 on success
276 exceptions: none
277 comments:
278 ----------------------------------------------------------*/
279 int check_lock_status( const char *filename )
280 {
281 struct stat buf;
282 /* First, can we find the directory? */
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 /* OK. Are we able to write to it? If not lets bail */
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 /* is the device alread locked */
301
302 if ( is_device_locked( filename ) )
303 {
304 /* syslog( LOG_INFO, "check_lock_status: device is locked by another application\n" ); */
305 return 1;
306 }
307 return 0;
308
309 }
310
311 /*----------------------------------------------------------
312 fhs_unlock
313
314 accept: The name of the device to unlock
315 perform: delete the lock file
316 return: none
317 exceptions: none
318 comments: This is for linux only currently. I see SVR4 does this
319 differently and there are other proposed changes to the
320 Filesystem Hierachy Standard
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 /* syslog( LOG_INFO, msg ); */
332 /* FIXME need to handle subdirectories /dev/cua/... */
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 /* hmm the file is not there? */
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 //syslog( LOG_INFO,"fhs_unlock: Removing LockFile\n");
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 uucp_unlock
365
366 accept: char *filename the device that is locked
367 perform: remove the uucp lockfile if it exists
368 return: none
369 exceptions: none
370 comments: http://docs.freebsd.org/info/uucp/uucp.info.UUCP_Lock_Files.html
371 ----------------------------------------------------------*/
372 void uucp_unlock( const char *filename, int openpid )
373 {
374 struct stat buf;
375 char file[80], message[80];
376 /* FIXME */
377
378 sprintf( message, "uucp_unlock( %s );\n", filename );
379 syslog( LOG_INFO, message );
380
381 if ( stat( filename, &buf ) != 0 )
382 {
383 /* hmm the file is not there? */
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 /* hmm the file is not there? */
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 check_lock_pid
413
414 accept: the name of the lockfile
415 perform: make sure the lock file is ours.
416 return: 0 on success
417 exceptions: none
418 comments:
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 check_group_uucp
455
456 accept: none
457 perform: check if the user is root or in group uucp
458 return: 0 on success
459 exceptions: none
460 comments:
461 This checks if the effective user is in group uucp so we can
462 create lock files. If not we give them a warning and bail.
463 If its root we just skip the test.
464
465 if someone really wants to override this they can use the USER_LOCK_DIRECTORY --not recommended.
466
467 In a recent change RedHat 7.2 decided to use group lock.
468 In order to get around this we just check the group id
469 of the lock directory.
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 if( strcmp( user->pw_name, "root" ) )
505 {
506 while( *g->gr_mem )
507 {
508 if( !strcmp( *g->gr_mem, user->pw_name ) )
509 {
510 break;
511 }
512 (void) *g->gr_mem++;
513 }
514 if( !*g->gr_mem )
515 {
516 syslog( LOG_INFO, UUCP_ERROR );
517 return 1;
518 }
519 }
520 */
521 #endif /* USER_LOCK_DIRECTORY */
522 return 0;
523 }
524
525 /*----------------------------------------------------------
526 The following should be able to follow symbolic links. I think the stat
527 method used below will work on more systems. This was found while looking
528 for information.
529
530 * realpath() doesn't exist on all of the systems my code has to run
531 on (HP-UX 9.x, specifically)
532 ----------------------------------------------------------
533 int different_from_LOCKDIR(const char* ld)
534 {
535 char real_ld[MAXPATHLEN];
536 char real_LOCKDIR[MAXPATHLEN];
537 if (strncmp(ld, LOCKDIR, strlen(ld)) == 0)
538 return 0;
539 if (realpath(ld, real_ld) == NULL)
540 return 1;
541 if (realpath(LOCKDIR, real_LOCKDIR) == NULL)
542 return 1;
543 if (strncmp(real_ld, real_LOCKDIR, strlen(real_ld)) == 0)
544 return 0;
545 else
546 return 1;
547 }
548 */
549
550 /*----------------------------------------------------------
551 is_device_locked
552
553 accept: char * filename. The device in question including the path.
554 perform: see if one of the many possible lock files is aready there
555 if there is a stale lock, remove it.
556 return: 1 if the device is locked or somethings wrong.
557 0 if its possible to create our own lock file.
558 exceptions: none
559 comments: check if the device is already locked
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 Look for lockfiles in all known places other than the
583 defined lock directory for this system
584 report any unexpected lockfiles.
585
586 Is the suspect lockdir there?
587 if it is there is it not the expected lock dir?
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 SCO Unix use lowercase all the time
596 taj
597 */
598 while( *( p - 1 ) != '/' && j-- != 1 )
599 {
600 #if defined ( __unixware__ )
601 *p = tolower( *p );
602 #endif /* __unixware__ */
603 p--;
604 }
605 k=0;
606 while ( lockprefixes[k] )
607 {
608 /* FHS style */
•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 /* UUCP style */
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 OK. We think there are no unexpect lock files for this device
649 Lets see if there any stale lock files that need to be
650 removed.
651 */
652
653 #ifdef FHS
654 /* FHS standard locks */
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 /* __unixware__ */
662 p--;
663 }
•664 asprintf( &file, "%s/%s%s", LOCKDIR, LOCKFILEPREFIX, p );
665 #else
666 /* UUCP standard locks */
•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 /* FHS */
675
676 if( stat( file, &buf ) == 0 )
677 {
678
679 /* check if its a stale lock */
680 fd=open( file, O_RDONLY );
681 read( fd, pid_buffer, 11 );
682 /* FIXME null terminiate pid_buffer? need to check in Solaris */
683 close( fd );
684 sscanf( pid_buffer, "%d", &pid );
•685 /* asprintf( &message, "found lock for %s with pid %i\n", file, pid ); */
686 /* syslog( LOG_INFO, message ); */
•687 /* free( message ); */
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 /* asprintf( &message, "could not kill %i\n", pid ); */
711 /* syslog( LOG_INFO, message ); */
•712 /* free( message ); */
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 //syslog( LOG_INFO, str );
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 }