473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading binary data

OK so here is my task. I want to get at the data stored in
/var/account/pacct, which stores process accounting data, so that I can
make it into a more human understandable format then what the program
sa can do. The thing is, its in a binary format and an example program
that reads some data from the file is done in C using a struct defined
in sys/acct.h.

http://www.linuxjournal.com/articles...44/6144l2.html

So I was wondering how can I do the same thing, but in python? I'm
still learning so please be gentle.

David

Nov 23 '05 #1
6 4362
On 2005-11-23, David M <Ne*******@yahoo.com> wrote:
OK so here is my task. I want to get at the data stored in
/var/account/pacct, which stores process accounting data, so that I can
make it into a more human understandable format then what the program
sa can do. The thing is, its in a binary format and an example program
that reads some data from the file is done in C using a struct defined
in sys/acct.h.

http://www.linuxjournal.com/articles...44/6144l2.html

So I was wondering how can I do the same thing, but in python? I'm
still learning so please be gentle.


use the struct module

http://www.python.org/doc/current/li...le-struct.html

--
Grant Edwards grante Yow! O.K.! Speak with a
at PHILADELPHIA ACCENT!! Send
visi.com out for CHINESE FOOD!! Hop
a JET!
Nov 23 '05 #2
David M wrote:
OK so here is my task. I want to get at the data stored in
/var/account/pacct, which stores process accounting data, so that I can
make it into a more human understandable format then what the program
sa can do. The thing is, its in a binary format and an example program
that reads some data from the file is done in C using a struct defined
in sys/acct.h.

http://www.linuxjournal.com/articles...44/6144l2.html

So I was wondering how can I do the same thing, but in python? I'm
still learning so please be gentle.


outline:

1. load the data

f = open(filename, "rb")

data = f.read()

2. parse it:

http://docs.python.org/lib/module-struct.html

</F>

Nov 23 '05 #3
Thanks but the C Struct describing the data doesn't match up with the
list on the module-struct page.

this is the acct.h file

#ifndef _SYS_ACCT_H
#define _SYS_ACCT_H 1

#include <features.h>

#define __need_time_t
#include <time.h>
#include <sys/types.h>

__BEGIN_DECLS

#define ACCT_COMM 16

/*
comp_t is a 16-bit "floating" point number with a 3-bit base 8
exponent and a 13-bit fraction. See linux/kernel/acct.c for the
specific encoding system used.
*/

typedef u_int16_t comp_t;

struct acct
{
char ac_flag; /* Accounting flags. */
u_int16_t ac_uid; /* Accounting user ID. */
u_int16_t ac_gid; /* Accounting group ID. */
u_int16_t ac_tty; /* Controlling tty. */
u_int32_t ac_btime; /* Beginning time. */
comp_t ac_utime; /* Accounting user time. */
comp_t ac_stime; /* Accounting system time. */
comp_t ac_etime; /* Accounting elapsed time. */
comp_t ac_mem; /* Accounting average memory usage. */
comp_t ac_io; /* Accounting chars transferred. */
comp_t ac_rw; /* Accounting blocks read or written. */
comp_t ac_minflt; /* Accounting minor pagefaults. */
comp_t ac_majflt; /* Accounting major pagefaults. */
comp_t ac_swaps; /* Accounting number of swaps. */
u_int32_t ac_exitcode; /* Accounting process exitcode. */
char ac_comm[ACCT_COMM+1]; /* Accounting command name. */
char ac_pad[10]; /* Accounting padding bytes. */
};

enum
{
AFORK = 0x01, /* Has executed fork, but no exec. */
ASU = 0x02, /* Used super-user privileges. */
ACORE = 0x08, /* Dumped core. */
AXSIG = 0x10 /* Killed by a signal. */
};

#define AHZ 100
/* Switch process accounting on and off. */
extern int acct (__const char *__filename) __THROW;

__END_DECLS

#endif /* sys/acct.h */

What are u_ini16_t and comp_t? And what about the enum section?

Nov 23 '05 #4
In comp.lang.python, "David M" wrote:
Thanks but the C Struct describing the data doesn't match up with the
list on the module-struct page.
Then you're going to have to do some bit-bashing.
comp_t is a 16-bit "floating" point number with a 3-bit base 8
exponent and a 13-bit fraction. See linux/kernel/acct.c for the
specific encoding system used.
Yoinks! That's just a bit too clever.

You'll have to impliment that yourself.
What are u_ini16_t and comp_t?
Dunno, I guess you'll have to look at the sources and find the
typedefs.
And what about the enum section?


It's probably a 32-bit integer, but it may take some
experimentation to confrim that.

--
Grant Edwards grante Yow! I invented skydiving
at in 1989!
visi.com
Nov 23 '05 #5
"David M" wrote:
What are u_ini16_t and comp_t?
comp_t is explained in the file you posted:
/*
comp_t is a 16-bit "floating" point number with a 3-bit base 8
exponent and a 13-bit fraction. See linux/kernel/acct.c for the
specific encoding system used.
*/

typedef u_int16_t comp_t;
as the comment says, comp_t is a 16-bit value. you can read it in as
an integer, but you have to convert it to a floating point according to
the encoding mentioned above.

the typedef says that comp_t is stored as a u_int16_t, which means
that it's 16-bit value too. judging from the name, and the fields using
it, it's safe to assume that it's an unsigned 16-bit integer.
And what about the enum section?
it just defines a bunch of symbolic values; AFORK is 1, ASU is 2, etc.
enum
{
AFORK = 0x01, /* Has executed fork, but no exec. */
ASU = 0x02, /* Used super-user privileges. */
ACORE = 0x08, /* Dumped core. */
AXSIG = 0x10 /* Killed by a signal. */
};


at this point, you should be able to do a little experimentation. read in
a couple of bytes (64 bytes should be enough), print them out, and try
to see if you can match the bytes with the description above.

import struct

f = open(filename, "rb")

data = f.read(64)

# hex dump
print data.encode("hex")

# list of decimal byte values
print map(ord, data)

# struct test (keep adding type codes until you're sorted everything out)
format = "BHHHHHH"
print struct.unpack(format, struct.calcsize(format))

</F>

Nov 23 '05 #6
"David M" <Ne*******@yahoo.com> writes:
Thanks but the C Struct describing the data doesn't match up with the
list on the module-struct page.

this is the acct.h file

[...]

Tooting my ctypes horn (sorry for that):

thomas@linux:~/ctypes> locate acct.h
/usr/include/linux/acct.h
/usr/include/sys/acct.h
thomas@linux:~/ctypes> python ctypes/wrap/h2xml.py sys/acct.h -o acct.xml
creating xml output file ...
running: gccxml /tmp/tmpSWogJs.cpp -fxml=acct.xml
thomas@linux:~/ctypes> python ctypes/wrap/xml2py.py acct.xml -o acct.py
thomas@linux:~/ctypes> python
Python 2.4.1a0 (#1, Oct 23 2004, 15:48:15)
[GCC 3.3.1 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import acct
acct.acct <class 'acct.acct'> acct.AFORK 1 acct.ASU 2 acct.acct.ac_comm <Field type=c_char_Array_17, ofs=36, size=17> acct.acct.ac_utime <Field type=c_ushort, ofs=12, size=2> from ctypes import sizeof
sizeof(acct.acct) 64 acct.acct.ac_flag <Field type=c_char, ofs=0, size=1>

thomas@linux:~/ctypes>

But it won't help you to decode/encode the comp_t fields into floats.
Note that the h2xml.py script requires gccxml.

Thomas
Nov 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
6
by: John Wright | last post by:
I am trying to read the data from a device on a serial port. I connect just fine and can receive data fine in text mode but not in binary mode. In text mode the data from the device comes in...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
6
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.