Can Logrotate be use in C? - c

I'm a newbie on linux and I was planning to use logrotate so that my log files which generate inside a radio file system, would not getting larger size and cause any memory issue.
but as can see from below, this configuration is set at logrotate.conf file in linux, then I am confused and wonder is it possible to add this thing as a "coding style" in my C files so that it work on my radio which is running Linux system.
The radio don't have this logrotate thing under /etc/logrotate.d path after all. I also unable to install any logrotate or execute the command "man logroate", it tells : no manual entry for 'logrotate'.
/var/logs/*.log {
hourly
missingok
rotate 24
compress
create
}
Can it be called like normal function such as fclose() or fopen() in .C files?
If it can be used in C files, what is the correct way to configure the parameter?

I think you misinterpreted how logrotate work: It only work with files created by service/application on host machine.
For example on your linux machine you are running mailing server postfix (base on config you posted):
Postfix service filling log file /var/log/postfix.log
Logrotate take every hour postfix.log and create compressed file postfix.1
Increment number of already existing archive files
Logrotate keep 24 compressed files (= 1 day)
In you case you have to setup application logging to save log files and then you can apply logrotate to it.
Logrotate man page can be found here

Related

How can I get access to files transferred from Windows to WSL-2 Ubuntu?

I have a Linux subsystem installed on my Windows machine. I've transferred a tar.gz file I want to access by finding the location of my subsystem and dragging the files over. But when I run the command:
tar -zxvf file_name.tar.gz
I get the error:
tar (child): vmd-1.9.4a51.bin.LINUXAMD64-CUDA102-OptiX650-OSPRay185.opengl.tar.gz: Cannot open: Permission denied
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
I assume permission being denied is to do with having transferred from Windows since I couldn't access directories I created through Windows either. So, is there something I need to change to gain access to these files?
(PS. I know there are other way of getting tar.gz files other than transferring from Windows, but I'll need to do this for other folders too, I only included the filetype in case it was relevant .)
EDIT: You shouldn't attempt to drag files over. See answer below.
For starters, this belongs on Super User since it doesn't deal directly with a programming question. But since you've already provide an answer here that may be slightly dangerous (and even in your question), I didn't want to leave this unanswered for other people to find inadvertently.
If you used the first method in that link, you are using a WSL1 instance, not WSL2. Only WSL1 made the filesystem available in that way. And it's a really, really bad idea:
There is one hard-and-fast rule when it comes to WSL on Windows:
DO NOT, under ANY circumstances, access, create, and/or modify Linux files inside of your %LOCALAPPDATA% folder using Windows apps, tools, scripts, consoles, etc.
Opening files using some Windows tools may read-lock the opened files and/or folders, preventing updates to file contents and/or metadata, essentially resulting in corrupted files/folders.
I'm guessing you probably went through the install process for WSL2, but you installed your distribution before setting wsl --set-default-version 2 or something like that.
As you can see in the Microsoft link above, there's now a safe method for transferring and editing files between Windows and WSL - the \\wsl$\ tmpfs mounts. Note that as a tmpfs mount stored in memory, it's really more for transferring files over. They will disappear when you reboot or shutdown WSL.
But even if you'd used the second method in that article (/mnt/c), you probably would have run into permissions issues. If you do, the solution should be to remount the C: drive with your uid/gid as I describe here.

How can I create a bootable dvd with custom DOS boot script?

We have a bootable USB stick that works perfectly. It will boot, ask the user for some options, then write files to the hard drive accordingly. We did this by using rufus-2.1, throwing freedos into the USB stick, and basically hijacking autoexec.bat. Autoexec.bat now executes some separate .bat scripts based on user input.
The problem is, we now want to have an option to allow users to burn an iso to a cd or dvd. We've been trying to create an image of the USB stick and burn it to a DVD.
We found plenty of information on this subject, but nothing has worked. We started by extracting the bootimage from various bootable iso files using imgburn. None of these worked. We tried extracting the bootimage from our USB stick, and that also didn't work.
Next we tried WinImage. We extracted the boot information from boot98.exe, hijacked autoexec.exe, created a new image and injected the files. We saved the files as a bootimage and tried the above method again, which also didn't work.
Is there perhaps a more straightforward way to execute DOS .bat scripts through cd/dvd boot? We'd be open to something other than DOS as well, as long as it can write files to a hard drive based on user input through a simple boot interface.
Note: The computer itself does not contain an operating system.
Thanks.
We found a very slick setup that does what we need. Unfortunately, the CD-ROM drivers do not work properly on modern DVD drives. Using this method, it should be possible to put all the scripts directly in autoexec.bat and run commands directly on the "simulated" floppy drive. For whatever reason, we can boot from the "floppy" section of the CD-ROM, but it cannot copy files from A: to C:. I believe we are very close, but no cigar.
http://www.hiren.info/pages/bootablecd
First method that actually produced a bootable CD and allowed scripts to be run by a choice entered by the user.

How to set, or get, local system log file in C?

I was wondering how to set or find the local log files on your computer in Linux. I was reading the GNU C guide on syslog, and when you open a log, you declare the log your going to write to. In the examples they have LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, etc.. How do you set these, or where are they located? I was also wondering where LOG_SYSLOG is located? When i was looking under /var/log, i didn't see anything that looked like a system log. I cannot seem to find much online about the log files themselves. Only how to use the syslog functions.
in linux (I'm running ubuntu 14.04)
the log files are (by default) in /var/log/
one of those files is 'syslog'
if the system has been up for a while
there will also be files named similar to:
'syslog.#.gz' which is a zipped file of the prior syslog(s)

How to determine if a file is on a FAT system (to see if it really is executable)

I am working at an OS independent file manager, and I divide files in groups, usually based on the extension. On Linux, I check if a file has the executable permissions or not, and if it does, I add it to the executables group.
This works great for Windows or Linux, but if you combine them it doesn't work so well. For example, while using it on Linux and exploring a windows mounted drive, all the files appear to be executable. I am trying to find a way to ignore those files and not add them to the executables group.
My code (on Linux) uses stat:
#ifndef WINDOWS
stat(ep->d_name, &buf);
....
if(!files_list[i].is_dir && buf.st_mode & 0111)
files_list[i].is_exe=1;
#endif
The first part of the answer is to find what filesystem the file is mounted on. To do that you need to find the filesystem using the st_dev field of the stat information for the file. (You can also do this by checking the file path, but you then have to check every path element for symbolic links).
You can then cross-reference the st_dev field with the mount table in /proc/mounts using getmntent_r(). There's an example of that in a previous answer. The mnt_type field will give you the text of the filesystem type, and you'll need to compare the string with a list of Windows filesystems.
Once you've found the filesystem, the only way to identify an executable is by heuristics. As other people have suggested, you can look at the file extension for Windows executables, and look at the initial bytes of the file for Linux executables. Don't forget executable scripts with the #! prefix, and you may need to read into a Jar file to find out if it contains an executable static main() method.
If you are browsing Windows files then you need to apply Windows rules for whether or not a file is executable. If the file extension is .EXE, .COM, .BAT, or .CMD then it is executable. If you want a more complete list then you should check MSDN. Note that it is possible to add registry entries on a machine that makes any extension you want to be considered executable, but it is best to ignore that kind of thing when you are browsing a drive from the network.
The fact is that you are fighting an uphill battle. The reason all the files have executable permissions is that the windows filesystem driver on Linux allows you to specify that as an option. This masks whether or not any files are Linux exceutables, for instance.
However, you could look into the file header for EVERY file and see if it is a Linux ELF executable (just like the Linux file command does).
It might be helpful to start by checking all the information about mounted filesystems so that you know what you are dealing with. For instance, do you have a CIFS filesystem mounted that is actually a Linux filesystem served up by SAMBA? If you enumerate every bit of information available about the mounted filesystem plus the complete set of stat info, you can probably identify combinations that act as fingerprints of the different scenarios.
Another option I could imagine, is to call the file util, and depend on its output (maybe its enough to grep for the words executable / script). This util exist/is compileable for windows (basically it just checks for some magic bytes in the files), too.

How to implement a file type based filesystem?

I want to essentially make it so that you never need to unzip/unrar any files. Currently, I have a Dokan filesystem which can do it given a specific zip file but I wanted to know how I can make it apply to all files. Meaning, I want to be able to compile a program that has "fopen("test.zip/1.jpg", "rb");". I think that a Shell Extension would work for dynamically loading the file into the filesystem IF I were browsing in the shell explorer but that doesnt help me with the fopen example. Any ideas?
What you want to do can be used with help of file system filter driver, which would track directory enumeration requests and report directories in place of ZIP files. Then this driver would create virtual files and take the data from ZIP archives. Quite a lot of kernel-mode work, I should say. And file system filter driver is not a file system driver, so dokan won't help you at all.

Resources