免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2289 | 回复: 0
打印 上一主题 下一主题

[C] mlock权限,求高手指点 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-31 15:07 |只看该作者 |倒序浏览
运行结果  Testing with user 'bin' (uid: 1)
              You have the right to call mlock
普通用户不是不能使用吗?求高手



#define _XOPEN_SOURCE 600

#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <pwd.h>
#include <string.h>
#include "posixtest.h"

#define BUFSIZE 8

/** Set the euid of this process to a non-root uid */
int set_nonroot()
{
        struct passwd *pw;
        setpwent();
        /* search for the first user which is non root */
        while((pw = getpwent()) != NULL)
                if(strcmp(pw->pw_name, "root"))
                        break;
        endpwent();
        if(pw == NULL) {
                printf("There is no other user than current and root.\n");
                return 1;
        }

        if(seteuid(pw->pw_uid) != 0) {
                if(errno == EPERM) {
                        printf("You don't have permission to change your UID.\n");
                        return 1;
                }
                perror("An error occurs when calling seteuid()");
                return 1;
        }

        printf("Testing with user '%s' (uid: %d)\n",
               pw->pw_name, (int)geteuid());
        return 0;
}
int main() {
        int result;
        void *ptr;

        set_nonroot();
        /* This test should be run under standard user permissions */
        if (getuid() == 0) {
                if (set_nonroot() != 0) {
                        printf("Cannot run this test as non-root user\n");
                        return PTS_UNTESTED;
                }
        }

        ptr = malloc(BUFSIZE);
        if(ptr == NULL) {
                printf("Can not allocate memory.\n");
                return PTS_UNRESOLVED;
        }

        result = mlock(ptr, BUFSIZE);

        if(result == -1 && errno == EPERM) {
                printf("Test PASSED\n");
                return PTS_PASS;
        } else if(result == 0) {
                printf("You have the right to call mlock\n");
                return PTS_FAIL;
        } else {
                perror("Unexpected error");
                return PTS_UNRESOLVED;
        }

}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP