Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Desktop Environments
  • Search

code to auto login without *dm

Problems with GUI applications? Questions about X, KDE, Gnome, Fluxbox, etc.? Come on in. NOTE: For multimedia, go up one forum
Post Reply
Advanced search
41 posts
  • 1
  • 2
  • Next
Author
Message
moowei
n00b
n00b
Posts: 20
Joined: Thu Apr 22, 2004 6:02 pm

code to auto login without *dm

  • Quote

Post by moowei » Sun Dec 19, 2004 6:34 am

code has been updated on Feb 13, 2005
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
2. copy the forntend shell script to /sbin
(note that the filename is 'autostartx' under /sbin instead of 'autostartx-sh')

Code: Select all

#cp autostartx-sh /sbin/autostartx
#chmod 755 /sbin/autostartx
3. copy the rc script to /etc/init.d. change file permission
(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/autostartx
4. edit rc script file. replace @USER with the user you want to login with

Code: Select all

edit /etc/init.d/autostartx
5. remove xdm from default runlevel, and add autostartx to default runlevel

Code: Select all

#rc-update del xdm
#rc-update add autostartx default
7. reboot and try!

Code: Select all

#reboot

autostartx-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;
}
autostartx (frontend shell script)

Code: Select all

#!/bin/sh
# this script sets system env vars

test -f /etc/profile && source /etc/profile
exec autostartx-bin "$@"
autostartx-rc (rc script)

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 $?
}
Last edited by moowei on Sun Feb 13, 2005 4:22 pm, edited 1 time in total.
Top
kgraehl
n00b
n00b
User avatar
Posts: 54
Joined: Sat Sep 13, 2003 11:10 am
Contact:
Contact kgraehl
Website

  • Quote

Post by kgraehl » Thu Dec 23, 2004 6:14 am

I really like this script. It small and efficient. I don't understand what it does, though... I need to get the environment variable CHOICESPATH set before X starts and placing it in /etc/env.d or /etc/rc.conf doesn't work.

How can I get this working with some set environment?
Top
kgraehl
n00b
n00b
User avatar
Posts: 54
Joined: Sat Sep 13, 2003 11:10 am
Contact:
Contact kgraehl
Website

  • Quote

Post by kgraehl » Thu Dec 23, 2004 6:23 am

Well, when I edited the c code and recompiled I can get my environment variable in there in time. I guess that's the only way to do it? What init script goes through and gets everything in /etc/env.d anyway? Or is it precalculated somewhere but doesn't get applied until someone logs into tty1-6?
Top
kgraehl
n00b
n00b
User avatar
Posts: 54
Joined: Sat Sep 13, 2003 11:10 am
Contact:
Contact kgraehl
Website

  • Quote

Post by kgraehl » Thu Dec 23, 2004 6:44 am

Dang, there's a big problem. I can't use my sound card

Code: Select all

$ alsamixer 

alsamixer: function snd_ctl_open failed for default: Permission denied
Adding default to the audio group in /etc/groups didn't fix this.
Top
moowei
n00b
n00b
Posts: 20
Joined: Thu Apr 22, 2004 6:02 pm

  • Quote

Post by moowei » Thu Dec 23, 2004 6:54 am

Sorry, can't help you on this one. I am not too sure about who sets the environment variables during the login session. I am guessing it's the shell that does this job. I do know the login program sets up the basics, like HOME, PATH, LOGNAME, and SHELL, Where do you used to set CHOICESPATH? in .bash_rc?

I revised my code, and it turned out to be shorter and better. Some group permission problems were fixed. However, I still have some trouble with Alsa... but there's a workaround to that..

again, I am not an experienced *unix programmer. Please correct any errors.

the installation is the same.

autostartx.c

Code: Select all

#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);
	putenv("PATH=/bin:/usr/bin:/usr/X11R6/bin");

	if ( setuid(userinfo->pw_uid) == -1){
		printf("Cannot setuid. Exit.\n");
		exit(1);
	}

	chdir(userinfo->pw_dir);

	execlp("startx", "startx", 0);
	return 0;
}
autostartx (rc script)

Code: Select all

#!/sbin/runscript

depend() {
	need xfs
}

start() {
	ebegin "Starting autostartx"
	start-stop-daemon --start --quiet --background --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 $?
}
26 Dec 2004: rc script updated
Last edited by moowei on Mon Dec 27, 2004 4:00 am, edited 2 times in total.
Top
kgraehl
n00b
n00b
User avatar
Posts: 54
Joined: Sat Sep 13, 2003 11:10 am
Contact:
Contact kgraehl
Website

  • Quote

Post by kgraehl » Thu Dec 23, 2004 7:21 am

That worked beautifully!

I'm configuring my gentoo on this old laptop to init just right, have everything autoconfigured, and boot into a very minimal fvwm/rox environment, where my mother should be able to use the internet, email, and office.

I wonder if I could get X to start even sooner, so all those init scripts wouldn't be displayed; alas it didn't seem to work. And I'm not going to waste any more time trying to get gensplash working!

Anyway, much thanks! This is exactly what I was looking for.
Top
moowei
n00b
n00b
Posts: 20
Joined: Thu Apr 22, 2004 6:02 pm

  • Quote

Post by moowei » Mon Dec 27, 2004 3:58 am

hey there,
sorry about the alsa problem..it's because of the permission bug. I worked around this by doing

Code: Select all

#chmod 660 /dev/snd/*
the user still need to be in the audio group.

but, of course, it would be the best if some one can fix this problem from the code.
Top
planetsheinker
Guru
Guru
User avatar
Posts: 403
Joined: Thu Feb 26, 2004 3:59 am
Location: Israel

  • Quote

Post by planetsheinker » Mon Dec 27, 2004 5:49 pm

moowei,
How about this:
Add the following line to the /etc/init.d/local.start:

Code: Select all

su -c USERNAME startx
Exschange USERNAME with acctual username
Unfortunatly I cant try it right now ( dont have linux installed right now )
but I geuss it would work too.
Top
moowei
n00b
n00b
Posts: 20
Joined: Thu Apr 22, 2004 6:02 pm

  • Quote

Post by moowei » Tue Dec 28, 2004 2:34 am

I tried using 'su' before, but it turned out that Alsa would still not work properly.
Top
planetsheinker
Guru
Guru
User avatar
Posts: 403
Joined: Thu Feb 26, 2004 3:59 am
Location: Israel

  • Quote

Post by planetsheinker » Tue Dec 28, 2004 5:56 am

moowei wrote:I tried using 'su' before, but it turned out that Alsa would still not work properly.
Hmmm... that's interesting...
I wonder why is that so?
What does ALSA got to do with it?
Top
djm
Arch/Herd Tester
Arch/Herd Tester
User avatar
Posts: 690
Joined: Mon Apr 12, 2004 1:00 pm
Location: Wadham College, Oxford

  • Quote

Post by djm » Tue Dec 28, 2004 2:49 pm

I have the following in my ~/.bashrc (well, actually ~/.zshrc):

Code: Select all

/home/djm/logincheck `/bin/tty`
where logincheck is as follows:

Code: Select all

if [ "$@" = "/dev/vc/6" ] ; then
	/usr/X11R6/bin/startx
fi
If I login on virtual console 6 X gets started, if I log in anywhere else it doesn't
the forums.gentoo.org poster formally known as metal leper
Top
moowei
n00b
n00b
Posts: 20
Joined: Thu Apr 22, 2004 6:02 pm

  • Quote

Post by moowei » Fri Dec 31, 2004 2:31 pm

Hmmm... that's interesting...
I wonder why is that so?
What does ALSA got to do with it?
I noticed that when loging in from a virtual console( from "login" program) or from gdm (not xdm!!), the owner of /dev/snd/* will be set to the user. But if I use 'su', the owner will remain the same (because it's in use by the previous logged-in user).
Top
djm
Arch/Herd Tester
Arch/Herd Tester
User avatar
Posts: 690
Joined: Mon Apr 12, 2004 1:00 pm
Location: Wadham College, Oxford

  • Quote

Post by djm » Fri Dec 31, 2004 2:38 pm

I've heard that this is pam related, but I've not checked. Might be worth looking into though (don't know why it doesn't happen with xdm though...)
the forums.gentoo.org poster formally known as metal leper
Top
thebigslide
l33t
l33t
User avatar
Posts: 792
Joined: Thu Dec 23, 2004 12:25 pm
Location: under a car or on top of a keyboard

  • Quote

Post by thebigslide » Fri Dec 31, 2004 8:21 pm

The quick and dirty fix to this is the following:
add:
su USER -c 'source /etc/profile;startx'
to /etc/conf.d/local.start
the source /etc/profile is needed to initialize environment variables that would otherwise not be set.
Use the same form for VNC servers and you won't end up with 'xauth not on your path' errors.

There should be no permissions issues with alsa if your user is in the audio group. Alsa should be working off GROUP permissions and alsa should be loaded at boot time before /etc/init.d/local is even looked at by init.
Top
moowei
n00b
n00b
Posts: 20
Joined: Thu Apr 22, 2004 6:02 pm

  • Quote

Post by moowei » Sun Feb 13, 2005 6:06 am

djm,
I've heard that this is pam related, but I've not checked. Might be worth looking into though (don't know why it doesn't happen with xdm though...)
yeah...you are right.. I am guessing it is pam related. don't have time to study that for now :(

thebigslide,
There should be no permissions issues with alsa if your user is in the audio group. Alsa should be working off GROUP permissions and alsa should be loaded at boot time before /etc/init.d/local is even looked at by init.
no, I still have problem with that. The reason is, on my computer, /dev/snd/* is set to 600 by default, so unless you are the owner of those devices, you can't access those files. Of course a quick hack would be changing the permission to 660, which is what I did. In order to become the owner of those files when you login, I think you have to start with PAM.
btw, I compiled alsa as a kernel module. not sure if that is why the devices are set to 600 by default.

But thanks for reminding me the environment variables (/etc/profile), I did forget that.
There won't be a problem if you start programs from xterm or gnome-terminal since they setup the enviroment variables for you; but when executing files like acroread, ooffice (whose binary files are not in PATH) from the Windows Manager directly (like icewm app menu), there will be a PATH error. One good example is acroread plugin for firefox would not work.

I add a sh script as the frontend of the binary file to solve this problem:
( the original autostartx C file is renamed to autostartx-bin)

autostartx:

Code: Select all

#!/bin/sh
# this script sets system env vars

test -f /etc/profile && source /etc/profile

exec autostartx-bin "$@"
remove the line

Code: Select all

putenv("PATH=/bin:/usr/bin:/usr/X11R6/bin");
from the original C file, and compile it as autostartx-bin.

put the two files autostartx & autostartx-bin in /sbin

the startup script need not to be modified at all.

If anyone needs a complete install process, I will write one. otherwise, I'm too lazy :P
the install process has been updated

comments are welcome
Top
artificio
Apprentice
Apprentice
Posts: 183
Joined: Wed Sep 15, 2004 9:46 pm

  • Quote

Post by artificio » Tue Sep 27, 2005 8:28 pm

I'm having problems with some needed services, it gets to xfs and then complains., './autostartx broken' doesn't list anything...
Any ideas, if you're still around? ;)
Top
moowei
n00b
n00b
Posts: 20
Joined: Thu Apr 22, 2004 6:02 pm

  • Quote

Post by moowei » Sat Oct 01, 2005 7:25 pm

sorry, no clue.
did you compile and put autostart-bin (the binary) in /sbin?
the filename is a little bit confusing
Top
artificio
Apprentice
Apprentice
Posts: 183
Joined: Wed Sep 15, 2004 9:46 pm

  • Quote

Post by artificio » Wed Oct 05, 2005 10:21 am

Yup, what's really odd is that now when I run ./autostartx broken as root, I get permission denied... :? I'll look at it more tommorow after I drop the computer off at my moms house.
Top
MasterC
Apprentice
Apprentice
Posts: 150
Joined: Sun May 25, 2003 9:48 pm
Location: Woods Cross, UT

  • Quote

Post by MasterC » Wed Oct 26, 2005 9:47 pm

I see that xfs, the X font server, is required. I just tried to emerge it, and it's masked. I'll happily unmask and emerge it, but I'm wondering why this is a dependency?

Thanks!

Cool
Top
bobpaul
Tux's lil' helper
Tux's lil' helper
Posts: 148
Joined: Tue Aug 09, 2005 8:55 am

XFS is NOT required

  • Quote

Post by bobpaul » Wed Nov 16, 2005 3:44 am

Change "NEED xfs" to "USE xfs" to allow force this to start after xfs if xfs is in the startup scripts, but still work without it.

Works fine for me without it and shaves off about 3-5 seconds over using GDM with autologin

[Edit] Put this in my boot runlevel (rc-update add autostartx boot) and X began to load 20seconds after grub right as the default runlevel was beginning. This shaved 15 seconds off my boot.
[Edit2] When launched from boot runlevel, occasionally the screen gets corrupted by the time gnome loads. Have to Ctrl+Alt+Backspace and then manually run startx.
Last edited by bobpaul on Tue Nov 22, 2005 3:44 pm, edited 1 time in total.
Top
artificio
Apprentice
Apprentice
Posts: 183
Joined: Wed Sep 15, 2004 9:46 pm

  • Quote

Post by artificio » Wed Nov 16, 2005 8:15 am

Thanks for the tip bobpaul! :) I've tried adding it to boot instead of default, but there's no change in startup, x runs after everthing else has started either way. :?
Top
bobpaul
Tux's lil' helper
Tux's lil' helper
Posts: 148
Joined: Tue Aug 09, 2005 8:55 am

  • Quote

Post by bobpaul » Tue Nov 22, 2005 3:46 pm

artificio wrote:Thanks for the tip bobpaul! :) I've tried adding it to boot instead of default, but there's no change in startup, x runs after everthing else has started either way. :?
Do you have RC_PARALLEL_STARTUP="yes" set in your /etc/conf.d/rc?
Top
Carpi
n00b
n00b
Posts: 20
Joined: Wed Nov 23, 2005 7:54 am
Location: Berlin (Germany)

  • Quote

Post by Carpi » Thu Nov 24, 2005 11:48 am

What is the difference (pros/cons) between using the autostartx solution and adding

Code: Select all

su USER -c 'source /etc/profile;startx'
to /etc/conf.d/local.start (in combination with a 'chmod 660 /dev/snd/*') ?
PII-MMX-350, 256MB; eth0: 2Mbit flat via 3c59x module; Kernel 2.6.12-gentoo-r6; X+Icewm
Top
timbo
Apprentice
Apprentice
User avatar
Posts: 231
Joined: Mon Jul 29, 2002 7:21 am
Location: New Zealand

  • Quote

Post by timbo » Sun Jan 08, 2006 7:36 pm

Guy's this worked a charm for me... wanted autologin without *dm because of all the app's that get onto my system. Building a mythtv box and now I'm one step closer...

Now I just have to get the nVidia driver sorted out 'cause it keep's faulting and loging xid, bummer having to use vesa...

Regards
Tim
8)
Linux User: 303160
Top
MoreCoffeePlease
n00b
n00b
Posts: 36
Joined: Tue May 18, 2004 9:11 pm
Location: London

  • Quote

Post by MoreCoffeePlease » Mon Jan 09, 2006 7:39 pm

Perfect! Just what I needed!

Finally the Mythtv box is that much more user friendy for the SO if things go wrong and need a reboot (which they haven't for a while since I stopped trying to get it to cope with wireless....)

Thanks! :lol:
Top
Post Reply

41 posts
  • 1
  • 2
  • Next

Return to “Desktop Environments”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic