Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7915557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:29:37+00:00 2026-06-03T14:29:37+00:00

I have a user level application that needs to enable/disable a device by writing

  • 0

I have a user level application that needs to enable/disable a device by writing to it’s device file(which requires root privileges).

Here’s the current implementation of my interface:

bool DeviceInterface::EnableDevice( bool isEnabled )
{
    bool isSuccessful = false;

    //enable device
    if(isEnabled)
    {
        isSuccessful = (system("enableDevice.sh 1") == 0)? true : false;
    }
    //disable device
    else
    {
        isSuccessful = (system("enableDevice.sh 0") == 0)? true : false;
    }
    return isSuccessful ;
}

My script “enableDevice.sh” simply looks like this, and runs just fine when ran as root:

echo $1 > /sys/devices/device_file

This fails as it requires root privileges to be able to write to a device file. I’m totally new to “pipes”, “fork” and “exec”. Can anybody help me how I could pass “0” or “1” to a script to enable and disable the device?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:29:40+00:00Added an answer on June 3, 2026 at 2:29 pm

    In order for ‘enableDevice.sh’ to do this, it needs to be running as root. You could mark it suid
    (chmod u+S enableDevice.sh) and chown it to root. Note that you’ll need to be root to do the chown (on any reasonable unix system).

    Of course you could always open up write permissions for your (well, the programs’) group or for everyone, i.e.
    chmod g+w,o+w /sys/devices/device_file

    I REALLY wouldn’t recommend you do this. if I called that script with ‘\”bunch of nothing at all > /dev/sda’ it’d overwrite the (likely) root of your system drive.

    A better idea would be to have the script check what “$1” is, and then echo 0 or 1 to the device, or do nothing if it’s neither. i.e. don’t trust user data, especially in suid scripts!

    case "$1" in
        enable)   VALUE=1 ;;
        disable)  VALUE=0 ;;
        *)        exit  ;;
    esac
    echo $VALUE > /sys/devices/device_file
    

    Better still, have a deamon which you’ve run as root hanging around watching a named pipe which does (effectively) the above, depending on what it gets in the pipe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two processes - a user process and a root-level LaunchDaemon. I'd like
I have a program that allows the user to enter a level number, and
I have an application that is using Windows Authentication and a SqlRoleProvider for user
I have a web application that needs to display 30,000 markers on a map
I have an ascx component, that holds two-level menu, because there are several user
I have code in an ASP.NET form that needs to, depending on user entry
I’m working on a project and have developed the high level user requirements for
i have user control, which i render on several views. i want to show
I have an application we bought that I need to integrate, and it uses
I have an application that currently stores timestamps in MySQL DATETIME and TIMESTAMP values.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.