Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
code to auto login without *dm
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
moowei
n00b
n00b


Joined: 22 Apr 2004
Posts: 20

PostPosted: Sun Dec 19, 2004 6:34 am    Post subject: code to auto login without *dm Reply with quote

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:
#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:
#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:
#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:
edit /etc/init.d/autostartx


5. remove xdm from default runlevel, and add autostartx to default runlevel
Code:
#rc-update del xdm
#rc-update add autostartx default


7. reboot and try!
Code:
#reboot



autostartx-bin.c
Code:

//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:
#!/bin/sh
# this script sets system env vars

test -f /etc/profile && source /etc/profile
exec autostartx-bin "$@"


autostartx-rc (rc script)
Code:
#!/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
Back to top
View user's profile Send private message
kgraehl
n00b
n00b


Joined: 13 Sep 2003
Posts: 54

PostPosted: Thu Dec 23, 2004 6:14 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
kgraehl
n00b
n00b


Joined: 13 Sep 2003
Posts: 54

PostPosted: Thu Dec 23, 2004 6:23 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
kgraehl
n00b
n00b


Joined: 13 Sep 2003
Posts: 54

PostPosted: Thu Dec 23, 2004 6:44 am    Post subject: Reply with quote

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

$ alsamixer

alsamixer: function snd_ctl_open failed for default: Permission denied


Adding default to the audio group in /etc/groups didn't fix this.
Back to top
View user's profile Send private message
moowei
n00b
n00b


Joined: 22 Apr 2004
Posts: 20

PostPosted: Thu Dec 23, 2004 6:54 am    Post subject: Reply with quote

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:
#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:
#!/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
Back to top
View user's profile Send private message
kgraehl
n00b
n00b


Joined: 13 Sep 2003
Posts: 54

PostPosted: Thu Dec 23, 2004 7:21 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
moowei
n00b
n00b


Joined: 22 Apr 2004
Posts: 20

PostPosted: Mon Dec 27, 2004 3:58 am    Post subject: Reply with quote

hey there,
sorry about the alsa problem..it's because of the permission bug. I worked around this by doing
Code:
#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.
Back to top
View user's profile Send private message
planetsheinker
Guru
Guru


Joined: 26 Feb 2004
Posts: 403
Location: Israel

PostPosted: Mon Dec 27, 2004 5:49 pm    Post subject: Reply with quote

moowei,
How about this:
Add the following line to the /etc/init.d/local.start:
Code:
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.
Back to top
View user's profile Send private message
moowei
n00b
n00b


Joined: 22 Apr 2004
Posts: 20

PostPosted: Tue Dec 28, 2004 2:34 am    Post subject: Reply with quote

I tried using 'su' before, but it turned out that Alsa would still not work properly.
Back to top
View user's profile Send private message
planetsheinker
Guru
Guru


Joined: 26 Feb 2004
Posts: 403
Location: Israel

PostPosted: Tue Dec 28, 2004 5:56 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
djm
Arch/Herd Tester
Arch/Herd Tester


Joined: 12 Apr 2004
Posts: 690
Location: Wadham College, Oxford

PostPosted: Tue Dec 28, 2004 2:49 pm    Post subject: Reply with quote

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

Code:
/home/djm/logincheck `/bin/tty`


where logincheck is as follows:

Code:
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
Back to top
View user's profile Send private message
moowei
n00b
n00b


Joined: 22 Apr 2004
Posts: 20

PostPosted: Fri Dec 31, 2004 2:31 pm    Post subject: Reply with quote

Quote:
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).
Back to top
View user's profile Send private message
djm
Arch/Herd Tester
Arch/Herd Tester


Joined: 12 Apr 2004
Posts: 690
Location: Wadham College, Oxford

PostPosted: Fri Dec 31, 2004 2:38 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
thebigslide
l33t
l33t


Joined: 23 Dec 2004
Posts: 792
Location: under a car or on top of a keyboard

PostPosted: Fri Dec 31, 2004 8:21 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
moowei
n00b
n00b


Joined: 22 Apr 2004
Posts: 20

PostPosted: Sun Feb 13, 2005 6:06 am    Post subject: Reply with quote

djm,
Quote:
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,
Quote:
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:
#!/bin/sh
# this script sets system env vars

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

exec autostartx-bin "$@"


remove the line
Code:
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
Back to top
View user's profile Send private message
artificio
Apprentice
Apprentice


Joined: 15 Sep 2004
Posts: 183

PostPosted: Tue Sep 27, 2005 8:28 pm    Post subject: Reply with quote

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? ;)
Back to top
View user's profile Send private message
moowei
n00b
n00b


Joined: 22 Apr 2004
Posts: 20

PostPosted: Sat Oct 01, 2005 7:25 pm    Post subject: Reply with quote

sorry, no clue.
did you compile and put autostart-bin (the binary) in /sbin?
the filename is a little bit confusing
Back to top
View user's profile Send private message
artificio
Apprentice
Apprentice


Joined: 15 Sep 2004
Posts: 183

PostPosted: Wed Oct 05, 2005 10:21 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
MasterC
Apprentice
Apprentice


Joined: 25 May 2003
Posts: 150
Location: Woods Cross, UT

PostPosted: Wed Oct 26, 2005 9:47 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
bobpaul
Tux's lil' helper
Tux's lil' helper


Joined: 09 Aug 2005
Posts: 148

PostPosted: Wed Nov 16, 2005 3:44 am    Post subject: XFS is NOT required Reply with quote

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
Back to top
View user's profile Send private message
artificio
Apprentice
Apprentice


Joined: 15 Sep 2004
Posts: 183

PostPosted: Wed Nov 16, 2005 8:15 am    Post subject: Reply with quote

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. :?
Back to top
View user's profile Send private message
bobpaul
Tux's lil' helper
Tux's lil' helper


Joined: 09 Aug 2005
Posts: 148

PostPosted: Tue Nov 22, 2005 3:46 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
Carpi
n00b
n00b


Joined: 23 Nov 2005
Posts: 20
Location: Berlin (Germany)

PostPosted: Thu Nov 24, 2005 11:48 am    Post subject: Reply with quote

What is the difference (pros/cons) between using the autostartx solution and adding
Code:
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
Back to top
View user's profile Send private message
timbo
Apprentice
Apprentice


Joined: 29 Jul 2002
Posts: 231
Location: New Zealand

PostPosted: Sun Jan 08, 2006 7:36 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
MoreCoffeePlease
n00b
n00b


Joined: 18 May 2004
Posts: 36
Location: London

PostPosted: Mon Jan 09, 2006 7:39 pm    Post subject: Reply with quote

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:
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum