Problem in determining runlevel through a C program


 
Thread Tools Search this Thread
Top Forums Programming Problem in determining runlevel through a C program
# 1  
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..
# 2  
Old 05-04-2011
You do realize those calls are deprecated - umtpx is probably what is being used on your system.

I ran your code on my Solaris box - no problem. It still supports umtp calls. Go into your debugger and view all of your variables as you step thru the code.

Plus you should be using sizeof instead of a numeric constant for each element to strncpy, and bzero is not in your headers, try gcc -Wall for warnings.

Last edited by jim mcnamara; 05-04-2011 at 12:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question