I'm on FreeBSD11. I want to get all devices that connect to my system and save their information with c++. for this reason I run camcontrol devlist command. The output is:
<OCZ-VERTEX4 1.4.1.3> at scbus0 target 0 lun 0 (pass0,ada0)
<OCZ-AGILITY3 2.15> at scbus0 target 1 lun 0 (pass1,ada1)
<OCZ-AGILITY3 2.15> at scbus1 target 0 lun 0 (pass2,ada2)
<OCZ-AGILITY3 2.15> at scbus1 target 1 lun 0 (pass3,ada3)
<OCZ-AGILITY3 2.15> at scbus3 target 0 lun 0 (pass4,ada4)
< USB FLASH DRIVE PMAP> at scbus4 target 0 lun 0 (da0,pass5)
I put this output in a file, with getline in c++, only get the line from at scbus0 target 0 lun 0 (pass0,ada0) and ignore first part.
I want to get ada0,ada1,ada2,ada3,ada4,da0 then with /usr/local/sbin/smartctl -i /dev/<device> get it's information and for other like da0,cd0,... that won't have output in this command save their name.
I'm not sure if this is a good way or not. Please give me a better solution if exist, or help me to solve my problem.
If you're only interested in disks, you might consider trying to use libgeom(3) instead. It offers functions to get providers.
Another approach might be to use libcam to pull the device list as camcontrol does and then you can handle formatting as needed. You can look at how the camcontrol program does this in camcontrol.c 's getdevlist function.
Related
I am trying to programmatically track the amount of data in my receive buffer. I am receiving UDP data. After doing some research it seems that the only way to do this in Linux is to look at /proc/net/udp. This seems like a good solution until I realized that two applications could be listening to the same multicast group and I need to tell them apart. It seems that I am supposed to do this by determining what my inode is.
I spend some time looking into this and there are suggestions that sockfd_lookup or sock_from_file is the way to go but on my CentOS Linux machine, these functions do not seem to be available.
Can someone please help me to figure out which line in /proc/net/udp belongs to my application?
I started using the ioctl (handle, FIONREAD, &bytesInBuffer) call only to discover that in Linux this only returns the size of the first datagram packet in the buffer.
Google seems to suggest that the sockfd_lookup call can be used to get the inode but a grep in my /usr/local/include/ does not return these functions.
My linux/net.h seems pretty bare-bone compared to some I can find on google which includes structs like "socket" which has the sock member which I believe has the inode information. My linux/net.h on CentOs only is 58 lines long and has only a few #defines and an enum.
after a bit of fiddling I noticed that readlink("/proc/self/fd/$fd") (under Linux 5.3) gives me something like:
socket:[3753088]
back. I can parse this and use the resulting digits to look up the relevant line in /proc/net/udp:
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops
2867: 00000000:0BB8 00000000:0000 07 00000000:00000000 00:00000000 00000000 1000 0 3753088 2 000000003ae8e911 0
that said, I don't understand why you'd want to do this, but never mind!
I'm also not sure why you don't just look up by sock&peer name, which might be easier
I'm looking to create a function in C++ running on Linux that returns true if the CDRom media is a DVD and false if its anything else (e.g. Audio CD).
I have been using ioctl with linux/cdrom.h.
I tried using the DVD_READ_STRUCT but it always returns true. Maybe I'm using it incorrectly.
dvd_struct s
if (ioctl(hDEV, DVD_READ_STRUCT, &s)) {
return true;
}
Look at /proc/sys/dev/cdrom/info, it contains something like this:
CD-ROM information, Id: cdrom.c 3.20 2003/12/17
drive name: sr0
drive speed: 125
drive # of slots: 1
Can close tray: 1
Can open tray: 1
Can lock tray: 1
Can change speed: 1
Can select disk: 0
Can read multisession: 1
Can read MCN: 1
Reports media changed: 1
Can play audio: 1
Can write CD-R: 1
Can write CD-RW: 1
Can read DVD: 1
Can write DVD-R: 1
Can write DVD-RAM: 1
Can read MRW: 0
Can write MRW: 0
Can write RAM: 1
(it is updated by the kernel and available in all distros)
You can use this information in addition to the ioctl's from cdrom.h. Also keep in mind that cdrom.h is an attempt to create a standard interface, it does not yet cater for all manufacturers, some still using SCSI-codes or some other proprietary schemes. So to be safe you should also check at least using the SCSI ioctl codes - do #include <scsi/... to have them available.
The official documentation is a bit more helpful. You have to specify the request type and any required inputs before calling ioctl.
// Is it a DVD?
dvd_struct ds;
ds.type = DVD_STRUCT_PHYSICAL;
ds.physical.layer_num=0;
result = ioctl(drive, DVD_READ_STRUCT, &ds);
if (result == -1) {
perror("Probably not a DVD: ");
} else {
printf("Layer 0: %i to %i.\n", ds.physical.layer[0].start_sector, ds.physical.layer[0].end_sector);
}
The really interesting stuff requires issuing SCSI commands like dvd+rw-tools, cdrkit, and cdrdao. Doing this is a bit painful, though, and it's not necessary if you don't need to know if the disc is recordable, rewritable, or pressed.
I feel like this must have a simple answer, but I really don't know how to approach this.
For background, the stack of things is like this:
Python script -> C++ binary -(fork)-> actual thing we want to measure.
Essentially, we have a python script that simulates an environment by using tmp directories and running multiple instances of this network software stack we're developing. The script calls a host binary (which is unimportant here), and then, after it loads, a helper binary. The helper binary can be passed a parameter to daemonize, and when it does this, it forks in the usual way.
What we need to do is measure the daemon's CPU utilization, but I don't really know how to. What I have done is read the stat file periodically, but since the process daemonizes, I can't use echo $! to get its PID. Using ps aux | grep 'thing' works fine, but I think this is giving me the parent process, because the stat information looks like this:
1472582561 9455 (nlsr) S 1 9455 9455 0 -1 4218944 394 0 0 0 13 0 0 0 20 0 2 0 909820 184770560 3851 18446744073709551615 4194304 5318592 140734694817376 140734694810512 140084250723843 0 0 16781312 0 0 0 0 17 0 0 0 0 0 0 7416544 7421528 16224256 140734694825496 140734694825524 140734694825524 140734694825962 0
I know that the parent process should not be PID1, and definitely the utime field and similar should be greater than 13 clock ticks. This is what is leading me to conclude that this process is really the parent process, and not the forked child that's doing all the work.
I can modify pretty much any file necessary, but because of code review constraints, design specs., etc., the less I have to change along many files, the better.
Get the PID of the child reliably
fork() returns the PID of the child to the parent
Get the CPU stats from /proc/[PID]/stat
#14 utime - CPU time spent in user code, measured in clock ticks
In my code NVRAM is implemented as character device and I want to give a condition to check whether it is character device or not. How to implement it programmatically? How can I do it through c++ code.
In terminal I gave command cat /proc/devices and it lists:
Character devices:
1 mem
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/nvram
What does this number 5 in /dev/nvram denote?
The number denotes the device's major number, and the Character devices: heading in that listing tells you it's a character device.
If your character device is linked into the filesystem somewhere, like /dev/mydevice, you can also get information about it via the stat system call. The st_mode field of the struct stat structure can be tested with the S_ISCHR macro.
I am in a situation where I am extremely bandwidth limited and must devote most of the bandwidth to transferring one type of measurement data. Sometimes I will be sending out lots of this measurement data and other times I will just be waiting for events to occur (all of this is over a TCP socket).
I would like to be able to stream out the full data capture file (different than the measurements) in the background at a speed that is inversely proportional to the amount of measurements that I am sending back.
I am looking for a way to monitor how many bytes are being sent out the network interface in much the same was as the system monitor on Ubuntu. The source code for the system monitor relies on gnome libraries and since my program is on an embedded device, I would like to reduce the number of external libraries that I use. Does anybody know of a way to do this in C/C++ without many additional libraries on a standard Linux distribution?
One of the simplest ways is to parse the file: /proc/net/dev
Mine contains:
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo: 44865 1431 0 0 0 0 0 0 44865 1431 0 0 0 0 0 0
eth0:150117850 313734 0 0 0 0 0 0 34347178 271210 0 0 0 0 0 0
pan0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You could then write a parser that uses nothing other than the C/C++ libraries.
Bytes transmitted and recieved accessable via
/sys/class/net/eth0/statistics/tx_bytes and /sys/class/net/eth0/statistics/rx_bytes files.
$ cat /sys/class/net/net1/statistics/rx_bytes
1055448
Use NetLink sockets of RTNetLink interface socket, they will get you the required in struct net_device_stats format