Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove flockfile for tedge_agent before running --init in postint script #1557

Merged

Conversation

rina23q
Copy link
Member

@rina23q rina23q commented Nov 3, 2022

Proposed changes

tedge_agent is supposed to run by the tedge user, therefore, the flockfile is owned by the tedge user. However, due to the directory creation of /var/tedge, tedge_agent --init must be run as root in the postinst script. If the flockfile owned by tedge user exists, the tedge_agent --init will fail.

To fix this issue, the postinst script should remove the flock file if it exists before the initialization.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue

#1551

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

tedge_agent is supposed to run by the 'tedge' user, therefore, the
flockfile is owned by the 'tedge' user. However, due to the directory
creation of '/var/tedge', 'tedge_agent --init' must be run as root in
the postinst script. If the flockfile owned by 'tedge' user exists, the
'tedge_agent --init' will fail.

To fix this issue, the postinst script should remove the flock file if
it exists before the initialization.

Signed-off-by: Rina Fujino <18257209+rina23q@users.noreply.github.com>
rm /run/lock/tedge_agent.lock
fi
# It must be run as root to create the directory `/var/tedge`.
tedge_agent --init
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then, we will have a lock own by root, preventing the agent to run as tedge. Am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My experiment on my laptop, tedge_agent.lock is gone after tedge_agent --init. However, to keep it safe, I can add the lines to remove /run/lock/tedge_agent.lock after tedge_agent --init.

         if [ -f "/run/lock/tedge_agent.lock" ]; then
                rm /run/lock/tedge_agent.lock
         fi

This was the output when I ran sudo tedge_agent --init.

rina@SAG-CFPPNV2-U:/etc/tedge$ inotifywait -m /run/lock/
Setting up watches.
Watches established.
/run/lock/ CREATE tedge_agent.lock
/run/lock/ OPEN tedge_agent.lock
/run/lock/ CLOSE_WRITE,CLOSE tedge_agent.lock
/run/lock/ DELETE tedge_agent.lock
rina@SAG-CFPPNV2-U:~$ sudo tedge_agent --init
[sudo] password for rina: 
2022-11-04T13:18:47.710656337Z  INFO flockfile::unix: Lockfile created "/run/lock/tedge_agent.lock"
2022-11-04T13:18:47.710699697Z  INFO tedge_agent::agent: tedge_agent starting
2022-11-04T13:18:47.712704417Z  INFO sm-agent{config_dir="/etc/tedge"}: tedge_agent::agent: Initializing the tedge agent session

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. It makes sense and it's useless to remove the lock after --init.

Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants