Tag Archives: Linux system management

[Solved] Linux Calls mmap Error: mmap: Invalid argument

Background

When using shared memory on Linux, the most common way is to use MMAP mapping file: a few days ago, when writing a program, there was an error in the execution of MMAP:

./test_mmap tmp.txt
mmap: Invalid argument

The procedure is as follows:

#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
    int fd;
    struct stat sb;
    char *mmapped;

    if (argc != 2) {
        return -1;
    }

    fd = open(argv[1], O_RDWR);
    if(fd < 0) {
        perror("open");
        return -1;
    }

    if(fstat(fd, &sb) == -1) {
        perror("fstat");
        return -1;
    }

    mmapped = mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (mmapped == MAP_FAILED) {
        perror("mmap");
        return -1;
    }

    close(fd);

    mmapped[0] = '1';

    if(msync((void*)mmapped, sb.st_size, MS_SYNC) == -1) {
        perror("msync");
        return -1;
    }

    munmap((void *)mmapped, sb.st_size);

    return 0;
}

I have encountered this problem before, but I forgot what happened and took a little time to find the reason. In order to avoid repeating the mistakes, write an article and record it.

reason

On my environment, the reason for this error is that the mmap mapped file is not on the local machine! My Ubuntu is in a virtual machine, and the host is macOS. A folder on the host is shared to the virtual machine, and it is in this shared folder that the mmap file is in when I execute the program, so this strange error is reported.

Fedora 14 yum Error: Cannot retrieve repository metadata (repomd.xml) for repository

Due to work reasons, Fedora 14 is needed. After installing the system, yum is used to install the software and report an error:
[root@bogon liu]# yum install samba samba-client samba-swat
Loaded plugins: langpacks, presto, refresh-packagekit
Adding en_US to language list
Error: Cannot retrieve repository metadata (repomd.xml) for repository: fedora. Please verify its path and try again
The solution is as follows:
1. Delete all files except Fedora.repo in /etc/yum.repos. D.
2. Modify the content of fedora.repo file, add the comment symbol ‘#’ in front of mirrorList, remove the comment symbol before baseurl, and replace the url.
The modified Fedora.repo file reads as follows:

[fedora]
name=Fedora $releasever - $basearch
failovermethod=priority
baseurl=http://archives.fedoraproject.org/pub/archive/fedora/linux/releases/$releasever/Everything/$basearch/os/
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch
enabled=1
metadata_expire=7d
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch

[fedora-debuginfo]
name=Fedora $releasever - $basearch - Debug
failovermethod=priority
baseurl=http://archives.fedoraproject.org/pub/archive/fedora/linux/releases/$releasever/Everything/$basearch/debug/
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-debug-$releasever&arch=$basearch
enabled=0
metadata_expire=7d
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch

[fedora-source]
name=Fedora $releasever - Source
failovermethod=priority
baseurl=http://archives.fedoraproject.org/pub/archive/fedora/linux/releases/$releasever/Everything/source/SRPMS/
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-source-$releasever&arch=$basearch
enabled=0
metadata_expire=7d
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch

Then you can install the software normally
.