Monday, February 25, 2013

User space application to retrieve device file's minor number dynamically

Note: This works only if you have single device node file, working on how to make it work for more than one device file.

Please leave your comments or help me in making it generic :)

I had written an simple linked list device driver code (LKM). I was manually creating a device node/file i.e. "/dev/list0". Since someone asked me to "How to dynamically retrieve minor number" from user space application. Have posted my idea below,

Code snippet:

/* Dynamically getting the device file */
system("ls -als /dev/list* | awk '{print $7}' > device_file.txt");
/* Retrieve the device file name from file */
fp = fopen("device_file.txt", "r");
if( fp < 0 )
  fprintf(stdout, "Failed to open the file \n");
  fread(&amp, minor, 1, 1, fp);         
  fprintf(stdout, "%d \n", minor);   

 * Minor no read from file would be decimal value
 * i.e. '0' is interpreted as char '0' so decimal value is 48
 * converting it to exact decimal value
 * INFO: atoi() can also be used
*  minor = atoi(minor);
minor = minor - 0x30;
fprintf(stdout, "Final=%d \n", minor);   
snprintf(buf, sizeof("/dev/list0"), "/dev/list%d", minor);

fd = open(buf, O_RDWR);

Monday, February 15, 2010

RPM Development in Linux

1) You need EMAC editor.

2) Go to File and select New and type the file name as Test.spec.

3) Test.spec file is a file which as all the  specification required for ur RPM development.

4) Go to /usr/src/redhat/SPEC folder i.e. cd /usr/src/redhat/SPEC , copy that Test.spec to this directory.

5) Then,  go to SOURCE directory i.e. cd ../SOURCE , copy all the required files that u want it to be in ur RPM ex:- executable, library, sripts from ur HOME dir to  this SOURCE dir.

6) Open Test.spec in /usr/src/redhat/, define the name for the RPM package, version number etc. Specify for which architecture u want the RPM.

7) Create a tar file of the source directory which contains all the resources which are to be in ur RPM .
tar cvf name_RPM-version.tar.bz2  as in Test.spec source folder name .

8) Go to /usr/src/redhat/SPEC i.e. cd /usr/src/redhat/SPEC, run a rpmbuild command to build the RPM
rpmbuild -ba Test.spec.

9) RPM will be built and placed in /usr/src/redhat/RPMS/architecture folder i.e. if u have configured ur RPM for i386 arch then RPM built will be in /usr/src/redhat/RPMS/i386 folder.

** Any doubts and any help required for building RPM in Linux u r free to ask .
Even i developed my first RPM 3 months back... but still.... i learned more from it......