Solved env var problems, such as PATH
Hi all,
I have seen a lot of people asking how to auto start X on boot time without gdm or xdm, and so far the solutions require either editing /etc/inittab or your personal bash_profile. I feel quite uncomfortable changing the behaviour of these files ('cuase the user may not want to start X everytime he/she login!), so I wrote a short code with a rc script that do the job without changing extra files.
hope this helps
I am not an experienced *unix programmer. please correct any of my mistakes.
Features
1. Simple. Run as a regular service through rc-script, which can be start/stop/add/remove easily.
2. Friendly. Does not interfere original system layout nor need to edit any sensitive system files
3. Flexible. System admin can edit the shell script to do more setups, such as logger, before autologin
Installing instruction
There are 3 files need to be installed:
1. autostartx-bin.c ----- C file
2. autostartx-sh ----- binary forntend shell script
3. autostartx-rc ----- rc script
Do the following as root to install the files:
1. compile autostartx-bin.c. Copy the compiled program to /sbin and change file permission
Code: Select all
#gcc -o autostartx-bin autostartx-bin.c
#cp autostartx-bin /sbin
#chmod 755 /sbin/autostartx-bin(note that the filename is 'autostartx' under /sbin instead of 'autostartx-sh')
Code: Select all
#cp autostartx-sh /sbin/autostartx
#chmod 755 /sbin/autostartx(note that the filename is 'autostartx' under /etc/init.d instead of 'autostartx-rc')
Code: Select all
#cp autostartx-rc /etc/init.d/autostartx
#chmod 700 /etc/init.d/autostartxCode: Select all
edit /etc/init.d/autostartxCode: Select all
#rc-update del xdm
#rc-update add autostartx defaultCode: Select all
#rebootautostartx-bin.c
Code: Select all
//autostartx-bin.c
//by moowei
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>
#include <string.h>
#include <stdio.h>
#include <grp.h>
int main(int argc, char *argv[])
{
struct passwd *userinfo;
if(argc < 2){
printf("No specified username. Exit.\n");
exit(1);
}
userinfo = getpwnam(argv[1]);
if (userinfo == NULL){
printf("User does not exist. Exit.\n");
exit(1);
}
if ( initgroups(userinfo->pw_name, userinfo->pw_gid) == -1){
printf("Cannot initgroups. Exit.\n");
exit(1);
}
if ( setgid(userinfo->pw_gid) == -1){
printf("Cannot setgid. Exit.\n");
exit(1);
}
setenv("USER", userinfo->pw_name, 1);
setenv("USERNAME", userinfo->pw_name, 1);
setenv("LOGNAME", userinfo->pw_name, 1);
setenv("HOME", userinfo->pw_dir, 1);
setenv("SHELL", userinfo->pw_shell, 1);
if ( setuid(userinfo->pw_uid) == -1){
printf("Cannot setuid. Exit.\n");
exit(1);
}
chdir(userinfo->pw_dir);
execlp("startx", "startx", 0);
return 0;
}Code: Select all
#!/bin/sh
# this script sets system env vars
test -f /etc/profile && source /etc/profile
exec autostartx-bin "$@"Code: Select all
#!/sbin/runscript
depend() {
need xfs
}
start() {
ebegin "Starting autostartx"
start-stop-daemon --start --background --quiet --pidfile /var/run/autostartx.pid --make-pidfile --exec /sbin/autostartx -- @USER
eend $?
}
stop() {
ebegin "Stopping autostartx"
start-stop-daemon --stop --pidfile /var/run/autostartx.pid
eend $?
}




