blob: 5716323ce6545f69bf6ee1937d0f53a60a85b28d [file] [log] [blame]
Elliott Hughes1b708d32015-12-11 19:07:01 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "diagnose_usb.h"
18
19#include <errno.h>
20#include <unistd.h>
21
Colin Cross9f0e6492021-12-16 13:30:28 -080022#include <algorithm>
Elliott Hughes1b708d32015-12-11 19:07:01 -080023#include <string>
Colin Cross9f0e6492021-12-16 13:30:28 -080024#include <vector>
Elliott Hughes1b708d32015-12-11 19:07:01 -080025
26#include <android-base/stringprintf.h>
27
28#if defined(__linux__)
29#include <grp.h>
Elliott Hughescd356642017-05-03 17:25:34 -070030#include <pwd.h>
Elliott Hughes1b708d32015-12-11 19:07:01 -080031#endif
32
33static const char kPermissionsHelpUrl[] = "http://developer.android.com/tools/device.html";
34
Elliott Hughescd356642017-05-03 17:25:34 -070035// Returns a message describing any potential problems we find with udev, or an empty string if we
36// can't find plugdev information (i.e. udev is not installed).
37static std::string GetUdevProblem() {
Josh Gao27768452018-01-02 12:01:43 -080038#if defined(__linux__) && !defined(__BIONIC__)
Elliott Hughes1b708d32015-12-11 19:07:01 -080039 errno = 0;
40 group* plugdev_group = getgrnam("plugdev");
41
42 if (plugdev_group == nullptr) {
43 if (errno != 0) {
44 perror("failed to read plugdev group info");
45 }
46 // We can't give any generally useful advice here, just let the caller print the help URL.
Elliott Hughescd356642017-05-03 17:25:34 -070047 return "";
Elliott Hughes1b708d32015-12-11 19:07:01 -080048 }
49
Colin Cross9f0e6492021-12-16 13:30:28 -080050 int ngroups = getgroups(0, nullptr);
51 if (ngroups < 0) {
52 perror("failed to get groups list size");
53 return "";
54 }
55
56 std::vector<gid_t> groups(ngroups);
57 ngroups = getgroups(groups.size(), groups.data());
58 if (ngroups < 0) {
59 perror("failed to get groups list");
60 return "";
61 }
62
63 groups.resize(ngroups);
64
65 // getgroups(2) indicates that the egid may not be included so we check it additionally just
66 // to be sure.
67 if (std::find(groups.begin(), groups.end(), plugdev_group->gr_gid) != groups.end() ||
68 getegid() == plugdev_group->gr_gid) {
Elliott Hughes1b708d32015-12-11 19:07:01 -080069 // The user is in plugdev so the problem is likely with the udev rules.
Elliott Hughese7edfbc2020-07-31 15:09:35 -070070 return "missing udev rules? user is in the plugdev group";
Elliott Hughes1b708d32015-12-11 19:07:01 -080071 }
Elliott Hughescd356642017-05-03 17:25:34 -070072 passwd* pwd = getpwuid(getuid());
73 return android::base::StringPrintf("user %s is not in the plugdev group",
74 pwd ? pwd->pw_name : "?");
Elliott Hughes1b708d32015-12-11 19:07:01 -080075#else
Elliott Hughescd356642017-05-03 17:25:34 -070076 return "";
Elliott Hughes1b708d32015-12-11 19:07:01 -080077#endif
78}
79
80// Short help text must be a single line, and will look something like:
Elliott Hughescd356642017-05-03 17:25:34 -070081//
82// no permissions (reason); see [URL]
Elliott Hughes1b708d32015-12-11 19:07:01 -080083std::string UsbNoPermissionsShortHelpText() {
84 std::string help_text = "no permissions";
85
Elliott Hughescd356642017-05-03 17:25:34 -070086 std::string problem(GetUdevProblem());
87 if (!problem.empty()) help_text += " (" + problem + ")";
Elliott Hughes1b708d32015-12-11 19:07:01 -080088
89 return android::base::StringPrintf("%s; see [%s]", help_text.c_str(), kPermissionsHelpUrl);
90}
91
Elliott Hughescd356642017-05-03 17:25:34 -070092// Long help text can span multiple lines but doesn't currently provide more detailed information:
93//
94// insufficient permissions for device: reason
95// See [URL] for more information
Elliott Hughes1b708d32015-12-11 19:07:01 -080096std::string UsbNoPermissionsLongHelpText() {
97 std::string header = "insufficient permissions for device";
98
Elliott Hughescd356642017-05-03 17:25:34 -070099 std::string problem(GetUdevProblem());
100 if (!problem.empty()) header += ": " + problem;
Elliott Hughes1b708d32015-12-11 19:07:01 -0800101
Elliott Hughescd356642017-05-03 17:25:34 -0700102 return android::base::StringPrintf("%s\nSee [%s] for more information", header.c_str(),
103 kPermissionsHelpUrl);
Elliott Hughes1b708d32015-12-11 19:07:01 -0800104}