?? servaccept.c
字號(hào):
/*** $Id: servaccept.c,v 1.5 2003/09/04 06:02:53 weiym Exp $** ** servaccept.c: accept connection from clients.** ** Copyright (C) 2003 Feynman Software** Copyright (C) 2000 ~ 2002 Wei Yongming.**** Current maintainer: Wei Yongming.**** Create date: 2000/12/19**** NOTE: The idea comes from sample code in APUE.*//*** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//*** TODO:*/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/stat.h>#include <sys/un.h>#include <stddef.h>#include <time.h>#include <unistd.h>#include "ourhdr.h"#define STALE 30 /* client's name can't be older than this (sec) *//* Wait for a client connection to arrive, and accept it. * We also obtain the client's pid from the pathname * that it must bind before calling us. *//* returns new fd if all OK, < 0 on error */int serv_accept (int listenfd, pid_t *pidptr, uid_t *uidptr){ int clifd, len; time_t staletime; struct sockaddr_un unix_addr; struct stat statbuf; const char* pid_str; len = sizeof (unix_addr); if ( (clifd = accept (listenfd, (struct sockaddr *) &unix_addr, &len)) < 0) return (-1); /* often errno=EINTR, if signal caught */ /* obtain the client's uid from its calling address */ len -= /* th sizeof(unix_addr.sun_len) - */ sizeof(unix_addr.sun_family); /* len of pathname */ unix_addr.sun_path[len] = 0; /* null terminate */ if (stat(unix_addr.sun_path, &statbuf) < 0) return(-2);#ifdef S_ISSOCK /* not defined for SVR4 */ if (S_ISSOCK(statbuf.st_mode) == 0) return(-3); /* not a socket */#endif if ((statbuf.st_mode & (S_IRWXG | S_IRWXO)) || (statbuf.st_mode & S_IRWXU) != S_IRWXU) return(-4); /* is not rwx------ */ staletime = time(NULL) - STALE; if (statbuf.st_atime < staletime || statbuf.st_ctime < staletime || statbuf.st_mtime < staletime) return(-5); /* i-node is too old */ if (uidptr != NULL) *uidptr = statbuf.st_uid; /* return uid of caller */ /* get pid of client from sun_path */ pid_str = strrchr (unix_addr.sun_path, '/'); pid_str++; *pidptr = atoi (pid_str); unlink (unix_addr.sun_path); /* we're done with pathname now */ return (clifd);}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -