Debian Codegraph @

src/lfd/lockdaemon.c.noinetd

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
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
74 #define LOCK fhs_lock
75 #define UNLOCK fhs_unlock
76 #define LOCKDIR "/var/lock"
77
78 char hostname[256];
79 #define M200 "200 Command okay.\n"
80 #define M202 "202 Command not implemented\n"
81 #define M220 "220 %s Lock File Server (Version rxtx-1.5-9) ready\n", hostname
82 #define M221 "221 Thank you for using the Lock File service on %s\n", hostname
83 #define M450 "450 : File busy.\n"
84 #define M500 "500 '%s': command not understood\n", str
85 #define M550 "550 : Permission denied.\n"
86
87 int is_device_locked( const char * );
88 int check_group_uucp();
89 int check_lock_status( const char * );
90 int check_lock_pid( const char *, int );
91
92 #define UNEXPECTED_LOCK_FILE "RXTX Error: Unexpected lock file: %s\n Please report to the RXTX developers\n"
93 #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"
94
95 extern int errno;
96
97 /*----------------------------------------------------------
98 fhs_lock
99
100 accept: The name of the device to try to lock
101 termios struct
102 perform: Create a lock file if there is not one already.
103 return: 1 on failure 0 on success
104 exceptions: none
105 comments: This is for linux and freebsd only currently. I see SVR4 does
106 this differently and there are other proposed changes to the
107 Filesystem Hierachy Standard
108
109 more reading:
110
111 ----------------------------------------------------------*/
112 int fhs_lock( const char *filename, int pid )
113 {
114 /*
115 * There is a zoo of lockdir possibilities
116 * Its possible to check for stale processes with most of them.
117 * for now we will just check for the lockfile on most
118 * Problem lockfiles will be dealt with. Some may not even be in use.
119 *
120 */
121 int fd,j;
122 char lockinfo[12], message[80];
123 char file[80], *p;
124
125 j = strlen( filename );
126 p = ( char * ) filename + j;
127 /* FIXME need to handle subdirectories /dev/cua/...
128 SCO Unix use lowercase all the time
129 taj
130 */
131 while( *( p - 1 ) != '/' && j-- != 1 )
132 {
133 #if defined ( __unixware__ )
134 *p = tolower( *p );
135 #endif /* __unixware__ */
136 p--;
137 }
138 sprintf( file, "%s/LCK..%s", LOCKDIR, p );
139 if ( check_lock_status( filename ) )
140 {
141 syslog( LOG_INFO, "fhs_lock() lockstatus fail\n" );
142 return 1;
143 }
144 fd = open( file, O_CREAT | O_WRONLY | O_EXCL, 0444 );
145 if( fd < 0 )
146 {
147 snprintf( message, 79,
148 "RXTX fhs_lock() Error: creating lock file: %s: %s\n",
149 file, strerror(errno) );
150 syslog( LOG_INFO, message );
151 return 1;
152 }
153 sprintf( lockinfo, "%10d\n", pid );
154 sprintf( message, "fhs_lock: creating lockfile: %s\n", lockinfo );
155 syslog( LOG_INFO, message );
156 write( fd, lockinfo, 11 );
157 close( fd );
158 return 0;
159 }
160
161 /*----------------------------------------------------------
162 uucp_lock
163
164 accept: char * filename. Device to be locked
165 perform: Try to get a uucp_lock
166 return: int 0 on success
167 exceptions: none
168 comments:
169 The File System Hierarchy Standard
170 http://www.pathname.com/fhs/
171
172 UUCP Lock Files
173 http://docs.freebsd.org/info/uucp/uucp.info.UUCP_Lock_Files.html
174
175 FSSTND
176 ftp://tsx-11.mit.edu/pub/linux/docs/linux-standards/fsstnd/
177
178 Proposed Changes to the File System Hierarchy Standard
179 ftp://scicom.alphacdc.com/pub/linux/devlock-0.X.tgz
180
181 "UNIX Network Programming", W. Richard Stevens,
182 Prentice-Hall, 1990, pages 96-101.
183
184 There is much to do here.
185
186 1) UUCP style locks (done)
187 /var/spool/uucp
188 2) SVR4 locks
189 /var/spool/locks
190 3) FSSTND locks (done)
191 /var/lock
192 4) handle stale locks (done except kermit locks)
193 5) handle minicom lockfile contents (FSSTND?)
194 " 16929 minicom root\n" (done)
195 6) there are other Lock conventions that use Major and Minor
196 numbers...
197 7) Stevens recommends LCK..<pid>
198
199 most are caught above. If they turn out to be problematic
200 rather than an exercise, we will handle them.
201
202 ----------------------------------------------------------*/
203 int uucp_lock( const char *filename, int pid )
204 {
205 char lockfilename[80], lockinfo[12], message[80];
206 char name[80];
207 int fd;
208 struct stat buf;
209
210 sprintf( message, "uucp_lock( %s );\n", filename );
211 syslog( LOG_INFO, message );
212
213 if ( check_lock_status( filename ) )
214 {
215 syslog( LOG_INFO, "RXTX uucp check_lock_status true\n" );
216 return 1;
217 }
218 if ( stat( LOCKDIR, &buf ) != 0 )
219 {
220 syslog( LOG_INFO, "RXTX uucp_lock() could not find lock directory.\n" );
221 return 1;
222 }
223 if ( stat( filename, &buf ) != 0 )
224 {
225 syslog( LOG_INFO, "RXTX uucp_lock() could not find device.\n" );
226 sprintf( message, "uucp_lock: device was %s\n", name );
227 syslog( LOG_INFO, message );
228 sprintf( message, "Filename is : %s \n", filename );
229 syslog( LOG_INFO, message );
230 return 1;
231 }
232 sprintf( lockfilename, "%s/LK.%03d.%03d.%03d",
233 LOCKDIR,
234 (int) major( buf.st_dev ),
235 (int) major( buf.st_rdev ),
236 (int) minor( buf.st_rdev )
237 );
238 sprintf( lockinfo, "%10d\n", pid );
239 if ( stat( lockfilename, &buf ) == 0 )
240 {
241 sprintf( message, "RXTX uucp_lock() %s is there\n",
242 lockfilename );
243 syslog( LOG_INFO, message );
244 syslog( LOG_INFO, message );
245 return 1;
246 }
247 fd = open( lockfilename, O_CREAT | O_WRONLY | O_EXCL, 0444 );
248 if( fd < 0 )
249 {
250 sprintf( message,
251 "RXTX uucp_lock() Error: creating lock file: %s\n",
252 lockfilename );
253 syslog( LOG_INFO, message );
254 return 1;
255 }
256 write( fd, lockinfo,11 );
257 close( fd );
258 /*
259 setgid( nobody );
260 setuid( nobody );
261 */
262 return 0;
263 }
264
265 /*----------------------------------------------------------
266 check_lock_status
267
268 accept: the lock name in question
269 perform: Make sure everything is sane
270 return: 0 on success
271 exceptions: none
272 comments:
273 ----------------------------------------------------------*/
274 int check_lock_status( const char *filename )
275 {
276 struct stat buf;
277 /* First, can we find the directory? */
278
279 if ( stat( LOCKDIR, &buf ) != 0 )
280 {
281 syslog( LOG_INFO, "check_lock_status: could not find lock directory.\n" );
282 return 1;
283 }
284
285 /* OK. Are we able to write to it? If not lets bail */
286
287 if ( check_group_uucp() )
288 {
289 syslog( LOG_INFO, "check_lock_status: No permission to create lock file.
290
291 please see: How can I use Lock Files with rxtx? in INSTALL\n" );
292 return 1;
293 }
294
295 /* is the device alread locked */
296
297 if ( is_device_locked( filename ) )
298 {
299 syslog( LOG_INFO, "check_lock_status: device is locked by another application\n" );
300 return 1;
301 }
302 return 0;
303
304 }
305
306 /*----------------------------------------------------------
307 fhs_unlock
308
309 accept: The name of the device to unlock
310 perform: delete the lock file
311 return: none
312 exceptions: none
313 comments: This is for linux only currently. I see SVR4 does this
314 differently and there are other proposed changes to the
315 Filesystem Hierachy Standard
316 ----------------------------------------------------------*/
317 int fhs_unlock( const char *filename, int openpid )
318 {
319 char file[80],*p;
320 struct stat buf;
321 int i;
322
323 i = strlen( filename );
324 p = ( char * ) filename + i;
325 /* FIXME need to handle subdirectories /dev/cua/... */
326 while( *( p - 1 ) != '/' && i-- != 1 ) p--;
327 sprintf( file, "%s/LCK..%s", LOCKDIR, p );
328 if ( ! check_lock_status( p ) )
329 {
330 return 0;
331 }
332 if ( stat( filename, &buf ) != 0 )
333 {
334 /* hmm the file is not there? */
335 syslog( LOG_INFO, "uucp_unlock() no such device\n" );
336 return(0);
337 }
338
339 if( !check_lock_pid( file, openpid ) )
340 {
341 unlink(file);
342 syslog( LOG_INFO,"fhs_unlock: Removing LockFile\n");
343 return( 0 );
344 }
345 else
346 {
347 syslog( LOG_INFO,"fhs_unlock: Unable to remove LockFile\n");
348 return( 1 );
349 }
350 }
351
352 /*----------------------------------------------------------
353 uucp_unlock
354
355 accept: char *filename the device that is locked
356 perform: remove the uucp lockfile if it exists
357 return: none
358 exceptions: none
359 comments: http://docs.freebsd.org/info/uucp/uucp.info.UUCP_Lock_Files.html
360 ----------------------------------------------------------*/
361 void uucp_unlock( const char *filename, int openpid )
362 {
363 struct stat buf;
364 char file[80], message[80];
365 /* FIXME */
366
367 sprintf( message, "uucp_unlock( %s );\n", filename );
368 syslog( LOG_INFO, message );
369
370 if ( stat( filename, &buf ) != 0 )
371 {
372 /* hmm the file is not there? */
373 syslog( LOG_INFO, "uucp_unlock() no such device\n" );
374 return;
375 }
376 sprintf( file, LOCKDIR"/LK.%03d.%03d.%03d",
377 (int) major( buf.st_dev ),
378 (int) major( buf.st_rdev ),
379 (int) minor( buf.st_rdev )
380 );
381 if ( stat( file, &buf ) != 0 )
382 {
383 /* hmm the file is not there? */
384 syslog( LOG_INFO, "uucp_unlock no such lockfile\n" );
385 return;
386 }
387 if( !check_lock_pid( file, openpid ) )
388 {
389 sprintf( message, "uucp_unlock: unlinking %s\n", file );
390 syslog( LOG_INFO, message );
391 unlink(file);
392 }
393 else
394 {
395 sprintf( message, "uucp_unlock: unlinking failed %s\n", file );
396 syslog( LOG_INFO, message );
397 }
398 }
399
400 /*----------------------------------------------------------
401 check_lock_pid
402
403 accept: the name of the lockfile
404 perform: make sure the lock file is ours.
405 return: 0 on success
406 exceptions: none
407 comments:
408 ----------------------------------------------------------*/
409 int check_lock_pid( const char *file, int openpid )
410 {
411 int fd, lockpid;
412 char pid_buffer[12];
413 char message[80];
414
415 fd=open( file, O_RDONLY );
416 if ( fd < 0 )
417 {
418 return( 1 );
419 }
420 if ( read( fd, pid_buffer, 11 ) < 0 )
421 {
422 close( fd );
423 return( 1 );
424 }
425 close( fd );
426 pid_buffer[11] = '\0';
427 lockpid = atol( pid_buffer );
428 if ( lockpid != openpid )
429 {
430 if( kill( (pid_t) lockpid, 0 ) && errno==ESRCH )
431 {
432 return( 0 );
433 }
434 sprintf(message, "check_lock_pid: lock = %s pid = %i gpid=%i openpid=%i\n",
435 pid_buffer, (int) getpid(), (int) getppid(), openpid );
436 syslog( LOG_INFO, message );
437 return( 1 );
438 }
439 return( 0 );
440 }
441
442 /*----------------------------------------------------------
443 check_group_uucp
444
445 accept: none
446 perform: check if the user is root or in group uucp
447 return: 0 on success
448 exceptions: none
449 comments:
450 This checks if the effective user is in group uucp so we can
451 create lock files. If not we give them a warning and bail.
452 If its root we just skip the test.
453
454 if someone really wants to override this they can use the USER_LOCK_DIRECTORY --not recommended.
455
456 In a recent change RedHat 7.2 decided to use group lock.
457 In order to get around this we just check the group id
458 of the lock directory.
459 ----------------------------------------------------------*/
460 int check_group_uucp()
461 {
462 #ifndef USER_LOCK_DIRECTORY
463 int group_count;
464 struct passwd *user = getpwuid( geteuid() );
465 struct stat buf;
466 char msg[80];
467 gid_t list[ NGROUPS_MAX ];
468
469 if( stat( LOCKDIR, &buf) )
470 {
471 sprintf( msg, "check_group_uucp: Can not find Lock Directory: %s\n", LOCKDIR );
472 syslog( LOG_INFO, msg );
473 return( 1 );
474 }
475 group_count = getgroups( NGROUPS_MAX, list );
476 list[ group_count ] = geteuid();
477
478 if( user->pw_gid )
479 {
480 while( group_count >= 0 && buf.st_gid != list[ group_count ] )
481 {
482 group_count--;
483 }
484 if( buf.st_gid == list[ group_count ] )
485 return 0;
486 sprintf( msg, "%i %i\n", buf.st_gid, list[ group_count ] );
487 syslog( LOG_INFO, msg );
488 syslog( LOG_INFO, UUCP_ERROR );
489 return 1;
490 }
491 return 0;
492 /*
493 if( strcmp( user->pw_name, "root" ) )
494 {
495 while( *g->gr_mem )
496 {
497 if( !strcmp( *g->gr_mem, user->pw_name ) )
498 {
499 break;
500 }
501 (void) *g->gr_mem++;
502 }
503 if( !*g->gr_mem )
504 {
505 syslog( LOG_INFO, UUCP_ERROR );
506 return 1;
507 }
508 }
509 */
510 #endif /* USER_LOCK_DIRECTORY */
511 return 0;
512 }
513
514 /*----------------------------------------------------------
515 The following should be able to follow symbolic links. I think the stat
516 method used below will work on more systems. This was found while looking
517 for information.
518
519 * realpath() doesn't exist on all of the systems my code has to run
520 on (HP-UX 9.x, specifically)
521 ----------------------------------------------------------
522 int different_from_LOCKDIR(const char* ld)
523 {
524 char real_ld[MAXPATHLEN];
525 char real_LOCKDIR[MAXPATHLEN];
526 if (strncmp(ld, LOCKDIR, strlen(ld)) == 0)
527 return 0;
528 if (realpath(ld, real_ld) == NULL)
529 return 1;
530 if (realpath(LOCKDIR, real_LOCKDIR) == NULL)
531 return 1;
532 if (strncmp(real_ld, real_LOCKDIR, strlen(real_ld)) == 0)
533 return 0;
534 else
535 return 1;
536 }
537 */
538
539 /*----------------------------------------------------------
540 is_device_locked
541
542 accept: char * filename. The device in question including the path.
543 perform: see if one of the many possible lock files is aready there
544 if there is a stale lock, remove it.
545 return: 1 if the device is locked or somethings wrong.
546 0 if its possible to create our own lock file.
547 exceptions: none
548 comments: check if the device is already locked
549 ----------------------------------------------------------*/
550 int is_device_locked( const char *port_filename )
551 {
552 const char *lockdirs[] = { "/etc/locks", "/usr/spool/kermit",
553 "/usr/spool/locks", "/usr/spool/uucp", "/usr/spool/uucp/",
554 "/usr/spool/uucp/LCK", "/var/lock", "/var/lock/modem",
555 "/var/spool/lock", "/var/spool/locks", "/var/spool/uucp",
556 LOCKDIR, NULL
557 };
558 const char *lockprefixes[] = { "LCK..", "lk..", "LK.", NULL };
559 char *p, file[80], pid_buffer[20], message[80];
560 int i = 0, j, k, fd , pid;
561 struct stat buf;
562 struct stat buf2;
563
564 j = strlen( port_filename );
565 p = ( char * ) port_filename+j;
566 while( *( p-1 ) != '/' && j-- !=1 ) p--;
567
568 while( lockdirs[i] )
569 {
570 /*
571 Look for lockfiles in all known places other than the
572 defined lock directory for this system
573 report any unexpected lockfiles.
574
575 Is the suspect lockdir there?
576 if it is there is it not the expected lock dir?
577 */
578 if( !stat( lockdirs[i], &buf2 ) &&
579 strncmp( lockdirs[i], LOCKDIR, strlen( lockdirs[i] ) ) )
580 {
581 j = strlen( port_filename );
582 p = ( char * ) port_filename + j;
583 /*
584 SCO Unix use lowercase all the time
585 taj
586 */
587 while( *( p - 1 ) != '/' && j-- != 1 )
588 {
589 #if defined ( __unixware__ )
590 *p = tolower( *p );
591 #endif /* __unixware__ */
592 p--;
593 }
594 k=0;
595 while ( lockprefixes[k] )
596 {
597 /* FHS style */
598 sprintf( file, "%s/%s%s", lockdirs[i],
599 lockprefixes[k], p );
600 if( stat( file, &buf ) == 0 )
601 {
602 sprintf( message, UNEXPECTED_LOCK_FILE,
603 file );
604 syslog( LOG_INFO, message );
605 return 1;
606 }
607
608 /* UUCP style */
609 stat(port_filename , &buf );
610 sprintf( file, "%s/%s%03d.%03d.%03d",
611 lockdirs[i],
612 lockprefixes[k],
613 (int) major( buf.st_dev ),
614 (int) major( buf.st_rdev ),
615 (int) minor( buf.st_rdev )
616 );
617 if( stat( file, &buf ) == 0 )
618 {
619 sprintf( message, UNEXPECTED_LOCK_FILE,
620 file );
621 syslog( LOG_INFO, message );
622 return 1;
623 }
624 k++;
625 }
626 }
627 i++;
628 }
629
630 /*
631 OK. We think there are no unexpect lock files for this device
632 Lets see if there any stale lock files that need to be
633 removed.
634 */
635
636 #ifdef FHS
637 /* FHS standard locks */
638 i = strlen( port_filename );
639 p = ( char * ) port_filename + i;
640 while( *(p-1) != '/' && i-- != 1)
641 {
642 #if defined ( __unixware__ )
643 *p = tolower( *p );
644 #endif /* __unixware__ */
645 p--;
646 }
647 sprintf( file, "%s/%s%s", LOCKDIR, LOCKFILEPREFIX, p );
648 #else
649 /* UUCP standard locks */
650 if ( stat( port_filename, &buf ) != 0 )
651 {
652 syslog( LOG_INFO, "RXTX is_device_locked() could not find device.\n" );
653 sprintf( message, "Filename is : %s \n", port_filename );
654 syslog( LOG_INFO, message );
655 sprintf( message, "Filename is : %s \n", port_filename );
656 syslog( LOG_INFO, message );
657 return 1;
658 }
659 sprintf( file, "%s/LK.%03d.%03d.%03d",
660 LOCKDIR,
661 (int) major( buf.st_dev ),
662 (int) major( buf.st_rdev ),
663 (int) minor( buf.st_rdev )
664 );
665
666 #endif /* FHS */
667
668 if( stat( file, &buf ) == 0 )
669 {
670
671 /* check if its a stale lock */
672 fd=open( file, O_RDONLY );
673 read( fd, pid_buffer, 11 );
674 /* FIXME null terminiate pid_buffer? need to check in Solaris */
675 close( fd );
676 sscanf( pid_buffer, "%d", &pid );
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 1;
692 }
693 }
694 }
695 return 0;
696 }
697 int init( void )
698 {
699 pid_t pid;
700
701 if( ( pid = fork() ) < 0 )
702 {
703 return(-1);
704 }
705 else if ( pid != 0 )
706 {
707 exit( 0 );
708 }
709 setsid();
710 chdir("/");
711 umask( 0 );
712 return( 0 );
713 }
714 int process_requests( int cfd )
715 {
716 for(;;)
717 {
718 char str[80];
719 char str2[80];
720 char *p;
721 int ret;
722
723 ret = read( cfd, str, 80 );
724
725 if( ret < 80 && ret > 1 )
726 str[ret] = '\0';
727 else
728 str[79] = '\0';
729 if ( !strncasecmp( str, "quit", 4 ) )
730 {
731 write( cfd, "Exit\n", strlen( "Exit\n" ) );
732 //sprintf( str, M220 );
733 //write( cfd, str, strlen( str ) );
734 sprintf( str, "221 Thank you for using the Lock File service on %s.\n", hostname );
735 write( cfd, str, strlen( str ) );
736 return( 1 );
737 }
738 else if( !strncasecmp( str, "lock ", 4 ) )
739 {
740 char *q,*r;
741 p = str + 5;
742 q=p;
743 while( *q != ' ' && *q != '\0' )
744 q++;
745 if ( *q == '\0' )
746 {
747 write( cfd, M450, strlen( M450 ) );
748 return(0);
749 }
750 *q = '\0';
751 q++;
752 r=q;
753 while( *r != '\n' && *r != '\0' )
754 r++;
755 if( *r == '\n' )
756 *r = '\0';
757 if ( LOCK( p, atoi( q ) ) )
758 write( cfd, M450, strlen( M450 ) );
759 write( cfd, M200, strlen( M200 ) );
760 }
761 else if( !strncasecmp( str, "unlock ", 6 ) )
762 {
763 char *q,*r;
764 p = str + 7;
765 q=p;
766 while( *q != ' ' && *q != '\0' )
767 q++;
768 if ( *q == '\0' )
769 {
770 write( cfd, "q=0\n", strlen( "q=0\n" ) );
771 write( cfd, M450, strlen( M450 ) );
772 return(0);
773 }
774 *q = '\0';
775 q++;
776 r=q;
777 while( *r != '\n' && *r != '\0' )
778 r++;
779 if( *r == '\n' )
780 *r = '\0';
781 if ( UNLOCK( (const char *) p, atoi( q ) ) )
782 {
783 write( cfd, M450, strlen( M450 ) );
784 }
785 write( cfd, M200, strlen( M200 ) );
786 }
787 else
788 {
789 str[ret-2]='\0';
790 sprintf( str2, M500 );
791 write( cfd, str2, strlen(str2));
792 }
793 //sprintf( str2, "%i\n", strlen(str) );
794 //write( cfd, str2, strlen(str2) );
795 //sprintf( str2, "%i\n", ret );
796 //write( cfd, str2, strlen(str2) );
797 //write( cfd, str, strlen(str) );
798 }
799 }
800 int main( int argc, char **argv )
801 {
802 char *ptr;
803 char str[128];
804 char portstr[7];
805 int fd, cfd;
806 struct sockaddr *cliaddr;
807 socklen_t len, addrlen;
808 struct sockaddr_in *sin;
809 struct addrinfo a_info, *results, *backup;
810 const int on = -1;
811
812 /* become a daemon */
813 if( argc != 3 )
814 {
815 fprintf( stderr, "usage: %s host port\n", argv[0] );
816 exit(1);
817 }
818 init();
819 gethostname( hostname, 255 );
820 hostname[256]='\0';
821 openlog( "rxtx", LOG_PID, LOG_UUCP );
822 syslog( LOG_INFO, "Lock Daemon Initialized" );
823
824 bzero( &a_info, sizeof( struct addrinfo ) );
825 a_info.ai_flags = AI_PASSIVE;
826 a_info.ai_family = AF_UNSPEC;
827 a_info.ai_socktype = SOCK_STREAM;
828
829 if( getaddrinfo( argv[1], argv[2], &a_info, &results ))
830 {
831 syslog( LOG_INFO, "Lock Daemon failed" );
832 goto exit;
833 }
834 backup = results;
835 do
836 {
837 fd = socket( results->ai_family, results->ai_socktype,
838 results->ai_protocol );
839 if ( fd < 0 ) continue;
840 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
841 if ( bind( fd, results->ai_addr, results->ai_addrlen ) == 0 ) break;
842 if ( results == NULL )
843 {
844 printf( "results is null\n" );
845 goto exit;
846 }
847 close( fd );
848
849 } while ( ( results = results->ai_next) != NULL );
850 if( fd < 0 )
851 {
852 }
853
854 if ( ( ptr = getenv( "LISTENQ" ) ) != NULL )
855 addrlen = atoi(ptr);
856 if( listen( fd, 1024 ) < 0 )
857 {
858 perror( strerror(errno));
859 printf( "fd is %i is another daemon running?\n", fd );
860 goto exit;
861 }
862
863 if ( &len )
864 len = results->ai_addrlen;
865 freeaddrinfo( backup );
866 cliaddr= malloc( addrlen );
867 len = addrlen;
868 cfd = accept( fd, cliaddr, &len );
869 sin = ( struct sockaddr_in * ) cliaddr;
870 if ( !inet_ntop( AF_INET, &(sin->sin_addr), str, sizeof(str) ) )
871 str[0] = '\0';
872 if ( !ntohs( sin->sin_port ) )
873 {
874 snprintf( portstr, sizeof( portstr ), ".%d", ntohs( sin->sin_port) );
875 strcat( str, portstr);
876 }
877 fprintf( stderr, "connected from %s\n", str );
878 sprintf( str, M220 );
879 write( cfd, str, strlen( str ) );
880 for(;;)
881 {
882 if (process_requests( cfd ))
883 {
884 close( cfd );
885 goto exit;
886 }
887 }
888 syslog( LOG_INFO, "Lock Daemon Shutting down" );
889 exit:
890 closelog();
891 exit(0);
892 }