blob: 9f721bf5ffdc61be584708b062926cf570a65490 [file] [log] [blame]
Elliott Hughes70097492015-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
22#include <string>
23
24#include <android-base/stringprintf.h>
25
26#if defined(__linux__)
27#include <grp.h>
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070028#include <pwd.h>
Elliott Hughes70097492015-12-11 19:07:01 -080029#endif
30
31static const char kPermissionsHelpUrl[] = "http://developer.android.com/tools/device.html";
32
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070033// Returns a message describing any potential problems we find with udev, or an empty string if we
34// can't find plugdev information (i.e. udev is not installed).
35static std::string GetUdevProblem() {
Elliott Hughes70097492015-12-11 19:07:01 -080036#if defined(__linux__)
37 errno = 0;
38 group* plugdev_group = getgrnam("plugdev");
39
40 if (plugdev_group == nullptr) {
41 if (errno != 0) {
42 perror("failed to read plugdev group info");
43 }
44 // We can't give any generally useful advice here, just let the caller print the help URL.
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070045 return "";
Elliott Hughes70097492015-12-11 19:07:01 -080046 }
47
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070048 // getgroups(2) indicates that the GNU group_member(3) may not check the egid so we check it
Elliott Hughes70097492015-12-11 19:07:01 -080049 // additionally just to be sure.
50 if (group_member(plugdev_group->gr_gid) || getegid() == plugdev_group->gr_gid) {
51 // The user is in plugdev so the problem is likely with the udev rules.
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070052 return "user in plugdev group; are your udev rules wrong?";
Elliott Hughes70097492015-12-11 19:07:01 -080053 }
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070054 passwd* pwd = getpwuid(getuid());
55 return android::base::StringPrintf("user %s is not in the plugdev group",
56 pwd ? pwd->pw_name : "?");
Elliott Hughes70097492015-12-11 19:07:01 -080057#else
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070058 return "";
Elliott Hughes70097492015-12-11 19:07:01 -080059#endif
60}
61
62// Short help text must be a single line, and will look something like:
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070063//
64// no permissions (reason); see [URL]
Elliott Hughes70097492015-12-11 19:07:01 -080065std::string UsbNoPermissionsShortHelpText() {
66 std::string help_text = "no permissions";
67
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070068 std::string problem(GetUdevProblem());
69 if (!problem.empty()) help_text += " (" + problem + ")";
Elliott Hughes70097492015-12-11 19:07:01 -080070
71 return android::base::StringPrintf("%s; see [%s]", help_text.c_str(), kPermissionsHelpUrl);
72}
73
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070074// Long help text can span multiple lines but doesn't currently provide more detailed information:
75//
76// insufficient permissions for device: reason
77// See [URL] for more information
Elliott Hughes70097492015-12-11 19:07:01 -080078std::string UsbNoPermissionsLongHelpText() {
79 std::string header = "insufficient permissions for device";
80
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070081 std::string problem(GetUdevProblem());
82 if (!problem.empty()) header += ": " + problem;
Elliott Hughes70097492015-12-11 19:07:01 -080083
Elliott Hughes3e9e74e2017-05-03 17:25:34 -070084 return android::base::StringPrintf("%s\nSee [%s] for more information", header.c_str(),
85 kPermissionsHelpUrl);
Elliott Hughes70097492015-12-11 19:07:01 -080086}