zoukankan      html  css  js  c++  java
  • UNIX系统文件

    密码文件

    密码文件又称用户数据库,一般为/etc/passwd,对应的结构为struct passwd,该文件内容大体如下:

    描述 passwd字段
    用户名 char* pw_name
    加密密码 char* pw_passwd
    UID uid_t  pw_uid
    GID gid_t  pw_gid
    注释 char* pw_gecos
    初始工作目录 char* pw_dir
    shell char* pw_shell

    加密密码一般一个字符,如果没有值说明该用户没有密码,这个字符只是表象,不是真正的加密密码,加密密码在/etc/shadow中

    shell值为shell解释器程序的绝对路径,shell为/bin/false /bin/true /dev/null 都是禁止登录的

    用户名为nobody的用户,系统允许向系统写日志,只能访问那些能被其他访问的文件

    通过下面函数可以获取对应的struct passwd结构:

    #include <pwd.h>
    struct passwd *getpwuid(uid_t uid);
    struct passwd *getpwnam(const char *name);
    //Both return: pointer if OK, NULL on error

    这两个函数返回值都是静态存储的,所以在此调用内容会被覆盖,在多线程环境下应该使用可重入的替代函数:

    int getpwnam_r(const char *name, struct passwd *pwd,char *buf, size_t buflen, struct passwd **result);
    
     int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);

    成功返回0,*result值pwd,失败返回0,*result 为NULL

    如果想遍历所有结构,使用下面的函数:

    #include <pwd.h>
    struct passwd *getpwent(void);//返回一条密码数据库记录
    //Returns: pointer if OK, NULL on error or end of file
    void setpwent(void);//返回到密码数据的开始
    void endpwent(void);//关闭密码数据库

    下面遍历数据库,查找对应的结构:

    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <pwd.h>
    
    int serchsth(const char* name)
    {
        struct passwd *ptr;
        int ret = -1;
        setpwent();
    
        while((ptr = getpwent())!= NULL )
        {
    
            if(strcmp(name,ptr->pw_name) == 0 )
            {
                printf("we got something:%s\n",ptr->pw_name);
                ret = 0;
                break;
            }
        }
        endpwent();
        return ret;
    }
    
    int main(int argc,char* argv[])
    {
        return serchsth("hero");
    }

    加密密码存在/etc/shadow中,加密算法是单向加密,所以很通过加密密码难猜测出原始密码,对应的有函数获取对应的加密记录以及遍历所有记录:

    #include <shadow.h>
    struct spwd *getspnam(const char *name);
    struct spwd *getspent(void);
    //Both return: pointer if OK, NULL on error
    //下面是上面两个可重入对应函数
     int getspent_r(struct spwd *spbuf, char *buf, size_t buflen, struct spwd **spbufp);
     int getspnam_r(const char *name, struct spwd *spbuf,char *buf, size_t buflen, struct spwd **spbufp);
    
    void setspent(void);
    void endspent(void);

    同样地有相似获取组信息的函数

    #include <grp.h>
    struct group *getgrent(void);
    //Returns: pointer if OK, NULL on error or end of file
    void setgrent(void);
    void endgrent(void);

    一个用户拥有一个组之外还可以属于多个组,这些组称为附加组,通过下面函数可以获取和设置附加组:

    #include <sys/types.h>
     #include <unistd.h>
    
     int getgroups(int size, gid_t list[]);
    
     #include <grp.h>
    
     int setgroups(size_t size, const gid_t *list);
    
    #include <sys/types.h>
    #include <grp.h>
    
     int initgroups(const char *user, gid_t group);
    
    //return 0 sucess -1 failed

    getgroups 获取size指定个数的附加组,值存在list中,当size为0的时候,返回附加组个数。这可以用来确定要开辟多大的数组。

    setgroups设置附加组,initgroups获取用户对应的组信息,只有特权才能操作这两个函数。

    系统信息

    获取当前主机和操作系统信息:

    #include <sys/utsname.h>
    
           int uname(struct utsname *buf);
          // On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

    获取主机名:

    #include <unistd.h>
    int gethostname(char *name, int namelen);
    //Returns: 0 if OK, −1 on error

    下面的程序获取系统信息和主机信息:

    #include <stdio.h>
    #include <sys/utsname.h>
    #include <unistd.h>
    
    int main(int argc,char* argv[])
    {
        struct utsname buf;
        char name[128];
    
        if(uname(&buf))
        {
            printf("get sys info faild:\n");
            return -1;
        }
    
        if(gethostname(name,128))
        {
            printf("get hostname failed\n");
            return -1;
        }
    
        printf("system name:%s,node name:%s,release:%s,version:%s,hardware identifier:%s\n",\
                buf.sysname,buf.nodename,buf.release,buf.version,buf.machine);
    
        printf("hostname:%s\n",name);
    
        return 0;
    }

    时间相关

    UNIX使用UTC时间而不是本地时间,自动转换夏令时,时间和日期在一起表示,获取日历时间:

    #include <time.h>
    time_t time(time_t *calptr);
    Returns: value of time if OK, −1 on error

    更高精度的时间处理:

    #include <time.h>
    
           int clock_getres(clockid_t clk_id, struct timespec *res);
    
           int clock_gettime(clockid_t clk_id, struct timespec *tp);
    
           int clock_settime(clockid_t clk_id, const struct timespec *tp);
    
          // Link with -lrt (only for glibc versions before 2.17).
    // clock_gettime(),  clock_settime()  and  clock_getres() return 0 for success, or -1 for failure

    clk_id取值如下:

    CLOCK_REALTIME   //real system time

    CLOCK_MONOTIME //real system time without negative jumps

    CLOCK_PROCESS_CPUTIME_ID //CPU time for calling process

    CLOCK_THREAD_CPUTIME_ID  //CPU time for calling thread

    clock_gerres获取时间精度,clock_gettime获取更精确的时间,clock_settime设置时间,需要特权。

    不鼓励用但是大面积用的函数:

    #include <sys/time.h>
    
           int gettimeofday(struct timeval *tv, struct timezone *tz); //tz 一般取NULL不用管
    
           int settimeofday(const struct timeval *tv, const struct timezone *tz);//tz或者tv为NULL函数不会起作用
    //0 sucess,-1 error

    时间本地化和UTC使用的结构:

    struct tm { /* a broken-down time */
    int  tm_sec; /* seconds after the minute: [0 - 60] */
    int  tm_min; /* minutes after the hour: [0 - 59] */
    int  tm_hour;  /* hours after midnight: [0 - 23] */
    int  tm_mday;  /* day of the month: [1 - 31] */
    int  tm_mon; /* months since January: [0 - 11] */
    int  tm_year;  /* years since 1900 */
    int  tm_wday;  /* days since Sunday: [0 - 6] */
    int  tm_yday;  /* days since January 1: [0 - 365] */
    int  tm_isdst; /* daylight saving time flag: <0, 0, >0 */
    };

    日历时间转为本地或UTC时间对应的函数:

    #include <time.h>
    struct tm *gmtime(const time_t *calptr);
    struct tm *localtime(const time_t *calptr);
    //Both return: pointer to broken-down time, NULL on error

    本地转为日历时间:

    #include <time.h>
    time_t mktime(struct tm *tmptr);
    //Returns: calendar time if OK, −1 on error

    时间格式化:

    #include <time.h>
    size_t strftime(char *restrict buf, size_t maxsize,const char *restrict format,const struct tm *restrict tmptr);
    size_t strftime_l(char *restrict buf, size_t maxsize,const char *restrict format,const struct tm *restrict tmptr, locale_t locale);
    //Both return: number of characters stored in array if room, 0 otherwise

    字符串转化为时间结构:

    #include <time.h>
    char *strptime(const char *restrict buf, const char *restrict format,struct tm *restrict tmptr);
    //Returns: pointer to one character past last character parsed, NULL otherwise

    看一个综合的小程序:

    #include <stdio.h>
    #include <time.h>
    #include <sys/time.h>
    
    int main(int argc,char* argv[])
    {
        time_t curtime;
        struct tm* st_tm;
        struct timespec res;
        char buf[128];
    
        curtime = time(NULL);
    
        printf("big value time:%lld\n",(long long)curtime);
        
        if(curtime == -1)
        {
            printf("get curtime failed\n");
            return -1;
        }
    
        if(clock_gettime(CLOCK_REALTIME,&res))
        {
            printf("get high resolution time failed\n");
            return -1;
        }
    
        if((st_tm = localtime(&curtime))==NULL)
        {
            printf("gettimeofday failed\n");
            return -1;
        }
    
        if(strftime(buf,128,"%F %T",st_tm)== 0)
        {
            printf("format time failed\n");
            return -1;
        }
    
        printf("now:%s\n",buf);
    
        
        if(!strptime(buf,"%F %T",st_tm)) 
        {
            printf("convert time back failed\n");
            return -1;
        }
    
        curtime = mktime(st_tm);
    
        printf("backed time:%lld\n",(long long )curtime);
    
        return 0;
    
    
    
        
    }
  • 相关阅读:
    Python基础-迭代器
    Python基础-生成器
    Python基础-装饰器
    Python基础-函数
    Python基础-文件操作
    Python基础-集合
    jfinal任务调度quartz(cron) 定时任务 QuartzPlugin
    ServletRequest.getRequestDispatcher
    QuartZ Cron表达式
    Jax-WS WebService实现
  • 原文地址:https://www.cnblogs.com/xiaofeifei/p/4082262.html
Copyright © 2011-2022 走看看