⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servaccept.c

📁 unix环境高级编程第二版配套源代码 unix环境高级编程第二版配套源代码
💻 C
字号:
#include "apue.h"#include <sys/socket.h>#include <sys/un.h>#include <time.h>#include <errno.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 user ID from the pathname * that it must bind before calling us. * Returns new fd if all OK, <0 on error */intserv_accept(int listenfd, uid_t *uidptr){	int					clifd, len, err, rval;	time_t				staletime;	struct sockaddr_un	un;	struct stat			statbuf;	len = sizeof(un);	if ((clifd = accept(listenfd, (struct sockaddr *)&un, &len)) < 0)		return(-1);		/* often errno=EINTR, if signal caught */	/* obtain the client's uid from its calling address */	len -= offsetof(struct sockaddr_un, sun_path); /* len of pathname */	un.sun_path[len] = 0;			/* null terminate */	if (stat(un.sun_path, &statbuf) < 0) {		rval = -2;		goto errout;	}#ifdef	S_ISSOCK	/* not defined for SVR4 */	if (S_ISSOCK(statbuf.st_mode) == 0) {		rval = -3;		/* not a socket */		goto errout;	}#endif	if ((statbuf.st_mode & (S_IRWXG | S_IRWXO)) ||		(statbuf.st_mode & S_IRWXU) != S_IRWXU) {		  rval = -4;	/* is not rwx------ */		  goto errout;	}	staletime = time(NULL) - STALE;	if (statbuf.st_atime < staletime ||		statbuf.st_ctime < staletime ||		statbuf.st_mtime < staletime) {		  rval = -5;	/* i-node is too old */		  goto errout;	}	if (uidptr != NULL)		*uidptr = statbuf.st_uid;	/* return uid of caller */	unlink(un.sun_path);		/* we're done with pathname now */	return(clifd);errout:	err = errno;	close(clifd);	errno = err;	return(rval);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -