Sponsored Content
Top Forums Programming Problem in determining runlevel through a C program Post 302519490 by royalibrahim on Wednesday 4th of May 2011 06:33:00 AM
Old 05-04-2011
Problem in determining runlevel through a C program

Hi,

I am trying with the following code to retrieve the runlevel of my Linux Ubuntu 8.04 system by reading the "utmp" database. But I am getting blank output. May I know what correction I should do inorder to make this program to work?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <utmp.h>

void dump_entry(struct utmp* ut) {
    char s0[9], s1[5], s2[13];

    bzero(s0, 9);
    bzero(s1, 5);
    bzero(s2, 13);

    strncpy(s0, ut->ut_user, 8);
    strncpy(s1, ut->ut_id, 4);
    strncpy(s2, ut->ut_line, 12);

    fprintf(stderr, "user: %s, id: %s, line: %s\npid=%d, type=%d, exit=%d:%d\n",
        s0, s1, s2, ut->ut_pid, ut->ut_type, ut->ut_exit.e_termination,
        ut->ut_exit.e_exit);
}

int main() {
    struct utmp* ut;
    struct utmp filter;

    setutent();
    filter.ut_type = RUN_LVL;

    ut = getutid(&filter);

    if (!ut) {
        perror("getutent");
        fprintf(stderr, "Can't find RUN_LVL entry in utmp");
        exit(1);
    }

    dump_entry(ut);

    if (!strncmp(ut->ut_line, "run-level ", 9)) {
        fprintf(stdout, "%c\n", ut->ut_line[10]);
    }

    endutent();

    return (0);
}


Last edited by royalibrahim; 05-04-2011 at 09:42 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Runlevel in Freebsd?

Hello, Is there any runlevels in freebsd? Like in linux or solaris? Thanks -I (2 Replies)
Discussion started by: Insomniac
2 Replies

2. Linux

runlevel from cmdline or lilo?

Hi, Is it possible to specify runlevel from bootloader command line? I would like to override settings from /etc/inittab without changing it. Thanks (2 Replies)
Discussion started by: odys
2 Replies

3. Linux

Runlevel(s)

Can some exsplain to to me what runlevel 1-9 do? I need to know. (5 Replies)
Discussion started by: Irish Jimmy
5 Replies

4. UNIX for Advanced & Expert Users

Multiple runlevel options at Bootup

Is it possible to give multiple runlevel options during boot up.. When the Welcome screen appears, i want to give multiple runlevel options.. So the user can boot into any desired runlevel he wants.. Found this kinda interesting.. Any hints and solutions please? (2 Replies)
Discussion started by: srikumar_cs
2 Replies

5. UNIX for Dummies Questions & Answers

Problem with writing a program

Hi guys I'm having trouble with trying to create a script which calculates the grade of a student and the marks out of 300. The grades are: 0-49% fail 50-59% pass 60-69% credit pass 70-79% distinction 80-100% high distinction less than 0 or greater than 100 displays error message. My... (1 Reply)
Discussion started by: CompNoob
1 Replies

6. Shell Programming and Scripting

Problem determining file

I got the following code, it partially works. Can someone tell me why it partially doenst work? #!/bin/sh file=$1 if then echo "File is a directory" else echo "File is not a directory!" fi heres the output: philip@philip-laptop:~/Desktop$ sh exFive.sh test.java File is... (4 Replies)
Discussion started by: philmetz
4 Replies

7. SuSE

New runlevel service

Ok, I am attempting to add a new program to startup during the runlevel 3. I am using Suse 10. I made a script lets call it foostart and placed it in /etc/init.d. It has 777 permissions on the script. I then created a link ln -s /etc/init.d/foostart /etc/init.d/rc3.d/S99foostart But during... (9 Replies)
Discussion started by: benefactr
9 Replies

8. Programming

Problem with Socket program..

I wrote a program which will send a message to multiple clients(i.e, broadcasting) that are connected to a server.Once when the client receives a message from the server ,the client should read a file in the server and display it in the client.The client which responds (i.e, client wants all the... (3 Replies)
Discussion started by: vigneshinbox
3 Replies

9. Red Hat

Running a script in runlevel 1

hi all i have to run a script in run level 1 automatically i.e when i give init 1 from runlevel 3 that script should run after entering into runlevel 1. where can i put that script so that it will run as i said above..??? something similar to rc.local but this will run in runlevel 3(... (1 Reply)
Discussion started by: sagar_md
1 Replies

10. Solaris

runlevel issue

hi all i am using solaris 10 with run level 3.(Graphical Interface). I am a beginner to solaris so that i just want to do some R&D with my machine. I decided to boot into the command line interface, so i go througth the internet and find that the following command should be given to boot into the... (8 Replies)
Discussion started by: kingston
8 Replies
GETUTENT(3)							 Library functions						       GETUTENT(3)

NAME
getutent, getutid, getutline, pututline, setutent, endutent, utmpname - access utmp file entries SYNOPSIS
#include <utmp.h> struct utmp *getutent(void); struct utmp *getutid(struct utmp *ut); struct utmp *getutline(struct utmp *ut); struct utmp *pututline(struct utmp *ut); void setutent(void); void endutent(void); void utmpname(const char *file); DESCRIPTION
utmpname() sets the name of the utmp-format file for the other utmp functions to access. If utmpname() is not used to set the filename before the other functions are used, they assume _PATH_UTMP, as defined in <paths.h>. setutent() rewinds the file pointer to the beginning of the utmp file. It is generally a Good Idea to call it before any of the other functions. endutent() closes the utmp file. It should be called when the user code is done accessing the file with the other functions. getutent() reads a line from the current file position in the utmp file. It returns a pointer to a structure containing the fields of the line. getutid() searches forward from the current file position in the utmp file based upon ut. If ut->ut_type is RUN_LVL, BOOT_TIME, NEW_TIME, or OLD_TIME, getutid() will find the first entry whose ut_type field matches ut->ut_type. If ut->ut_type is one of INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS, getutid() will find the first entry whose ut_id field matches ut->ut_id. getutline() searches forward from the current file position in the utmp file. It scans entries whose ut_type is USER_PROCESS or LOGIN_PROCESS and returns the first one whose ut_line field matches ut->ut_line. pututline() writes the utmp structure ut into the utmp file. It uses getutid() to search for the proper place in the file to insert the new entry. If it cannot find an appropriate slot for ut, pututline() will append the new entry to the end of the file. RETURN VALUE
getutent(), getutid(), getutline() and pututline() return a pointer to a static struct utmp on success, and NULL on failure. EXAMPLE
The following example adds and removes a utmp record, assuming it is run from within a pseudo terminal. For usage in a real application, you should check the return values of getpwuid() and ttyname(). #include <string.h> #include <stdlib.h> #include <pwd.h> #include <unistd.h> #include <utmp.h> int main(int argc, char *argv[]) { struct utmp entry; system("echo before adding entry:;who"); entry.ut_type=USER_PROCESS; entry.ut_pid=getpid(); strcpy(entry.ut_line,ttyname(0)+strlen("/dev/")); /* only correct for ptys named /dev/tty[pqr][0-9a-z] */ strcpy(entry.ut_id,ttyname(0)+strlen("/dev/tty")); time(&entry.ut_time); strcpy(entry.ut_user,getpwuid(getuid())->pw_name); memset(entry.ut_host,0,UT_HOSTSIZE); entry.ut_addr=0; setutent(); pututline(&entry); system("echo after adding entry:;who"); entry.ut_type=DEAD_PROCESS; memset(entry.ut_line,0,UT_LINESIZE); entry.ut_time=0; memset(entry.ut_user,0,UT_NAMESIZE); setutent(); pututline(&entry); system("echo after removing entry:;who"); endutent(); return 0; } FILES
/var/run/utmp database of currently logged-in users /var/log/wtmp database of past user logins CONFORMING TO
XPG 2, SVID 2, Linux FSSTND 1.2 In XPG2 and SVID2 the function pututline() is documented to return void, and that is what it does on many systems (AIX, HPUX, Linux libc5). HPUX introduces a new function _pututline() with the prototype given above for pututline() (also found in Linux libc5). All these functions are obsolete now on non-Linux systems. POSIX 1003.1-2001, following XPG4.2, does not have any of these functions, but instead uses #include <utmpx.h> struct utmpx *getutxent(void); struct utmpx *getutxid(const struct utmpx *); struct utmpx *getutxline(const struct utmpx *); struct utmpx *pututxline(const struct utmpx *); void setutxent(void); void endutxent(void); The utmpx structure is a superset of the utmp structure, with additional fields, and larger versions of the existing fields. The corre- sponding files are often /var/*/utmpx and /var/*/wtmpx. Linux glibc on the other hand does not use utmpx since its utmp structure is already large enough. The functions getutxent etc. are aliases for getutent etc. SEE ALSO
utmp(5) 1996-07-25 GETUTENT(3)
All times are GMT -4. The time now is 06:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy