How to set, or get, local system log file in C? - 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)

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.

Can Logrotate be use in 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

Application calls old source functions

There is an application on remote machine with Linux OS(Fedora), writing to the log file when certain events occur. Some time ago I changed format of the message being written to the log file. But recently it turned out that for some reason in some seldom cases log files with old format messages appear there. I know for sure that none part of my code can write such strings. Also there is no instance of the old application running. Does anyone have some ideas why it can happen? It's not possible to check which process writes those files because anything like auditctl is not installed there, and neither package manager or yum to get it or install. Application is written in C language.
you can use fuser command to find out all the processes that are using that file
`fuser file.log`

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.

Compile C++ over FTP

I would like to know if I can use g++ to compile C++ source files stored on an FTP server? Can this be done?
Note: The FTP server is within the local network
You can't execute commands over FTP, nor can you operate directly on files stored on an FTP server unless you have mapped the FTP server to a filesystem. How to do the latter depends on your operating system.
Since you said g++, I assume Linux, so look into FTPFS.
No, this is not possible. Ftp doesn't allow you to execute programs on the server, it is just used to transfer files. To execute programs (like the compiler) you need some different kind of access to the server, like for example with Ssh.
If you are using a Linux system (and probably any *nix or BSD flavout as well) then yes it is possible if the ftp-server is mounted as a filesystem on your machine, like Tyler McHenry wrote.
It is however not neccessary to "look into FTPFS" if you are using any recent Gnome-based distro. In Ubuntu (9.04) I can do "Places"->"Connect to server" and choose FTP. Then, when the folder is opened in Nautilus you can find the mounted directory in ~/.gvfs/ and then you should be able to compile it without any trouble at all.
I would be very surprised if KDE did not have the same feature, but the directory will be mounted somewhere else.
Does the FTP server have a public website that works with scripting languages, such as PHP? If so, you could upload your source code, edit a PHP file that calls system and compile your code.
In general this probably isn't a good idea: It's a slow, manual process & could be subject to security problems if the PHP script lets you edit the compilation command.

Resources