udev_db.c revision 726687ad48bdececed1e7e44387c50e009e28208
afe3ab588a6b2992efe5a9b22ed038545ba3cdbfLennart Poettering/*
c343be283b7152554bac0c02493a4e1759c163f7Kay Sievers * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
b3ae710c251d0ce5cf2cef63208e325497b5e323Zbigniew Jędrzejewski-Szmek * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
b3ae710c251d0ce5cf2cef63208e325497b5e323Zbigniew Jędrzejewski-Szmek *
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * This program is free software; you can redistribute it and/or modify it
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * under the terms of the GNU General Public License as published by the
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * Free Software Foundation version 2 of the License.
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers *
8adaf7bd23baa6e2cd99e9e88e55d0f5f5db29a2Richard Maw * This program is distributed in the hope that it will be useful, but
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek * WITHOUT ANY WARRANTY; without even the implied warranty of
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek * General Public License for more details.
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek *
d19e85f0d474ed1882561b458d528cbae49f640eZbigniew Jędrzejewski-Szmek * You should have received a copy of the GNU General Public License along
3e495a6651609d0a45b62aab5c3ed5a3b40e11abZbigniew Jędrzejewski-Szmek * with this program; if not, write to the Free Software Foundation, Inc.,
3e495a6651609d0a45b62aab5c3ed5a3b40e11abZbigniew Jędrzejewski-Szmek * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
3e495a6651609d0a45b62aab5c3ed5a3b40e11abZbigniew Jędrzejewski-Szmek *
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering */
f85857df75cfedbc0d10b8ca2400188dc8f4c22eLennart Poettering
f38afcd0c7f558ca5bf0854b42f8c6954f8ad7f3Lennart Poettering
bafb15bab99887d1b6b8a35136531bac6c3876a6Lennart Poettering#include <stdlib.h>
83a1ff25e5228b0a5b2cc942fd4f964d10bb73b0Zbigniew Jędrzejewski-Szmek#include <stdio.h>
83a1ff25e5228b0a5b2cc942fd4f964d10bb73b0Zbigniew Jędrzejewski-Szmek#include <string.h>
83a1ff25e5228b0a5b2cc942fd4f964d10bb73b0Zbigniew Jędrzejewski-Szmek#include <stddef.h>
83a1ff25e5228b0a5b2cc942fd4f964d10bb73b0Zbigniew Jędrzejewski-Szmek#include <unistd.h>
83a1ff25e5228b0a5b2cc942fd4f964d10bb73b0Zbigniew Jędrzejewski-Szmek#include <fcntl.h>
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering#include <string.h>
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering#include <errno.h>
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering#include <dirent.h>
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering#include <sys/stat.h>
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering#include <sys/types.h>
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering#include "udev.h"
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering#include "udev_selinux.h"
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering
5e524b404b7b319aa6a24bf21132b1074b206996Lennart Poettering
fd6c2363af2cb144bb6a7d6b8bdba9f777440078Lennart Poetteringstatic size_t devpath_to_db_path(const char *devpath, char *filename, size_t len)
47ee3ee03483efd271642d5043070cbd171f19d4Lennart Poettering{
0d4c4b7141c494fd8db273cf637a17af53674b2fLennart Poettering size_t start;
0d4c4b7141c494fd8db273cf637a17af53674b2fLennart Poettering
3efc8c72f02423ece08b5c4d5c812420092cc10aLennart Poettering /* translate to location of db file */
3efc8c72f02423ece08b5c4d5c812420092cc10aLennart Poettering strlcpy(filename, udev_root, len);
3efc8c72f02423ece08b5c4d5c812420092cc10aLennart Poettering start = strlcat(filename, "/"DB_DIR"/", len);
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poettering strlcat(filename, devpath, len);
42718282f5b545684ca1a4e3fd71f8c593d15f97Lennart Poettering return path_encode(&filename[start], len - start);
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poettering}
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poettering
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poettering/* reverse mapping from the device file name to the devpath */
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poetteringstatic int name_index(const char *devpath, const char *name, int add)
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poettering{
03364e472bccf64544e3a0d36d09f90c78f9f699Lennart Poettering char device[PATH_SIZE];
573831165360e89a880f90627753962ab28c4f37Lennart Poettering char filename[PATH_SIZE * 2];
abab50081c8c12cc46482a43264deb46853bb8faLennart Poettering size_t start;
abab50081c8c12cc46482a43264deb46853bb8faLennart Poettering int fd;
e6a26d8c972d45a0927ad0b7c654c830daa2243eLennart Poettering
e6a26d8c972d45a0927ad0b7c654c830daa2243eLennart Poettering /* directory with device name */
e6a26d8c972d45a0927ad0b7c654c830daa2243eLennart Poettering strlcpy(filename, udev_root, sizeof(filename));
e6a26d8c972d45a0927ad0b7c654c830daa2243eLennart Poettering start = strlcat(filename, "/"DB_NAME_INDEX_DIR"/", sizeof(filename));
e6a26d8c972d45a0927ad0b7c654c830daa2243eLennart Poettering strlcat(filename, name, sizeof(filename));
e6a26d8c972d45a0927ad0b7c654c830daa2243eLennart Poettering path_encode(&filename[start], sizeof(filename) - start);
1361205099406d2a19d64547448638a6b665af81Lennart Poettering /* entry with the devpath */
1361205099406d2a19d64547448638a6b665af81Lennart Poettering strlcpy(device, devpath, sizeof(device));
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poettering path_encode(device, sizeof(device));
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poettering strlcat(filename, "/", sizeof(filename));
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poettering strlcat(filename, device, sizeof(filename));
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poettering
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poettering if (add) {
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poettering info("creating index: '%s'\n", filename);
a2088fd025deb90839c909829e27eece40f7fce4Lennart Poettering create_path(filename);
c874ef05a705d3c679e5fd5a50b81e1f5512c4fdLennart Poettering fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
c874ef05a705d3c679e5fd5a50b81e1f5512c4fdLennart Poettering if (fd > 0)
affb71da79b0c3cbcd6ff6c0cedb218a14162c7cLennart Poettering close(fd);
affb71da79b0c3cbcd6ff6c0cedb218a14162c7cLennart Poettering } else {
affb71da79b0c3cbcd6ff6c0cedb218a14162c7cLennart Poettering info("removing index: '%s'\n", filename);
ff609b8ecd0e36bfff0ba120932307274d993dc8Lennart Poettering unlink(filename);
ff609b8ecd0e36bfff0ba120932307274d993dc8Lennart Poettering delete_path(filename);
ff609b8ecd0e36bfff0ba120932307274d993dc8Lennart Poettering }
eedb4ac83158bad6f74305612660b4aff6acc6b4Lennart Poettering return 0;
eedb4ac83158bad6f74305612660b4aff6acc6b4Lennart Poettering}
de587378ea5d22e11373b18b4fcabf8f26f89529Lennart Poettering
de587378ea5d22e11373b18b4fcabf8f26f89529Lennart Poetteringint udev_db_get_devices_by_name(const char *name, struct list_head *name_list)
203e81db24ccb9b4dcb0b1bad0ba554116267d20Lennart Poettering{
203e81db24ccb9b4dcb0b1bad0ba554116267d20Lennart Poettering char dirname[PATH_MAX];
63432f5d9570b76a8efe82702d69611c20645530Lennart Poettering size_t start;
63432f5d9570b76a8efe82702d69611c20645530Lennart Poettering DIR *dir;
fd6c2363af2cb144bb6a7d6b8bdba9f777440078Lennart Poettering int rc = 0;
47ee3ee03483efd271642d5043070cbd171f19d4Lennart Poettering
c2570ab99be82eddbee0f397fbf8704a95f6f736Lennart Poettering strlcpy(dirname, udev_root, sizeof(dirname));
c2570ab99be82eddbee0f397fbf8704a95f6f736Lennart Poettering start = strlcat(dirname, "/"DB_NAME_INDEX_DIR"/", sizeof(dirname));
c2570ab99be82eddbee0f397fbf8704a95f6f736Lennart Poettering strlcat(dirname, name, sizeof(dirname));
c2570ab99be82eddbee0f397fbf8704a95f6f736Lennart Poettering path_encode(&dirname[start], sizeof(dirname) - start);
eba6fd30f2a27f51fa328484cb241807c6934ce1Zbigniew Jędrzejewski-Szmek
eba6fd30f2a27f51fa328484cb241807c6934ce1Zbigniew Jędrzejewski-Szmek dir = opendir(dirname);
eba6fd30f2a27f51fa328484cb241807c6934ce1Zbigniew Jędrzejewski-Szmek if (dir == NULL) {
c2570ab99be82eddbee0f397fbf8704a95f6f736Lennart Poettering info("no index directory '%s': %s\n", dirname, strerror(errno));
c2570ab99be82eddbee0f397fbf8704a95f6f736Lennart Poettering rc = -1;
c2570ab99be82eddbee0f397fbf8704a95f6f736Lennart Poettering goto out;
d89e647542a6ceeefac15fbe8e193de7418bf449Lennart Poettering }
d89e647542a6ceeefac15fbe8e193de7418bf449Lennart Poettering
e7e90a8eee056fd12c8ad83470143f7798240dbcLennart Poettering info("found index directory '%s'\n", dirname);
e7e90a8eee056fd12c8ad83470143f7798240dbcLennart Poettering while (1) {
e7e90a8eee056fd12c8ad83470143f7798240dbcLennart Poettering struct dirent *ent;
f06944d65b1a9012a5564b364608796d1fad45d2Lennart Poettering char device[PATH_SIZE];
d72bc5973465018e077fcecdcc720840bc1e9cfdLennart Poettering
d72bc5973465018e077fcecdcc720840bc1e9cfdLennart Poettering ent = readdir(dir);
d72bc5973465018e077fcecdcc720840bc1e9cfdLennart Poettering if (ent == NULL || ent->d_name[0] == '\0')
d72bc5973465018e077fcecdcc720840bc1e9cfdLennart Poettering break;
d72bc5973465018e077fcecdcc720840bc1e9cfdLennart Poettering if (ent->d_name[0] == '.')
d72bc5973465018e077fcecdcc720840bc1e9cfdLennart Poettering continue;
d72bc5973465018e077fcecdcc720840bc1e9cfdLennart Poettering
d72bc5973465018e077fcecdcc720840bc1e9cfdLennart Poettering strlcpy(device, ent->d_name, sizeof(device));
07cc65c70150faa68a63a444d615f922517c7d94Lennart Poettering path_decode(device);
07cc65c70150faa68a63a444d615f922517c7d94Lennart Poettering name_list_add(name_list, device, 0);
07cc65c70150faa68a63a444d615f922517c7d94Lennart Poettering rc++;
07cc65c70150faa68a63a444d615f922517c7d94Lennart Poettering }
07cc65c70150faa68a63a444d615f922517c7d94Lennart Poettering closedir(dir);
07cc65c70150faa68a63a444d615f922517c7d94Lennart Poetteringout:
07cc65c70150faa68a63a444d615f922517c7d94Lennart Poettering return rc;
07cc65c70150faa68a63a444d615f922517c7d94Lennart Poettering}
07cc65c70150faa68a63a444d615f922517c7d94Lennart Poettering
c2570ab99be82eddbee0f397fbf8704a95f6f736Lennart Poetteringint udev_db_rename(const char *devpath_old, const char *devpath)
0a0215783159b9c3a3652b231df36dbff08e0ac5Lennart Poettering{
dcd12626734ce81e89c74515eaca601ef9edf785Lennart Poettering char filename[PATH_SIZE];
dcd12626734ce81e89c74515eaca601ef9edf785Lennart Poettering char filename_old[PATH_SIZE];
dcd12626734ce81e89c74515eaca601ef9edf785Lennart Poettering
17978b17d9f5d3591f1c644938efc9c27aa60485Lennart Poettering devpath_to_db_path(devpath_old, filename_old, sizeof(filename_old));
17978b17d9f5d3591f1c644938efc9c27aa60485Lennart Poettering devpath_to_db_path(devpath, filename, sizeof(filename));
17978b17d9f5d3591f1c644938efc9c27aa60485Lennart Poettering return rename(filename_old, filename);
48e6d6a6e911af0cf4e3ef12b0a3eeb2c8031d8aLennart Poettering}
48e6d6a6e911af0cf4e3ef12b0a3eeb2c8031d8aLennart Poettering
48e6d6a6e911af0cf4e3ef12b0a3eeb2c8031d8aLennart Poetteringint udev_db_add_device(struct udevice *udev)
48e6d6a6e911af0cf4e3ef12b0a3eeb2c8031d8aLennart Poettering{
8aa203819fc7f2a840191f8d9d0e65566c0ce98eLennart Poettering char filename[PATH_SIZE];
8aa203819fc7f2a840191f8d9d0e65566c0ce98eLennart Poettering
b18d23d7ac6a53d52b99dbf0b2048d5a946a2e28Lennart Poettering if (udev->test_run)
b18d23d7ac6a53d52b99dbf0b2048d5a946a2e28Lennart Poettering return 0;
449a22a58f0fa55d9d0fd2eb597272446e032acdLennart Poettering
449a22a58f0fa55d9d0fd2eb597272446e032acdLennart Poettering devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
449a22a58f0fa55d9d0fd2eb597272446e032acdLennart Poettering create_path(filename);
449a22a58f0fa55d9d0fd2eb597272446e032acdLennart Poettering unlink(filename);
60d17b74d714fa15d68639062dc2932d7b096229Lennart Poettering
60d17b74d714fa15d68639062dc2932d7b096229Lennart Poettering /*
60d17b74d714fa15d68639062dc2932d7b096229Lennart Poettering * don't waste tmpfs memory pages, if we don't have any data to store
60d17b74d714fa15d68639062dc2932d7b096229Lennart Poettering * create fake db-file; store the node-name in a symlink target
60d17b74d714fa15d68639062dc2932d7b096229Lennart Poettering */
2f653bded321fc2271edcda43d54fcc3e6c20dc9Lennart Poettering if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
2f653bded321fc2271edcda43d54fcc3e6c20dc9Lennart Poettering !udev->partitions && !udev->ignore_remove) {
2f653bded321fc2271edcda43d54fcc3e6c20dc9Lennart Poettering int ret;
ac749874bbb66c0e7eff15ca35d1616d29b6f3c1Lennart Poettering dbg("nothing interesting to store, create symlink\n");
ac749874bbb66c0e7eff15ca35d1616d29b6f3c1Lennart Poettering selinux_setfscreatecon(filename, NULL, S_IFLNK);
e342b74468870f2e4f3e15f7277a0adea42183caZbigniew Jędrzejewski-Szmek ret = symlink(udev->name, filename);
e342b74468870f2e4f3e15f7277a0adea42183caZbigniew Jędrzejewski-Szmek selinux_resetfscreatecon();
e342b74468870f2e4f3e15f7277a0adea42183caZbigniew Jędrzejewski-Szmek if (ret != 0) {
036a4773ffa1152e4cabd27473450b8cc01e019bLennart Poettering err("unable to create db link '%s': %s\n", filename, strerror(errno));
036a4773ffa1152e4cabd27473450b8cc01e019bLennart Poettering return -1;
036a4773ffa1152e4cabd27473450b8cc01e019bLennart Poettering }
036a4773ffa1152e4cabd27473450b8cc01e019bLennart Poettering } else {
a88abde72169ddc2df77df3fa5bed30725022253Lennart Poettering FILE *f;
a88abde72169ddc2df77df3fa5bed30725022253Lennart Poettering struct name_entry *name_loop;
a88abde72169ddc2df77df3fa5bed30725022253Lennart Poettering
a88abde72169ddc2df77df3fa5bed30725022253Lennart Poettering f = fopen(filename, "w");
f93b36affa5ac5710cd84bfb8ff0dafabe99fbf1Lennart Poettering if (f == NULL) {
f93b36affa5ac5710cd84bfb8ff0dafabe99fbf1Lennart Poettering err("unable to create db file '%s': %s\n", filename, strerror(errno));
20760dcf5df7e253c21a51886cdb02dee612730bLennart Poettering return -1;
20760dcf5df7e253c21a51886cdb02dee612730bLennart Poettering }
20760dcf5df7e253c21a51886cdb02dee612730bLennart Poettering dbg("storing data for device '%s' in '%s'\n", udev->dev->devpath, filename);
20760dcf5df7e253c21a51886cdb02dee612730bLennart Poettering
20760dcf5df7e253c21a51886cdb02dee612730bLennart Poettering fprintf(f, "N:%s\n", udev->name);
df22b0bbfde403b3fa71f3c2f1e2ca38af946388Lennart Poettering list_for_each_entry(name_loop, &udev->symlink_list, node) {
df22b0bbfde403b3fa71f3c2f1e2ca38af946388Lennart Poettering fprintf(f, "S:%s\n", name_loop->name);
df22b0bbfde403b3fa71f3c2f1e2ca38af946388Lennart Poettering /* add symlink-name to index */
df22b0bbfde403b3fa71f3c2f1e2ca38af946388Lennart Poettering name_index(udev->dev->devpath, name_loop->name, 1);
73cb77549536deab85d8d1261b5381e87d80ab23Lennart Poettering }
73cb77549536deab85d8d1261b5381e87d80ab23Lennart Poettering fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
73cb77549536deab85d8d1261b5381e87d80ab23Lennart Poettering if (udev->link_priority != 0)
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering fprintf(f, "L:%u\n", udev->link_priority);
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering if (udev->event_timeout >= 0)
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering fprintf(f, "T:%u\n", udev->event_timeout);
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering if (udev->partitions != 0)
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering fprintf(f, "A:%u\n", udev->partitions);
477e75ef9ea6bb2e7e8cc76278c442942110f227Lennart Poettering if (udev->ignore_remove)
7dfb0404b3b6882d582a571f61a52b2f56961675Lennart Poettering fprintf(f, "R:%u\n", udev->ignore_remove);
7dfb0404b3b6882d582a571f61a52b2f56961675Lennart Poettering list_for_each_entry(name_loop, &udev->env_list, node)
7dfb0404b3b6882d582a571f61a52b2f56961675Lennart Poettering fprintf(f, "E:%s\n", name_loop->name);
7dfb0404b3b6882d582a571f61a52b2f56961675Lennart Poettering fclose(f);
7dfb0404b3b6882d582a571f61a52b2f56961675Lennart Poettering }
2bc39683c0cada86c9dc39e5f3d0ea475cf12e57Lennart Poettering
2bc39683c0cada86c9dc39e5f3d0ea475cf12e57Lennart Poettering /* add name to index */
2bc39683c0cada86c9dc39e5f3d0ea475cf12e57Lennart Poettering name_index(udev->dev->devpath, udev->name, 1);
63b20d5c0fa61e3aa8235de269161d0ee877ebccMichal Sekletar
63b20d5c0fa61e3aa8235de269161d0ee877ebccMichal Sekletar return 0;
63b20d5c0fa61e3aa8235de269161d0ee877ebccMichal Sekletar}
63b20d5c0fa61e3aa8235de269161d0ee877ebccMichal Sekletar
63b20d5c0fa61e3aa8235de269161d0ee877ebccMichal Sekletarint udev_db_get_device(struct udevice *udev, const char *devpath)
63b20d5c0fa61e3aa8235de269161d0ee877ebccMichal Sekletar{
687f6a0ba77872299b9fb1f2f04d31c977088a63Lennart Poettering struct stat stats;
31a11e8f30449a81867e8fd081e3e76cf6664bb4Lennart Poettering char filename[PATH_SIZE];
31a11e8f30449a81867e8fd081e3e76cf6664bb4Lennart Poettering char line[PATH_SIZE];
41bc22f3a0d5c61e019a0b493c430941d56aba4eLennart Poettering unsigned int maj, min;
dbdee28bfadd6d8bd93cb34c85ce1fc325dd8120Lennart Poettering char *bufline;
17fe56148c44dfa5583a8643c1918fd6eccf2aeeLennart Poettering char *buf;
17fe56148c44dfa5583a8643c1918fd6eccf2aeeLennart Poettering size_t bufsize;
17fe56148c44dfa5583a8643c1918fd6eccf2aeeLennart Poettering size_t cur;
17fe56148c44dfa5583a8643c1918fd6eccf2aeeLennart Poettering size_t count;
7687f85ea6bab434324bb985e2898bf6373891bfLennart Poettering
7687f85ea6bab434324bb985e2898bf6373891bfLennart Poettering sysfs_device_set_values(udev->dev, devpath, NULL, NULL);
7687f85ea6bab434324bb985e2898bf6373891bfLennart Poettering devpath_to_db_path(devpath, filename, sizeof(filename));
7687f85ea6bab434324bb985e2898bf6373891bfLennart Poettering
c14db9b32ab90738973071d31f259d1a457d7b4aLennart Poettering if (lstat(filename, &stats) != 0) {
c14db9b32ab90738973071d31f259d1a457d7b4aLennart Poettering info("no db file to read %s: %s\n", filename, strerror(errno));
fa607802f332e06f4044c3eb38dbea41076c803dLennart Poettering return -1;
fa607802f332e06f4044c3eb38dbea41076c803dLennart Poettering }
fa607802f332e06f4044c3eb38dbea41076c803dLennart Poettering if ((stats.st_mode & S_IFMT) == S_IFLNK) {
a47e6701bfc45519a4e038daa52e9236e932f59aLennart Poettering char target[NAME_SIZE];
92ff080be100aff15f292e2631921131c610afe7Lennart Poettering int target_len;
b80c66ba9836456de5260e4a1b696ba25561f613Lennart Poettering
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poettering info("found a symlink as db file\n");
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poettering target_len = readlink(filename, target, sizeof(target));
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poettering if (target_len > 0)
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poettering target[target_len] = '\0';
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poettering else {
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poettering info("error reading db link %s: %s\n", filename, strerror(errno));
de146bb2aac13361ade3050d37696499ac4ca9aeLennart Poettering return -1;
de146bb2aac13361ade3050d37696499ac4ca9aeLennart Poettering }
de146bb2aac13361ade3050d37696499ac4ca9aeLennart Poettering dbg("db link points to '%s'\n", target);
de146bb2aac13361ade3050d37696499ac4ca9aeLennart Poettering strlcpy(udev->name, target, sizeof(udev->name));
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poettering return 0;
c3a0d00d6dd6f5997d673e133ef6f9f856550b0aLennart Poettering }
82e6c50c473f4be8df77c7a510577f1975eedddbLennart Poettering
82e6c50c473f4be8df77c7a510577f1975eedddbLennart Poettering if (file_map(filename, &buf, &bufsize) != 0) {
d2f81fb00cc3c49e21b31000ba7d37b81a260257Lennart Poettering info("error reading db file %s: %s\n", filename, strerror(errno));
d2f81fb00cc3c49e21b31000ba7d37b81a260257Lennart Poettering return -1;
98cd2651988619bf606f0b27825440c4638a7e0bLennart Poettering }
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poettering
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poettering cur = 0;
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poettering while (cur < bufsize) {
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poettering count = buf_get_line(buf, bufsize, cur);
2a441c8afe7c81f74cc2ceccdf9a71301cd39d4dLennart Poettering bufline = &buf[cur];
40ff4a4abd76d71408e598ed02d7860b8465fa9aLennart Poettering cur += count+1;
40ff4a4abd76d71408e598ed02d7860b8465fa9aLennart Poettering
40ff4a4abd76d71408e598ed02d7860b8465fa9aLennart Poettering if (count > sizeof(line))
40ff4a4abd76d71408e598ed02d7860b8465fa9aLennart Poettering count = sizeof(line);
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering memcpy(line, &bufline[2], count-2);
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering line[count-2] = '\0';
f131770b1465fbf423881f16ba85523a05f846feVeres Lajos
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering switch(bufline[0]) {
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering case 'N':
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering strlcpy(udev->name, line, sizeof(udev->name));
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering break;
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering case 'M':
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering sscanf(line, "%u:%u", &maj, &min);
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering udev->devt = makedev(maj, min);
0a86c1a9d8066267b878dfeddc5e0087dda6a37bLennart Poettering break;
e3286870fdf20c3c93e944b24fd9af53620f7dbaLennart Poettering case 'S':
e3286870fdf20c3c93e944b24fd9af53620f7dbaLennart Poettering name_list_add(&udev->symlink_list, line, 0);
e3286870fdf20c3c93e944b24fd9af53620f7dbaLennart Poettering break;
e3286870fdf20c3c93e944b24fd9af53620f7dbaLennart Poettering case 'L':
8514b67754c5ff7fa628929b3d27131010c21842Lennart Poettering udev->link_priority = atoi(line);
8514b67754c5ff7fa628929b3d27131010c21842Lennart Poettering break;
7b0fce617c48eda32b2d4e04b5f0e4376e8c0106Lennart Poettering case 'T':
7b0fce617c48eda32b2d4e04b5f0e4376e8c0106Lennart Poettering udev->event_timeout = atoi(line);
7b0fce617c48eda32b2d4e04b5f0e4376e8c0106Lennart Poettering break;
7b0fce617c48eda32b2d4e04b5f0e4376e8c0106Lennart Poettering case 'A':
7b0fce617c48eda32b2d4e04b5f0e4376e8c0106Lennart Poettering udev->partitions = atoi(line);
b568ef14a75dffb7182e0acbdec743b31df2a597Lennart Poettering break;
c2d5b3c94d0c082ef29597fb230f8b88b124bab8Lennart Poettering case 'R':
264b8070715d2d19344c4991ace21147d998f56dLennart Poettering udev->ignore_remove = atoi(line);
264b8070715d2d19344c4991ace21147d998f56dLennart Poettering break;
7e27f3121e5a10629302b5221eb21345f832724aLennart Poettering case 'E':
7e27f3121e5a10629302b5221eb21345f832724aLennart Poettering name_list_add(&udev->env_list, line, 0);
f81e67f79fa856aa2ecffad4d014772ce981745cLennart Poettering break;
f81e67f79fa856aa2ecffad4d014772ce981745cLennart Poettering }
d48b7bd271b1e70924c8485d2f95c2f5a1ae77cbLennart Poettering }
d48b7bd271b1e70924c8485d2f95c2f5a1ae77cbLennart Poettering file_unmap(buf, bufsize);
758c4d7a391c0e024737053c815bf3924653b8c5Lennart Poettering
758c4d7a391c0e024737053c815bf3924653b8c5Lennart Poettering if (udev->name[0] == '\0')
821cc13ddae40fb7608458b44aaa7a3fd33d56d9Lennart Poettering return -1;
821cc13ddae40fb7608458b44aaa7a3fd33d56d9Lennart Poettering
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering return 0;
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering}
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poetteringint udev_db_delete_device(struct udevice *udev)
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering{
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering char filename[PATH_SIZE];
8483d73ff158ee0d51ccbba09a470cc6ae9b071aLennart Poettering struct name_entry *name_loop;
25e773eeb4f853804e1bf0dbd9a184f23e9b2a97Kay Sievers
b857e042d621ffb98a652f33850b431fafbece43Lennart Poettering if (udev->test_run)
6dab5bb18151c80fc39bd51f03dcff40b920de3eLennart Poettering return 0;
6dab5bb18151c80fc39bd51f03dcff40b920de3eLennart Poettering
384a4be2b00cb95ce215dd343cc9aa77adc9b1ecLennart Poettering devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
384a4be2b00cb95ce215dd343cc9aa77adc9b1ecLennart Poettering unlink(filename);
706d97503df83d141d241b645d2c920d691b3d62Lennart Poettering
706d97503df83d141d241b645d2c920d691b3d62Lennart Poettering name_index(udev->dev->devpath, udev->name, 0);
3bcde97e8502c48b53f7420e2433ca68e601662dLennart Poettering list_for_each_entry(name_loop, &udev->symlink_list, node)
3bcde97e8502c48b53f7420e2433ca68e601662dLennart Poettering name_index(udev->dev->devpath, name_loop->name, 0);
533bb267f13e2f7b4d7b78de30e821dc81c82335Lennart Poettering
533bb267f13e2f7b4d7b78de30e821dc81c82335Lennart Poettering return 0;
279f036675536d55c901562b49f9df146af1a0e3Lennart Poettering}
533bb267f13e2f7b4d7b78de30e821dc81c82335Lennart Poettering
b42de08aa4c97636e42c28c7bce08f0d7c2a719aZbigniew Jędrzejewski-Szmekint udev_db_get_all_entries(struct list_head *name_list)
b42de08aa4c97636e42c28c7bce08f0d7c2a719aZbigniew Jędrzejewski-Szmek{
b42de08aa4c97636e42c28c7bce08f0d7c2a719aZbigniew Jędrzejewski-Szmek char dbpath[PATH_MAX];
b42de08aa4c97636e42c28c7bce08f0d7c2a719aZbigniew Jędrzejewski-Szmek DIR *dir;
202aea456dfb279cd34da7bfd1880f6ac0fd849fLennart Poettering
42aeb14a4a0fa7d43da96a8ed0fb0e180a2dd5c8Lennart Poettering strlcpy(dbpath, udev_root, sizeof(dbpath));
42aeb14a4a0fa7d43da96a8ed0fb0e180a2dd5c8Lennart Poettering strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
42aeb14a4a0fa7d43da96a8ed0fb0e180a2dd5c8Lennart Poettering dir = opendir(dbpath);
42aeb14a4a0fa7d43da96a8ed0fb0e180a2dd5c8Lennart Poettering if (dir == NULL) {
6868560773ada8ea31d1f86422be6bf026a1f660Richard Maw info("no udev_db available '%s': %s\n", dbpath, strerror(errno));
14a32924c9b46817c92ae11c1147a59dcb62012bLennart Poettering return -1;
14a32924c9b46817c92ae11c1147a59dcb62012bLennart Poettering }
14a32924c9b46817c92ae11c1147a59dcb62012bLennart Poettering
563b1bdc09efe0cf94dd3f514f30376ca854c1ceLennart Poettering while (1) {
563b1bdc09efe0cf94dd3f514f30376ca854c1ceLennart Poettering struct dirent *ent;
5dcf983854c2e8314dbee239180255490ec8ae1cLennart Poettering char device[PATH_SIZE];
5dcf983854c2e8314dbee239180255490ec8ae1cLennart Poettering
5dcf983854c2e8314dbee239180255490ec8ae1cLennart Poettering ent = readdir(dir);
6bd7941ece602ae9962a103c8d65ecda7d642391Tom Gundersen if (ent == NULL || ent->d_name[0] == '\0')
6bd7941ece602ae9962a103c8d65ecda7d642391Tom Gundersen break;
6bd7941ece602ae9962a103c8d65ecda7d642391Tom Gundersen if (ent->d_name[0] == '.')
b873d33ec9583c92a0c2bc6807d010720fa31595Lennart Poettering continue;
d61bb44a89fde3042c7c15ea4975239f7dcb0cb0Lennart Poettering
ed220efd6657822332b9563ec53c5ab9f3c33220Lennart Poettering strlcpy(device, ent->d_name, sizeof(device));
ed220efd6657822332b9563ec53c5ab9f3c33220Lennart Poettering path_decode(device);
41488fe9024a8955d19811620fd55dcc56a5b2baLennart Poettering name_list_add(name_list, device, 1);
41488fe9024a8955d19811620fd55dcc56a5b2baLennart Poettering dbg("added '%s'\n", device);
41488fe9024a8955d19811620fd55dcc56a5b2baLennart Poettering }
41488fe9024a8955d19811620fd55dcc56a5b2baLennart Poettering
41488fe9024a8955d19811620fd55dcc56a5b2baLennart Poettering closedir(dir);
1dbe0a6efda7b1d35957eab7e1d56a2c69d806d9Lennart Poettering return 0;
1dbe0a6efda7b1d35957eab7e1d56a2c69d806d9Lennart Poettering}
157a180e4fc827606833a6724834ba7b0246d650Tom Gundersen