Debian Codegraph @

src/lfd/lockdaemon.c.noinetd

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