/* write the current process id to the specified file */ static void create_pidfile(const char *filename) { int fd; char buffer[20]; if (filename != NULL) { mkdirname(filename); if ((fd = open(filename, O_RDWR | O_CREAT, 0644)) < 0) { log_log(LOG_ERR, "cannot create pid file (%s): %s", filename, strerror(errno)); exit(EXIT_FAILURE); } if (lockf(fd, F_TLOCK, 0) < 0) { log_log(LOG_ERR, "cannot lock pid file (%s): %s", filename, strerror(errno)); exit(EXIT_FAILURE); } if (ftruncate(fd, 0) < 0) { log_log(LOG_ERR, "cannot truncate pid file (%s): %s", filename, strerror(errno)); exit(EXIT_FAILURE); } mysnprintf(buffer, sizeof(buffer), "%d\n", (int)getpid()); if (write(fd, buffer, strlen(buffer)) != (int)strlen(buffer)) { log_log(LOG_ERR, "error writing pid file (%s): %s", filename, strerror(errno)); exit(EXIT_FAILURE); } /* we keep the pidfile open so the lock remains valid */ } }
/* returns a socket ready to answer requests from the client, exit()s on error */ static int create_socket(const char *filename) { int sock; int i; struct sockaddr_un addr; /* create a socket */ if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { log_log(LOG_ERR, "cannot create socket: %s", strerror(errno)); exit(EXIT_FAILURE); } if (sock >= FD_SETSIZE) { log_log(LOG_ERR, "socket file descriptor number too high (%d)", sock); exit(EXIT_FAILURE); } /* remove existing named socket */ if (unlink(filename) < 0) { log_log(LOG_DEBUG, "unlink() of %s failed (ignored): %s", filename, strerror(errno)); } /* do not block on accept() */ if ((i = fcntl(sock, F_GETFL, 0)) < 0) { log_log(LOG_ERR, "fctnl(F_GETFL) failed: %s", strerror(errno)); if (close(sock)) log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno)); exit(EXIT_FAILURE); } if (fcntl(sock, F_SETFL, i | O_NONBLOCK) < 0) { log_log(LOG_ERR, "fctnl(F_SETFL,O_NONBLOCK) failed: %s", strerror(errno)); if (close(sock)) log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno)); exit(EXIT_FAILURE); } /* create the directory if needed */ mkdirname(filename); /* create socket address structure */ memset(&addr, 0, sizeof(struct sockaddr_un)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, filename, sizeof(addr.sun_path)); addr.sun_path[sizeof(addr.sun_path) - 1] = '\0'; /* bind to the named socket */ if (bind(sock, (struct sockaddr *)&addr, SUN_LEN(&addr))) { log_log(LOG_ERR, "bind() to %s failed: %s", filename, strerror(errno)); if (close(sock)) log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno)); exit(EXIT_FAILURE); } /* close the file descriptor on exec */ if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0) { log_log(LOG_ERR, "fctnl(F_SETFL,FD_CLOEXEC) on %s failed: %s", filename, strerror(errno)); if (close(sock)) log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno)); exit(EXIT_FAILURE); } /* set permissions of socket so anybody can do requests */ /* Note: we use chmod() here instead of fchmod() because fchmod does not work on sockets http://www.opengroup.org/onlinepubs/009695399/functions/fchmod.html http://lkml.org/lkml/2005/5/16/11 */ if (chmod(filename, (mode_t)0666)) { log_log(LOG_ERR, "chmod(0666) of %s failed: %s", filename, strerror(errno)); if (close(sock)) log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno)); exit(EXIT_FAILURE); } /* start listening for connections */ if (listen(sock, SOMAXCONN) < 0) { log_log(LOG_ERR, "listen() failed: %s", strerror(errno)); if (close(sock)) log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno)); exit(EXIT_FAILURE); } /* we're done */ return sock; }
// edit or display a file with vertical & horizontal scrolling & text searching int _near List_Cmd( LPTSTR pszCmdLine ) { int nFVal, nReturn = 0, argc; TCHAR szSource[MAXFILENAME+1], szFileName[MAXFILENAME+1], *pszArg; FILESEARCH dir; memset( &dir, '\0', sizeof(FILESEARCH) ); // check for and remove switches if ( GetRange( pszCmdLine, &(dir.aRanges), 0 ) != 0 ) return ERROR_EXIT; // check for /T"search string" GetMultiCharSwitch( pszCmdLine, _TEXT("T"), szSource, 255 ); if ( szSource[0] == _TEXT('"') ) sscanf( szSource+1, _TEXT("%79[^\"]"), szListFindWhat ); else if ( szSource[0] ) sprintf( szListFindWhat, FMT_PREC_STR, 79, szSource ); if ( GetSwitches( pszCmdLine, _TEXT("*HIRSWX"), &lListFlags, 0 ) != 0 ) return ( Usage( LIST_USAGE )); if ( szSource[0] ) lListFlags |= LIST_SEARCH; // check for pipe to LIST w/o explicit /S switch if ( first_arg( pszCmdLine ) == NULL ) { if ( _isatty( STDIN ) == 0 ) lListFlags |= LIST_STDIN; else if (( lListFlags & LIST_STDIN ) == 0 ) return ( Usage( LIST_USAGE )); } // initialize buffers & globals if ( ListInit() ) return ERROR_EXIT; nCurrent = nStart = 0; // ^C handling if ( setjmp( cv.env ) == -1 ) { list_abort: FindClose( dir.hdir ); Cls_Cmd( NULL ); nReturn = CTRLC; goto list_bye; } RestartFileSearch: for ( argc = 0; ; argc++ ) { // break if at end of arg list, & not listing STDIN if (( pszArg = ntharg( pszCmdLine, argc )) == NULL ) { if (( lListFlags & LIST_STDIN ) == 0 ) break; } else strcpy( szSource, pszArg ); for ( nFVal = FIND_FIRST; ; ) { szClip[0] = _TEXT('\0'); // if not reading from STDIN, get the next matching file if (( lListFlags & LIST_STDIN ) == 0 ) { // qualify filename if ( nFVal == FIND_FIRST ) { mkfname( szSource, 0 ); if ( is_dir( szSource )) mkdirname( szSource, WILD_FILE ); } if ( stricmp( szSource, CLIP ) == 0 ) { RedirToClip( szClip, 99 ); if ( CopyFromClipboard( szClip ) != 0 ) break; strcpy( szFileName, szClip ); } else if ( QueryIsPipeName( szSource )) { // only look for pipe once if ( nFVal == FIND_NEXT ) break; copy_filename( szFileName, szSource ); } else if ( find_file( nFVal, szSource, ( FIND_BYATTS | FIND_RANGE | FIND_EXCLUDE | 0x07), &dir, szFileName ) == NULL ) { nReturn = (( nFVal == FIND_FIRST ) ? ERROR_EXIT : 0 ); break; } else if ( nStart < nCurrent ) { nStart++; nFVal = FIND_NEXT; continue; } else if ( dir.ulSize > 0L ) LFile.lSize = dir.ulSize; } // clear the screen (scrolling the buffer first to save the current screen) Cls_Cmd( NULL ); if (( nReturn = _list( szFileName )) == CTRLC ) goto list_abort; if ( szClip[0] ) remove( szClip ); if ( nReturn != 0 ) break; SetCurPos( nScreenRows, 0 ); if (( szClip[0] ) || ( lListFlags & LIST_STDIN )) break; if ( LFile.hHandle > 0 ) _close( LFile.hHandle ); LFile.hHandle = -1; // increment index to current file if ( nCurrent < nStart ) { FindClose( dir.hdir ); nStart = 0; goto RestartFileSearch; } else { nFVal = FIND_NEXT; nCurrent++; nStart++; } } // we can only read STDIN once! lListFlags &= ~LIST_STDIN; } crlf(); list_bye: FreeMem( LFile.lpBufferStart ); if ( LFile.hHandle > 0 ) _close( LFile.hHandle ); LFile.hHandle = -1; return nReturn; }