|
[PHP]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define S_IRW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
#define STD_OUT "/tmp/.X11-unix/autox.log"
#define STD_ERR "/tmp/.X11-unix/autox.err"
int main(int argc, char **argv)
{
if (fork()==0) {
struct passwd *pw;
int fd0, fd1, fd2;
uid_t uid;
gid_t gid;
char *dir, *shell, **args = NULL;
char *user = "sxzzsf"; /*my login name :-)*/
char *cmd = "/usr/bin/startx";
if (argc>1)
user = argv[1];
if (argc>2)
cmd = argv[2];
if (argc>3)
args = argv + 3;
pw = getpwnam(user);
if (pw) {
uid=pw->pw_uid;
gid=pw->pw_gid;
dir=strdup(pw->pw_dir);
shell=strdup(pw->pw_shell);
}
else {
printf("ERROR: No such user %s or in-sufficient privilege!\n", user);
return 1;
}
if (chdir(dir) == -1) {
printf("ERROR: Failed to chdir to %s\n", dir);
return 1;
}
if (setgid(gid) == -1) {
printf("ERROR: Failed to setgid to %d\n", gid);
return 1;
}
if (setregid(gid, gid) == -1) {
printf("ERROR: Failed to setregid to %d\n", gid, gid);
return 1;
}
if (initgroups(user, gid) == -1) {
printf("WARNING: Failed to initgroups for %s\n", user);
}
if (setuid(uid) == -1) {
printf("ERROR: Failed to setuid to %d\n", uid);
return 1;
}
if (setreuid(uid, uid) == -1) {
printf("ERROR: Failed to setreuid to %d %d\n", uid, uid);
return 1;
}
chown("/dev/console", uid, gid);
chown("/dev/tty", uid, gid);
setenv("LOAD_BY_AUTOX", "1", 1);
setenv("HOME", dir, 1);
setenv("SHELL", shell, 1);
setenv("USER", user, 1);
setenv("LOGNAME", user, 1);
umask(0);
fd0 = open("/dev/null", O_RDWR);
if (fd0 == -1) {
printf("ERROR: Failed to open file /dev/null for stdin\n");
return 1;
}
fd1 = open(STD_OUT, O_CREAT | O_TRUNC | O_RDWR | O_APPEND, S_IRW);
if (fd1 == -1) {
printf("ERROR: Failed to open file %s for stdout\n", STD_OUT);
return 1;
}
fd2 = open(STD_ERR, O_CREAT | O_TRUNC | O_RDWR | O_APPEND, S_IRW);
if (fd2 == -1) {
printf("ERROR: Failed to open file %s for stderr\n", STD_ERR);
return 1;
}
if (dup2(fd0, 0) == -1) {
printf("ERROR: Failed to dup stdin to /dev/null\n");
return 1;
}
if (dup2(fd2, 2) == -1) {
printf("ERROR: Failed to dup stderr to %s\n", STD_ERR);
return 1;
}
if (dup2(fd1, 1) == -1) {
printf("ERROR: Failed to dup stdout to %s\n", STD_OUT);
return 1;
}
printf("LOGGING: started ...............................................................\n");
printf("LOGGING: %s execute %s\n", user, cmd);
if (args) {
int i = 0;
printf("LOGGING: cmd options: ");
while (args)
printf(" %s", args[i++]);
printf("\n");
}
printf("LOGGING: continued .............................................................\n");
execvp(cmd, args);
}
return 0;
}
[/PHP]
[PHP]
# gunzip autox.c.gz
# gcc autox.c -o autox
# cp autox /usr/bin/
# echo /usr/bin/autox your_login_name >> /etc/rc.d/rc.local[/PHP]
把 your_login_name 替换为用于自动登录 X window 的用户名,默认是我自己的 :-)
在 ~/.xinitrc 的开头部分添加下面三行
[PHP]
if [ "$LOAD_BY_AUTOX" != "" ]; then
. ~/.bash_profile
fi[/PHP] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|