Example of using popen in C

26. January 2012 08:00

 

A short code example of using popen in c to start a process then write to it using fprintf.

 

#include <stdio.h>


int main(int argc, char **argv) {
	FILE *fp = popen("cat", "w");
	int i = 0;

	for(i=0;i<10;i++)
		fprintf(fp, "Count = %d\n", i);

	pclose(fp);

	return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink


More Stuff

25. January 2012 19:48

 

I fix some issues and created some new goodies to play with. 

 

E-mail Kick it! DZone it! del.icio.us Permalink


Random failing terminals

20. January 2012 12:37

 

Well, we had one system on which you couldn't log in on the console for a while after rebooting, but it'd start working sometimes. What was happening was that the manufacturer had, for some idiot reason, hardcoded the names of the terminals they wanted to support into getty (this manufacturers own terminals, that I can understand, but also a handful of common types like adm3a) so getty could clear the screen properly (I guess hacking that into gettydefs was too obvious or something). If getty couldn't recognise the terminal type on the command line, it'd display a message on the console reading "Unknown terminal type pc100". We ignored this flamage, which was a pity. Cos that was the problem.

 

It did this *before* opening the terminal, so if it happened to run between the time rc completed and the getty on the console started the console got attached to some random terminal somewhere, so when login attempted to open /dev/tty to prompt for a password it failed.

 

 

Moral: always deal with error messages even when you *know* they're bogus.

Moral: never cry wolf.

 

E-mail Kick it! DZone it! del.icio.us Permalink


Getting tcpdump to run as non root

19. January 2012 23:43

 

Another short tutorial on how to get tcpdump to run as a non root user. However this is setup correctly so that root and only the permitted users can execute the file and run it. We would not want random people being able to run it stealing our traffic now.

 

You can enable this for non root users in a secure method by using the following commands

 

 

groupadd tcpdump
addgroup <username> tcpdump
chown root.tcpdump /usr/sbin/tcpdump
chmod 0750 tcpdump
setcap "CAP_NET_RAW+eip" /usr/sbin/tcpdump

 

 

As a breif explenation of the above.

  • We create a group called tcpdump. 
  • We then add the user or users that we want to be able to use tcpdump to the group.
  • We then change the user/group of tcpdump to match root and the new group.
  • We then make sure the permissions are set on tcpdump so that members of the group can execute it but other normal users cannot.
  • We then use setcap to give the CAP_NET_RAW priviledge to the executable when it runs. This is so that tcpdump can open its raw socket which is not normally permitted unless you are root.

 

E-mail Kick it! DZone it! del.icio.us Permalink


Color Coding the bash prompt

16. January 2012 07:00

 

Here is a little tip so that you can quickly tell which linux machine that you are currently logged into. The easy way to do this is to colour code the shell prompt.

 

In bash the PS1 enviroment variable controls how the prompt is formatted. On a debian squeeze install it would be set to something like this by default

 

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

 

That of course does look somewhat confusing mostly because of the escaping. The letters with a '\' at the front are telling bash to also display certain things but I will cover that in another post. Lets add some colour.

 

To add some colour we need to add a control sequence to the shell prompt this tells the terminal to enable a specific colour then at the end of the prompt turn that colour back to the default for the terminal.

 

to set the colour we need to apply this to the beginning of the enviroment variable (this is using the color red 31m)

 

\[\033[31m\]

 

Then we also need to append the close sequence to the end of the enviroment variable.

 

\[\033[0m\]

 

So the complete enviroment variable should now look like this.

 

export PS1="\[\033[31m\]\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$\[\033[0m\]"

 

Our prompt will be red. To change the colour you only need to change the starting sequence and choose from one of the following colours.

 

Black 0;30     

Blue 0;34     

Green 0;32     

Cyan 0;36     

Red 0;31     

Purple 0;35     

Brown 0;33     

Light Gray 0;37     

 

Dark Gray 1;30

Light Blue 1;34

Light Green 1;32

Light Cyan 1;36

Light Red 1;31

Light Purple 1;35

Yellow 1;33

White 1;37

 

E-mail Kick it! DZone it! del.icio.us Permalink