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(3C)						   Standard C Library Functions 					      getutent(3C)

NAME
getutent, getutid, getutline, pututline, setutent, endutent, utmpname - user accounting database functions SYNOPSIS
#include <utmp.h> struct utmp *getutent(void); struct utmp *getutid(const struct utmp *id); struct utmp *getutline(const struct utmp *line); struct utmp *pututline(const struct utmp *utmp); void setutent(void); void endutent(void); int utmpname(const char *file); DESCRIPTION
These functions provide access to the user accounting database, utmp. Entries in the database are described by the definitions and data structures in <utmp.h>. The utmp structure contains the following members: char ut_user[8]; /* user login name */ char ut_id[4]; /* /sbin/inittab id (usually line #) */ char ut_line[12]; /* device name (console, lnxx) */ short ut_pid; /* process id */ short ut_type; /* type of entry */ struct exit_status ut_exit; /* exit status of a process */ /* marked as DEAD_PROCESS */ time_t ut_time; /* time entry was made */ The structure exit_status includes the following members: short e_termination; /* termination status */ short e_exit; /* exit status */ getutent() The getutent() function reads in the next entry from a utmp database. If the database is not already open, it opens it. If it reaches the end of the database, it fails. getutid() The getutid() function searches forward from the current point in the utmp database until it finds an entry with a ut_type matching id->ut_type if the type specified is RUN_LVL, BOOT_TIME, DOWN_TIME, OLD_TIME, or NEW_TIME. If the type specified in id is INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS, then getutid() will return a pointer to the first entry whose type is one of these four and whose ut_id member matches id->ut_id. If the end of database is reached without a match, it fails. getutline() The getutline() function searches forward from the current point in the utmp database until it finds an entry of the type LOGIN_PROCESS or ut_line string matching the line->ut_line string. If the end of database is reached without a match, it fails. pututline() The pututline() function writes the supplied utmp structure into the utmp database. It uses getutid() to search forward for the proper place if it finds that it is not already at the proper place. It is expected that normally the user of pututline() will have searched for the proper entry using one of the these functions. If so, pututline() will not search. If pututline() does not find a matching slot for the new entry, it will add a new entry to the end of the database. It returns a pointer to the utmp structure. When called by a non-root user, pututline() invokes a setuid() root program to verify and write the entry, since the utmp database is normally writable only by root. In this event, the ut_name member must correspond to the actual user name associated with the process; the ut_type member must be either USER_PROCESS or DEAD_PROCESS; and the ut_line member must be a device special file and be writable by the user. setutent() The setutent() function resets the input stream to the beginning. This reset should be done before each search for a new entry if it is desired that the entire database be examined. endutent() The endutent() function closes the currently open database. utmpname() The utmpname() function allows the user to change the name of the database file examined to another file. If the file does not exist, this will not be apparent until the first attempt to reference the file is made. The utmpname() function does not open the file but closes the old file if it is currently open and saves the new file name. RETURN VALUES
A null pointer is returned upon failure to read, whether for permissions or having reached the end of file, or upon failure to write. If the file name given is longer than 79 characters, utmpname() returns 0. Otherwise, it returns 1. USAGE
These functions use buffered standard I/O for input, but pututline() uses an unbuffered non-standard write to avoid race conditions between processes trying to modify the utmp and wtmp databases. Applications should not access the utmp and wtmp databases directly, but should use these functions to ensure that these databases are maintained consistently. Using these functions, however, may cause applications to fail if user accounting data cannot be represented prop- erly in the utmp structure (for example, on a system where PIDs can exceed 32767). Use the functions described on the getutxent(3C) manual page instead. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
getutxent(3C), ttyslot(3C), utmpx(4), attributes(5) NOTES
The most current entry is saved in a static structure. Multiple accesses require that it be copied before further accesses are made. On each call to either getutid() or getutline(), the function examines the static structure before performing more I/O. If the contents of the static structure match what it is searching for, it looks no further. For this reason, to use getutline() to search for multiple occurrences, it would be necessary to zero out the static area after each success, or getutline() would just return the same structure over and over again. There is one exception to the rule about emptying the structure before further reads are done. The implicit read done by pututline() (if it finds that it is not already at the correct place in the file) will not hurt the contents of the static structure returned by the getutent(), getutid() or getutline() functions, if the user has just modified those contents and passed the pointer back to pututline(). SunOS 5.10 27 Oct 1998 getutent(3C)
All times are GMT -4. The time now is 01:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy